二叉搜索树之Java实现

注:此文非笔者所有。

转载自  http://blog.csdn.net/kimylrong/article/details/21872909

因为没有发现转载功能,所以我只是复制粘贴过来免得以后再找。


什么是二叉搜索树

二叉搜索树(Binary Search Tree),是最基础,且相对简单的一种数据结构,支持Insert,Delete,Search,Min,Max,Successor,Predecessor等操作。最大的特点是每一个节点有不超过两个子节点,并且左子节点小于或者等于父节点,而右节点大于或者等于父节点。说它基础,是因为很多其它树形数据结构以它为原型而扩展,比如红黑树,B树。

具体实现

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class BinaryTree<T extends Comparable<T>> {  
  2.     private Node<T> root;  
  3.   
  4.     public void insert(T element) {  
  5.         if (element == null) {  
  6.             throw new IllegalArgumentException("element can not be null");  
  7.         }  
  8.   
  9.         if (root == null) {  
  10.             root = new Node<T>(null, element);  
  11.         } else {  
  12.             Node<T> node = root;  
  13.             while (true) {  
  14.                 if (element.compareTo(node.value) <= 0) {  
  15.                     if (node.left == null) {  
  16.                         Node<T> newNode = new Node<T>(node, element);  
  17.                         node.left = newNode;  
  18.                         break;  
  19.                     } else {  
  20.                         node = node.left;  
  21.                     }  
  22.                 } else {  
  23.                     if (node.right == null) {  
  24.                         Node<T> newNode = new Node<T>(node, element);  
  25.                         node.right = newNode;  
  26.                         break;  
  27.                     } else {  
  28.                         node = node.right;  
  29.                     }  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     private int childCount(Node<T> node) {  
  36.         if (node == null) {  
  37.             throw new IllegalArgumentException("node can not be null");  
  38.         }  
  39.   
  40.         int count = 0;  
  41.   
  42.         if (node.left != null) {  
  43.             count++;  
  44.         }  
  45.   
  46.         if (node.right != null) {  
  47.             count++;  
  48.         }  
  49.   
  50.         return count;  
  51.     }  
  52.   
  53.     public void delete(Node<T> node) {  
  54.         if (node == null) {  
  55.             throw new IllegalArgumentException("node can not be null");  
  56.         }  
  57.   
  58.         int childCount = childCount(node);  
  59.         Node<T> parentNode = node.parent;  
  60.   
  61.         if (childCount == 0) {  
  62.             if (parentNode == null) {  
  63.                 // node is root  
  64.                 root = null;  
  65.             } else {  
  66.                 if (node == parentNode.left) {  
  67.                     parentNode.left = null;  
  68.                 } else {  
  69.                     parentNode.right = null;  
  70.                 }  
  71.             }  
  72.         } else if (childCount == 1) {  
  73.             if (parentNode == null) {  
  74.                 // node is root, set child of node to be new root  
  75.                 if (node.left != null) {  
  76.                     root = node.left;  
  77.                     node.left.parent = null;  
  78.                 } else {  
  79.                     root = node.right;  
  80.                     node.right.parent = null;  
  81.                 }  
  82.             } else {  
  83.                 if (node == parentNode.left) {  
  84.                     if (node.left != null) {  
  85.                         parentNode.left = node.left;  
  86.                         node.left.parent = parentNode;  
  87.                     } else {  
  88.                         parentNode.left = node.right;  
  89.                         node.right.parent = parentNode;  
  90.                     }  
  91.                 } else {  
  92.                     if (node.left != null) {  
  93.                         parentNode.right = node.left;  
  94.                         node.left.parent = parentNode;  
  95.                     } else {  
  96.                         parentNode.right = node.right;  
  97.                         node.right.parent = parentNode;  
  98.                     }  
  99.                 }  
  100.             }  
  101.         } else {  
  102.             // successor has no left child  
  103.             Node<T> successor = min(node);  
  104.   
  105.             if (successor != node.right) {  
  106.                 transplant(successor, successor.right);  
  107.   
  108.                 successor.right = node.right;  
  109.                 node.right.parent = successor;  
  110.             }  
  111.   
  112.             transplant(node, successor);  
  113.   
  114.             successor.left = node.left;  
  115.             node.left.parent = successor;  
  116.         }  
  117.     }  
  118.   
  119.     private void transplant(Node<T> u, Node<T> v) {  
  120.         if (u == null) {  
  121.             throw new IllegalArgumentException("node can not be null");  
  122.         }  
  123.   
  124.         if (u.parent == null) {  
  125.             root = v;  
  126.         } else if (u == u.parent.left) {  
  127.             u.parent.left = v;  
  128.         } else {  
  129.             u.parent.right = v;  
  130.         }  
  131.   
  132.         if (v != null) {  
  133.             v.parent = u.parent;  
  134.         }  
  135.     }  
  136.   
  137.     public Node<T> search(T element) {  
  138.         if (element == null) {  
  139.             throw new IllegalArgumentException("element can not be null");  
  140.         }  
  141.   
  142.         Node<T> node = root;  
  143.         while (node != null) {  
  144.             if (node.value.equals(element)) {  
  145.                 return node;  
  146.             } else if (element.compareTo(node.value) < 0) {  
  147.                 node = node.left;  
  148.             } else {  
  149.                 node = node.right;  
  150.             }  
  151.         }  
  152.   
  153.         return null;  
  154.     }  
  155.   
  156.     public Node<T> min(Node<T> rootNode) {  
  157.         if (rootNode == null) {  
  158.             throw new IllegalArgumentException("node can not be null");  
  159.         }  
  160.   
  161.         Node<T> node = rootNode;  
  162.         while (node.left != null) {  
  163.             node = node.left;  
  164.         }  
  165.   
  166.         return node;  
  167.     }  
  168.   
  169.     public Node<T> min() {  
  170.         if (root != null) {  
  171.             return min(root);  
  172.         } else {  
  173.             return null;  
  174.         }  
  175.     }  
  176.   
  177.     public Node<T> max(Node<T> rootNode) {  
  178.         if (rootNode == null) {  
  179.             throw new IllegalArgumentException("node can not be null");  
  180.         }  
  181.   
  182.         Node<T> node = rootNode;  
  183.         while (node.right != null) {  
  184.             node = node.right;  
  185.         }  
  186.   
  187.         return node;  
  188.     }  
  189.   
  190.     public Node<T> max() {  
  191.         if (root != null) {  
  192.             return max(root);  
  193.         } else {  
  194.             return null;  
  195.         }  
  196.     }  
  197.   
  198.     public Node<T> successor(Node<T> node) {  
  199.         if (node == null) {  
  200.             throw new IllegalArgumentException("node can not be null");  
  201.         }  
  202.   
  203.         if (node.right != null) {  
  204.             return min(node.right);  
  205.         }  
  206.   
  207.         Node<T> processNode = node;  
  208.         Node<T> parent = processNode.parent;  
  209.         while (parent != null && processNode == parent.right) {  
  210.             processNode = parent;  
  211.             parent = processNode.parent;  
  212.         }  
  213.   
  214.         return parent;  
  215.     }  
  216.   
  217.     public Node<T> predecesssor(Node<T> node) {  
  218.         if (node == null) {  
  219.             throw new IllegalArgumentException("node can not be null");  
  220.         }  
  221.   
  222.         if (node.left != null) {  
  223.             return max(node.left);  
  224.         }  
  225.   
  226.         Node<T> processNode = node;  
  227.         Node<T> parent = processNode.parent;  
  228.         while (parent != null && processNode == parent.left) {  
  229.             processNode = parent;  
  230.             parent = processNode.parent;  
  231.         }  
  232.   
  233.         return parent;  
  234.     }  
  235.   
  236.     public void print() {  
  237.         print(root);  
  238.     }  
  239.   
  240.     public void print(Node<T> node) {  
  241.         if (node == null) {  
  242.             return;  
  243.         }  
  244.   
  245.         print(node.left);  
  246.         System.out.print("  " + node.value.toString() + "  ");  
  247.         print(node.right);  
  248.     }  
  249.   
  250.     public static class Node<T extends Comparable<T>> {  
  251.         private Node<T> parent;  
  252.         private Node<T> left;  
  253.         private Node<T> right;  
  254.   
  255.         private T value;  
  256.   
  257.         public Node(Node<T> parent, T value) {  
  258.             this.parent = parent;  
  259.             this.value = value;  
  260.         }  
  261.   
  262.         public Node<T> getParent() {  
  263.             return parent;  
  264.         }  
  265.   
  266.         public void setParent(Node<T> parent) {  
  267.             this.parent = parent;  
  268.         }  
  269.   
  270.         public Node<T> getLeft() {  
  271.             return left;  
  272.         }  
  273.   
  274.         public void setLeft(Node<T> left) {  
  275.             this.left = left;  
  276.         }  
  277.   
  278.         public Node<T> getRight() {  
  279.             return right;  
  280.         }  
  281.   
  282.         public void setRight(Node<T> right) {  
  283.             this.right = right;  
  284.         }  
  285.   
  286.         public T getValue() {  
  287.             return value;  
  288.         }  
  289.   
  290.         public void setValue(T value) {  
  291.             this.value = value;  
  292.         }  
  293.     }  
  294.   
  295.     public static void main(String[] args) {  
  296.         BinaryTree<String> tree = new BinaryTree<String>();  
  297.   
  298.         tree.insert("Hello");  
  299.         tree.insert("World");  
  300.         tree.insert("Money");  
  301.   
  302.         tree.print();  
  303.         System.out.println();  
  304.   
  305.         Node<String> moneyNode = tree.search("Money");  
  306.         tree.print(moneyNode);  
  307.         System.out.println();  
  308.   
  309.         tree.insert("Like");  
  310.         tree.print(moneyNode);  
  311.         System.out.println();  
  312.   
  313.         tree.delete(moneyNode);  
  314.         tree.print();  
  315.         System.out.println();  
  316.     }  
  317. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值