Java二叉搜索树(Binary Search Tree)实现

树集合了数组(查找速度快)和链表(插入、删除速度快)的优点

二叉树是一种特殊的树,即:树中的每个节点最多只能有两个子节点

二叉搜索树是一种特殊的二叉树,即:节点的左子节点的值都小于这个节点的值,节点的右子节点的值都大于等于这个节点的值

节点类:

[java]  view plain  copy
  1. public class Node {  
  2.     public int id;  
  3.     public String name;  
  4.     public Node leftChild;  
  5.     public Node rightChild;  
  6.   
  7.     public Node(int id, String name) {  
  8.         this.id = id;  
  9.         this.name = name;  
  10.     }  
  11. }  

实现类(如果树中允许存在重复数据,处理起来比较麻烦,故实现中不允许树中存在重复数据,即节点的右子节点的值必须大于节点的值):

搜索二叉树有一个特点,即如果使用中序遍历遍历搜索二叉树,将得到包含搜索二叉树中所有节点值的升序排序结果

[java]  view plain  copy
  1. public class BinarySearchTree {  
  2.     public Node root;  
  3.       
  4.     public Node find(int key){  
  5.         if(root == null){  
  6.             System.out.println("The tree is empty!");  
  7.             return null;  
  8.         }  
  9.         Node current = root;  
  10.         while(current.id != key){  
  11.             if(key > current.id)  
  12.                 current = current.rightChild;  
  13.             else  
  14.                 current = current.leftChild;  
  15.             if(current == null)  
  16.                 return null;  
  17.         }  
  18.         return current;  
  19.     }  
  20.       
  21.     public boolean insert(Node node){  
  22.         if(root == null){  
  23.             root = node;  
  24.             return true;  
  25.         }  
  26.         //树中不允许插入重复的数据项  
  27.         if(this.find(node.id) != null){  
  28.             System.out.println("Node with id '" +  
  29.                     node.id + "' has already existed!");  
  30.             return false;  
  31.         }  
  32.         Node current = root;  
  33.         while(current != null){  
  34.             if(node.id > current.id){  
  35.                 if(current.rightChild == null){  
  36.                     current.rightChild = node;  
  37.                     return true;  
  38.                 }  
  39.                 current = current.rightChild;  
  40.             }else{  
  41.                 if(current.leftChild == null){  
  42.                     current.leftChild = node;  
  43.                     return true;  
  44.                 }  
  45.                 current = current.leftChild;  
  46.             }  
  47.         }  
  48.         return false;  
  49.     }  
  50.       
  51.     //前序遍历  
  52.     public void preorder_iterator(Node node){  
  53.         System.out.print(node.id + " ");  
  54.         if(node.leftChild != null)  
  55.             this.preorder_iterator(node.leftChild);  
  56.         if(node.rightChild != null)  
  57.             this.preorder_iterator(node.rightChild);  
  58.     }  
  59.       
  60.     //中序遍历  
  61.     //中序遍历二叉搜索树将会得到包含二叉搜索树  
  62.     //所有数据项的有序数列  
  63.     public void inorder_iterator(Node node){  
  64.         if(node.leftChild != null)  
  65.             this.inorder_iterator(node.leftChild);  
  66.         System.out.print(node.id + " ");  
  67.         if(node.rightChild != null)  
  68.             this.inorder_iterator(node.rightChild);  
  69.     }  
  70.       
  71.     //后序遍历  
  72.     public void postorder_iterator(Node node){  
  73.         if(node.leftChild != null)  
  74.             this.postorder_iterator(node.leftChild);  
  75.         if(node.rightChild != null)   
  76.             this.postorder_iterator(node.rightChild);  
  77.         System.out.print(node.id + " ");  
  78.     }  
  79.       
  80.     //获取树(子树)中的最小节点  
  81.     public Node getMinNode(Node node){  
  82.         if(this.find(node.id) == null){  
  83.             System.out.println("Node dosen't exist!");  
  84.             return null;  
  85.         }  
  86.         if(node.leftChild == null)  
  87.             return node;  
  88.         Node current = node.leftChild;  
  89.         while(current.leftChild != null)  
  90.             current = current.leftChild;  
  91.         return current;  
  92.     }  
  93.       
  94.     //获取树(子树)中的最大节点  
  95.     public Node getMaxNode(Node node){  
  96.         if(this.find(node.id) == null){  
  97.             System.out.println("Node dosen't exist!");  
  98.             return null;  
  99.         }  
  100.         if(node.rightChild == null)  
  101.             return node;  
  102.         Node current = node.rightChild;  
  103.         while(current.rightChild != null)  
  104.             current = current.rightChild;  
  105.         return current;  
  106.     }  
  107.       
  108.     //删除节点需要分3种情况进行讨论  
  109.     public boolean delete(int key){  
  110.         if(root == null){  
  111.             System.out.println("The tree is empty!");  
  112.             return false;  
  113.         }  
  114.         Node targetParent = root;  
  115.         Node target = root;  
  116.         boolean isLeftChild = true;  
  117.         while(target.id != key){  
  118.             if(key > target.id){  
  119.                 targetParent = target;  
  120.                 target = target.rightChild;   
  121.                 isLeftChild = false;  
  122.             }else{  
  123.                 targetParent = target;  
  124.                 target = target.leftChild;  
  125.                 isLeftChild = true;  
  126.             }  
  127.             if(target == null)  
  128.                 break;  
  129.         }  
  130.         if(target == null){  
  131.             System.out.println("Node dosen't exist!"   
  132.                     + "Can not delete.");  
  133.             return false;  
  134.         }  
  135.         //被删除节点为叶子节点  
  136.         if(target.leftChild == null &&  
  137.                 target.rightChild == null){  
  138.             if(target.id == root.id){  
  139.                 root = null;  
  140.                 return true;  
  141.             }  
  142.             if(isLeftChild)  
  143.                 targetParent.leftChild = null;  
  144.             else  
  145.                 targetParent.rightChild = null;  
  146.         }  
  147.         //被删除节点有1个子节点  
  148.         //被删除节点只有右子节点  
  149.         else if(target.leftChild == null &&   
  150.                 target.rightChild != null){  
  151.             if(target.id == root.id){  
  152.                 root = root.rightChild;  
  153.                 return true;  
  154.             }  
  155.             if(isLeftChild)  
  156.                 targetParent.leftChild = target.rightChild;  
  157.             else  
  158.                 targetParent.rightChild = target.rightChild;  
  159.         }  
  160.         //被删除节点只有左子节点  
  161.         else if(target.leftChild != null &&   
  162.                 target.rightChild == null){  
  163.             if(target.id == root.id){  
  164.                 root = root.leftChild;  
  165.                 return true;  
  166.             }  
  167.             if(isLeftChild)  
  168.                 targetParent.leftChild = target.leftChild;  
  169.             else  
  170.                 targetParent.rightChild = target.leftChild;  
  171.         }  
  172.         //被删除节点有左右子节点,先找到后续节点,将,然后将后续节点插入至待删除节点的位置  
  173.         else{  
  174.             Node followingNode = this.getFollowingNode(target);  
  175.             if(target.id == root.id)  
  176.                 root = followingNode;  
  177.             else if(isLeftChild)  
  178.                 targetParent.leftChild = followingNode;  
  179.             else  
  180.                 targetParent.rightChild = followingNode;  
  181.             followingNode.leftChild = target.leftChild;  
  182.             followingNode.rightChild = target.rightChild;  
  183.         }  
  184.         return true;  
  185.     }  
  186.       
  187.     //获取被删除节点的后续节点  
  188.     private Node getFollowingNode(Node node2Del){  
  189.         Node nodeParent = node2Del;  
  190.         //只有被删除节点有左右子节点时,才会调用该方法  
  191.         //这里直接调用rightChild是没有问题的  
  192.         Node node = node2Del.rightChild;  
  193.         while(node.leftChild != null){  
  194.             nodeParent = node;  
  195.             node = node.leftChild;  
  196.         }  
  197.         if(node.id != node2Del.rightChild.id)  
  198.             nodeParent.leftChild = node.rightChild;  
  199.         else  
  200.             nodeParent.rightChild = node.rightChild;  
  201.         return node;  
  202.     }  
  203.       
  204.     public static void main(String[] args) {  
  205.         //插入  
  206.         BinarySearchTree bst = new BinarySearchTree();  
  207.         Node n1 = new Node(20"root");  
  208.         Node n2 = new Node(10"left");  
  209.         Node n3 = new Node(30"right");  
  210.         bst.insert(n1);  
  211.         bst.insert(n2);  
  212.         bst.insert(n3);  
  213.         //遍历  
  214.         bst.preorder_iterator(bst.root);  
  215.         System.out.println();  
  216.         bst.inorder_iterator(bst.root);  
  217.         System.out.println();  
  218.         bst.postorder_iterator(bst.root);  
  219.         System.out.println();  
  220.         //删除  
  221.         Node n4 = new Node(5"");  
  222.         Node n5 = new Node(15"");  
  223.         Node n6 = new Node(40"");  
  224.         Node n7 = new Node(35"");  
  225.         Node n8 = new Node(45"");  
  226.         bst.insert(n4);  
  227.         bst.insert(n5);  
  228.         bst.insert(n6);  
  229.         bst.insert(n7);  
  230.         bst.insert(n8);  
  231.         bst.inorder_iterator(bst.root);  
  232.         System.out.println();  
  233.         bst.delete(20);  
  234.         bst.inorder_iterator(bst.root);  
  235.         System.out.println();  
  236.     }  
  237. }  

