数组与链表区别:
二叉排序树的创建和遍历 代码实现
package tree.binarysorttree;public class BinarySortTreeDemo {public static void main(String []args){int [] arr = {7,3,10,12,5,1,9};BinarySortTree binarySortTree = new BinarySortTree();//循环添加节点for (int i = 0; i < arr.length; i++) {binarySortTree.add(new Node(arr[i]));}//中序遍历binarySortTree.infixOrder();}
}//创建二叉排序树
class BinarySortTree{private Node root;//添加节点的方法public void add(Node node){if (root == null){root = node;}else {root.add(node);}}//中序遍历public void infixOrder(){if (root != null){root.infixOrder();}else {System.out.println("二叉排序树为空,无法遍历");}}
}//创建节点
class Node{int value;Node left;Node right;public Node(int value) {this.value = value;}//添加节点的方法//递归的形式添加节点,满足二叉排序树public void add(Node node){if(node == null){return;}if (node.value < this.value){//当前节点的左子节点为空直接挂上if (this.left == null){this.left = node;}else {//不为空递归向下this.left.add(node);}}else { //添加的节点的值大于等于当前节点的值,右节点为空,挂到右节点if (this.right == null){this.right = node;}else{//不为空就挂上this.right.add(node);}}}//中序遍历public void infixOrder(){if (this.left != null){this.left.infixOrder();}System.out.println(this);if (this.right != null){this.right.infixOrder();}}@Overridepublic String toString() {return "Node{" +"value=" + value +'}';}
}
删除:
编写查找节点的方法
//查找要删除的节点public Node search(int value){if (root == null){return null;}else {return root.search(value);}}//查找父节点public Node searchParent(int value){if (root == null){return null;}else{return root.searchParent(value);}}
删除第三种需要用到的,查找以target.right为根节点节点的树的最小值并删除,然后返回value
//编写一个方法,查找以传入节点为根节点的二叉排序树中的最小节点的值,并删除此节点public int delRightTreeMin(Node node){Node target = node;//循环查找左节点,就会找到最小值while(target.left != null){target = target.left;}//删除最小节点delNode(target.value);return target.value;}
删除节点方法
public void delNode(int value){if (root == null){return;}else {//1.找到要删除的节点Node targetNode = search(value);if (targetNode == null){return;}//如果二叉排序树只有一个节点,并且删除的此节点if (root.left == null && root.right == null){root = null;return;}//找到父节点Node parent = searchParent(value);//如果删除的节点是叶子节点if (targetNode.left == null && targetNode.right == null){//第一种,删除叶子if (targetNode.value < parent.value){ //比较大小判断删除的左叶子还是右叶子parent.left = null;}else {parent.right = null;}}else if (targetNode.left != null && targetNode.right != null){//删除有两棵子树的(因为第二种判断比较麻烦,所以直接写在else里)//从target的右子树找到最小节点,//用一个临时变量存储这个最小节点的value//删除最小节点int min = delRightTreeMin(targetNode.right);//把最小节点的值赋给targetNodetargetNode.value = min;}else {//第二种,只有一棵子树//如果要删除的节点有左子节点if (targetNode.left != null){//如果只有根节点和一个叶子节点,要删除根节点,那就要判断根节点的父节点是否为空if (parent != null) {//如果targetNode是parent的左子节点if (parent.left.value == value) {parent.left = targetNode.left;} else {//targetNode是parent的右子节点parent.right = targetNode.left;}}else {root = targetNode.left;}}else {//要删除的节点有右子节点if (parent != null) {if (parent.left.value == value) {//如果targetNode是parent的左子节点parent.left = targetNode.right;} else {//targetNode是parent的右子节点parent.right = targetNode.right;}}else {root = targetNode.right;}}}}}
完整代码
package tree.binarysorttree;public class BinarySortTreeDemo {public static void main(String []args){int [] arr = {7,3,10,12,5,1,9,2};BinarySortTree binarySortTree = new BinarySortTree();//循环添加节点for (int i = 0; i < arr.length; i++) {binarySortTree.add(new Node(arr[i]));}//中序遍历binarySortTree.infixOrder();//测试删除binarySortTree.delNode(2);binarySortTree.delNode(5);binarySortTree.delNode(9);binarySortTree.delNode(12);binarySortTree.delNode(7);binarySortTree.delNode(3);binarySortTree.delNode(10);System.out.println();binarySortTree.infixOrder();}
}//创建二叉排序树
class BinarySortTree{private Node root;public Node getRoot() {return root;}//查找要删除的节点public Node search(int value){if (root == null){return null;}else {return root.search(value);}}//查找父节点public Node searchParent(int value){if (root == null){return null;}else{return root.searchParent(value);}}//编写一个方法,查找以传入节点为根节点的二叉排序树中的最小节点的值,并删除此节点public int delRightTreeMin(Node node){Node target = node;//循环查找左节点,就会找到最小值while(target.left != null){target = target.left;}//删除最小节点delNode(target.value);return target.value;}//删除节点public void delNode(int value){if (root == null){return;}else {//1.找到要删除的节点Node targetNode = search(value);if (targetNode == null){return;}//如果二叉排序树只有一个节点,并且删除的此节点if (root.left == null && root.right == null){root = null;return;}//找到父节点Node parent = searchParent(value);//如果删除的节点是叶子节点if (targetNode.left == null && targetNode.right == null){//第一种,删除叶子if (targetNode.value < parent.value){ //比较大小判断删除的左叶子还是右叶子parent.left = null;}else {parent.right = null;}}else if (targetNode.left != null && targetNode.right != null){//删除有两棵子树的(因为第二种判断比较麻烦,所以直接写在else里)//从target的右子树找到最小节点,//用一个临时变量存储这个最小节点的value//删除最小节点int min = delRightTreeMin(targetNode.right);//把最小节点的值赋给targetNodetargetNode.value = min;}else {//第二种,只有一棵子树//如果要删除的节点有左子节点if (targetNode.left != null){//如果只有根节点和一个叶子节点,要删除根节点,那就要判断根节点的父节点是否为空if (parent != null) {//如果targetNode是parent的左子节点if (parent.left.value == value) {parent.left = targetNode.left;} else {//targetNode是parent的右子节点parent.right = targetNode.left;}}else {root = targetNode.left;}}else {//要删除的节点有右子节点if (parent != null) {if (parent.left.value == value) {//如果targetNode是parent的左子节点parent.left = targetNode.right;} else {//targetNode是parent的右子节点parent.right = targetNode.right;}}else {root = targetNode.right;}}}}}//添加节点的方法public void add(Node node){if (root == null){root = node;}else {root.add(node);}}//中序遍历public void infixOrder(){if (root != null){root.infixOrder();}else {System.out.println("二叉排序树为空,无法遍历");}}
}//创建节点
class Node{int value;Node left;Node right;public Node(int value) {this.value = value;}//查找要删除的节点public Node search(int value){if (value == this.value){//找到return this;}else if(value < this.value){//应该向左继续查找//如果左子节点为空就不能找了,说明不存在if (this.left == null) {return null;}return this.left.search(value);}else {if (this.right == null){return null;}return this.right.search(value);}}//查找要删除节点的父节点public Node searchParent(int value){if ((this.left != null && this.left.value == value)||(this.right != null && this.right.value == value)){return this;}else {//如果查找的值小于当前节点的值,并且当前节点的左子节点不为空if (value < this.value && this.left != null){return this.left.searchParent(value);//像左子树递归查找}else if (value >= this.value && this.right != null){return this.right.searchParent(value);//像右子树递归查找}else {return null; //没有父节点}}}//添加节点的方法//递归的形式添加节点,满足二叉排序树public void add(Node node){if(node == null){return;}if (node.value < this.value){//当前节点的左子节点为空直接挂上if (this.left == null){this.left = node;}else {//不为空递归向下this.left.add(node);}}else { //添加的节点的值大于等于当前节点的值,右节点为空,挂到右节点if (this.right == null){this.right = node;}else{//不为空就挂上this.right.add(node);}}}//中序遍历public void infixOrder(){if (this.left != null){this.left.infixOrder();}System.out.println(this);if (this.right != null){this.right.infixOrder();}}@Overridepublic String toString() {return "Node{" +"value=" + value +'}';}
}