
文章插图
- 多出个用过的19节点,转化一下,在左子树中删除19的点!那么这个问题又转化为删除节点的问题,查找左子树中有没有能够替代19这个点的 。

文章插图
代码为
public node remove(int x, node t)// 删除节点 {if (t == null) {return null;}if (x < t.value) {t.left = remove(x, t.left);} else if (x > t.value) {t.right = remove(x, t.right);} else if (t.left != null && t.right != null)// 左右节点均不空{t.value = https://www.isolves.com/it/rj/czxt/linux/2019-08-19/findmin(t.right).value;// 找到右侧最小值替代t.right = remove(t.value, t.right);} else // 左右单空或者左右都空{if (t.left == null && t.right == null) {t = null;} else if (t.right != null) {t = t.right;} else if (t.left != null) {t = t.left;}return t;}return t; }完整代码
二叉排序树完整代码为:
package 二叉树;import JAVA.util.ArrayDeque;import java.util.Queue;import java.util.Stack;public class BinarySortTree { class node {// 结点public int value;public node left;public node right;public node() {}public node(int value) {this.value = https://www.isolves.com/it/rj/czxt/linux/2019-08-19/value;this.left = null;this.right = null;}public node(int value, node l, node r) {this.value = value;this.left = l;this.right = r;} } node root;// 根 public BinarySortTree() {root = null; } public void makeEmpty()// 变空 {root = null; } public boolean isEmpty()// 查看是否为空 {return root == null; } public node findmin(node t)// 查找最小返回值是node,调用查看结果时需要.value {if (t == null) {return null;} else if (t.left == null) {return t;} elsereturn (findmin(t.left)); } public node findmax(node t)// 查找最大 {if (t == null) {return null;} else if (t.right == null) {return t;} elsereturn (findmax(t.right)); } public boolean isContains(int x)// 是否存在 {node current = root;if (root == null) {return false;}while (current.value != x && current != null) {if (x < current.value) {current = current.left;}if (x > current.value) {current = current.right;}if (current == null) {return false;} // 在里面判断如果超直接返回}// 如果在这个位置判断是否为空会导致current.value不存在报错if (current.value == x) {return true;}return false; } public node insert(int x)// 插入 t是root的引用 {node current = root;if (root == null) {root = new node(x);return root;}while (current != null) {if (x < current.value) {if (current.left == null) {return current.left = new node(x);}else current = current.left;}else if (x > current.value) {if (current.right == null) {return current.right = new node(x);}else current = current.right;}}return current;//其中用不到 } public node remove(int x, node t)// 删除节点 {if (t == null) {return null;}if (x < t.value) {t.left = remove(x, t.left);} else if (x > t.value) {t.right = remove(x, t.right);} else if (t.left != null && t.right != null)// 左右节点均不空{t.value = findmin(t.right).value;// 找到右侧最小值替代t.right = remove(t.value, t.right);} else // 左右单空或者左右都空{if (t.left == null && t.right == null) {t = null;} else if (t.right != null) {t = t.right;} else if (t.left != null) {t = t.left;}return t;}return t; }}结语
- 这里我们优先学习了树,二叉树,以及二叉搜素树的基本构造 。对于二叉搜素树插入查找比较容易理解但是实现的时候要注意函数对参数的引用等等 。需要认真考虑 。
- 而偏有难度的是二叉树的删除,利用一个递归的思想,要找到特殊情况和普通情况,递归一定程度也是问题的转化(转成自己相同问题,作用域减小)需要思考 。
- 下面还会介绍二叉搜素树的三序遍历(递归和非递归).和层序遍历 。需要的朋友请持续关注 。另外,笔者数据结构专栏欢迎查房 。!
- 如果对后端、爬虫、数据结构算法等感性趣欢迎关注我的个人公众号交流:bigsai 。回复爬虫,数据结构等有精美资料一份 。

文章插图
推荐阅读
- Linux查找文件内容和字符串之grep与egrep的区别
- 机构内部数据库教程,Join算法
- 阐述茶叶的储存要求与方法
- 使用python构建递归算法,实现查找电脑中的所有文件
- 邱淑贞的性感与演技 红灯区邱淑贞
- 吕布董卓貂蝉这三个人是什么关系 董卓吕布与貂蝉的关系
- 微信小程序分销商城有什么功能与特点
- 手机信号的强弱与什么有关?手机壳会影响手机信号吗?长知识了
- 科学冲泡红茶好处多
- 绿茶与乌龙茶的差异