执行结果:

[plain]  view plain  copy
  1. 20 10 30   
  2. 10 20 30   
  3. 10 30 20   
  4. 5 10 15 20 30 35 40 45   
  5. 5 10 15 30 35 40 45   

二叉搜索树的效率:

树的大部分操作需要从上至下一层层的查找树的节点,对于一棵满树,大约有一半的节点处于最底层(最底层节点数 = 其它层节点数的和 + 1),故节点操作大约有一半需要找到最底层节点,大约有四分之一的节点处于倒数第二层,故节点操作大约有四分之一需要找到倒数第二层节点,依此类推

查找过程中,需要访问每一层的节点,故只要知道了查找的层数,就能知道操作所需的时间,如果节点总数为N,层数为L,L=log2(N+1)

如果为查找操作或删除操作,被操作的节点可能是是树的任意节点,故查找操作或删除操作的时间复杂度为:1/21*log2(N+1) + 1/22*log2(N/2+1) + ... + 1/2N*1

如果为插入操作,由于每次都在树的最低层插入新的节点,故插入操作的时间复杂度为:log2(N+1)

总的来说可以认为二叉搜索树操作的时间复杂度为为O(logN)

如果树不是一棵满树,则判断起来比较复杂,但是如果层数相同,对满树的操作肯定比对不满树的操作更耗时


对于一个含有10000个数据项的有序链表,查找操作平均需要比较5000次,对于一个含有10000个节点的二叉搜索树,查找操作大约需要13次

对于一个含有10000个数据项的有序数组,插入操作平均需要移动5000次(对于比较次数,使用不同的算法比较次数并不相同),对于一个含有10000个节点的二叉搜索树,插入操作只需大约13次比较就可找到待插入节点的插入位置,并且由于该位置总是处于二叉搜索树的最底层,并不需要移动其它的节点

可以看出,二叉搜索树集合了有序链表插入删除效率高和有序数组查询效率高的优点

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值