二叉搜索树的查找、添加和删除操作

来源:互联网 发布:mac 储存位置 编辑:程序博客网 时间:2024/06/05 15:11
class Node{int data;Node lChild;Node rChild;public Node(int data){this.data=data;this.lChild=null;this.rChild=null;}}public class Tree {public static void main(String[] args) {Node n1=new Node(45);Node n2=new Node(12);Node n3=new Node(53);        Node n4=new Node(3);        Node n5=new Node(37);        Node n6=new Node(100);        Node n7=new Node(24);        Node n8=new Node(61);        Node n9=new Node(90);        Node n10=new Node(78);                Node root=n1;        root.lChild=n2;        root.rChild=n3;        n2.lChild=n4;        n2.rChild=n5;        n3.rChild=n6;        n5.lChild=n7;        n6.lChild=n8;        n8.rChild=n9;        n9.lChild=n10;                System.out.println(delete(root,n2.data));}/** * 在二叉树中查找节点 * @param root 当前搜索节点 * @param num 查找的值 * @param f  当前搜索节点父节点 * @return 若找到节点,则返回该节点,否则返回空 */public static Node search(Node root,int num){if(root==null){                     return null;   }if(root.data>num){return search(root.lChild,num);}else if(root.data<num){return search(root.rChild,num);}else{return root;}}/** * 得到某一数值的节点的父节点 * @param root 根节点 * @param num  特定的数值 * @return  返回特定数值的根节点 */public static Node getFather(Node root,int num){if(root==null){return null;}if(root.data==num){return root;}Node temp=root;Node father = null;while(temp!=null){ father=temp;if(temp.data<num){temp=temp.rChild;}else{temp=temp.lChild;}if(temp==null||temp.data==num){  //找到特定节点或者特定节点不存在break;}}return father;}/** * 在已有的二叉树中插入新节点 * @param root 根节点 * @param num 待插入的数据 * @return root为空或此数据已存在返回false */public static boolean insert(Node root,int num){if(root!=null){Node p=search(root,num);if(p==null){Node node=new Node(num);Node father=getFather(root,num);if(father.data>num)father.lChild=node;elsefather.rChild=node;return true;}else{  //树中已有此数据return false;}}return false;}/** * 删除节点: * (1)删除的节点为叶子节点:直接删除节点,将其父节点的引用置空 * (2)删除的节点有左子树或者有右子树:直接将其父节点的引用指向其子树 * (3)删除的节点有左右子树:找到其右子树上的最小节点,将其父节点的引用指向最小节点,连接 * 其左右子树 * @param root 根节点 * @param num 要删除的节点的数值 * @return 返回删除操作成功与否 */public static boolean delete(Node root,int num){Node father;Node next;Node f;Node p=search(root,num);boolean left=false;if(p==null){ //此二叉树中不存在要删除的节点return false;}if(root.data==num&&root.lChild==null&&root.rChild==null){ //如果要删除的是根节点root.data=0;  //且根节点是叶子节点,则将根节点数据置0return true;}father=getFather(root,num);if(father.data>p.data){left=true;}if(p.lChild==null&&p.rChild==null){ //要删除的节点为叶子节点if(left){father.lChild=null;}else{father.rChild=null;}}else if(p.lChild!=null&&p.rChild==null){ //左孩子不为空,右孩子为空if(left){father.lChild=p.lChild;}else{father.rChild=p.lChild;}}else if(p.lChild==null&&p.rChild!=null){ //左孩子为空,右孩子不为空if(left){father.lChild=p.rChild;}else{father.rChild=p.rChild;}}else{ //左右孩子均不为空f=p;next=p.rChild;while(next.lChild!=null){ //找到要删除的节点右子树上的最小节点,并记录其父节点f=next;next=next.lChild;}if(p==root){ //如果删除的是根节点root.data=next.data;if(f!=root){root.rChild=f.rChild;f.lChild=null;}else{root.rChild=next.rChild;}return true;}if(f==p){ //如果要删除节点的右子树上没有左子树f.rChild=null;next.rChild=null;}else{f.lChild=null;next.rChild=p.rChild; //连接右子树}next.lChild=p.lChild; //连接左子树if(left){father.lChild=next;}else{father.rChild=next;}}return true;}}

0 0
原创粉丝点击