二叉查找树的实现(BST)(转载自http://blog.csdn.net/collonn/article/details/18732079)

来源:互联网 发布:如何自学java 编辑:程序博客网 时间:2024/05/22 12:39

直接上图吧,代码中有树形打印方法,图中有详细说明




代码实现如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.collonn.algorithm.tree;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.LinkedList;  
  5. import java.util.Map;  
  6. import java.util.Queue;  
  7.   
  8. /** 
  9.  * 二叉搜索树节点定义 
  10.  */  
  11. class Node {  
  12.     // 节点值  
  13.     public int value;  
  14.     // 节点左孩子  
  15.     public Node left;  
  16.     // 节点右孩子  
  17.     public Node right;  
  18.     // 节点的双亲  
  19.     public Node parent;  
  20.   
  21.     public Node() {  
  22.           
  23.     }  
  24.   
  25.     // 是否有左孩子  
  26.     public boolean hasLeft() {  
  27.         return this.left == null ? false : true;  
  28.     }  
  29.   
  30.     // 是否有右孩子  
  31.     public boolean hasRight() {  
  32.         return this.right == null ? true : false;  
  33.     }  
  34.   
  35.     // 是否只有一个孩子  
  36.     public boolean hasOneSub() {  
  37.         return (this.left == null && this.right != null) || (this.left != null && this.right == null) ? true : false;  
  38.     }  
  39.   
  40.     // 是否有两个孩子  
  41.     public boolean hasTwoSub() {  
  42.         return (this.left != null && this.right != null ? true : false);  
  43.     }  
  44.   
  45.     // 是否没有孩子  
  46.     public boolean hasNonSub() {  
  47.         return (this.left == null && this.right == null) ? true : false;  
  48.     }  
  49.   
  50.     // 是否叶子节点  
  51.     public boolean isLeaf() {  
  52.         return this.hasNonSub();  
  53.     }  
  54.   
  55.     // 是否是ROOT节点  
  56.     public boolean isRoot() {  
  57.         return (this.parent == null) ? true : false;  
  58.     }  
  59.   
  60.     // 是否是双亲的左孩子  
  61.     public boolean isLeft() {  
  62.         boolean flag = false;  
  63.         if (this.parent != null && (this == this.parent.left)) {  
  64.             flag = true;  
  65.         }  
  66.         return flag;  
  67.     }  
  68.   
  69.     // 是否是双亲的右孩子  
  70.     public boolean isRight() {  
  71.         boolean flag = false;  
  72.         if (this.parent != null && (this == this.parent.right)) {  
  73.             flag = true;  
  74.         }  
  75.         return flag;  
  76.     }  
  77.   
  78.     // 该节点只能有一个孩子,返回这个孩子  
  79.     public Node getOneSub() {  
  80.         if (this.left != null && this.right != null) {  
  81.             throw new RuntimeException("该节点只能有一个孩子。");  
  82.         }  
  83.         return this.left == null ? this.right : this.left;  
  84.     }  
  85. }  
  86.   
  87. /** 
  88.  * 二叉查找树(BST树)<br/> 
  89.  * 比节点小的元素放在节点的左子树中,比节点大的元素放在节点的右子树中<br/> 
  90.  */  
  91. public class BST {  
  92.     // 二叉根节点  
  93.     public Node root;  
  94.   
  95.     // 向二叉树插入新值  
  96.     public void insert(int nv) {  
  97.         System.out.print("\n----------");  
  98.         if (this.root == null) {  
  99.             this.root = new Node();  
  100.             this.root.value = nv;  
  101.             this.root.left = null;  
  102.             this.root.right = null;  
  103.             this.root.parent = null;  
  104.             System.out.printf("\n插入根据节点:" + nv);  
  105.             return;  
  106.         }  
  107.   
  108.         System.out.print("\nvisit:");  
  109.         Node node = this.findPlace(nv);  
  110.         if (node != null) {  
  111.             if (nv < node.value) {  
  112.                 System.out.printf("\n在%d的左子数中插入%d:", node.value, nv);  
  113.                 node.left = new Node();  
  114.                 node.left.value = nv;  
  115.                 node.left.left = null;  
  116.                 node.left.right = null;  
  117.                 node.left.parent = node;  
  118.             } else if (nv > node.value) {  
  119.                 System.out.printf("\n在%d的右子数中插入%d:", node.value, nv);  
  120.                 node.right = new Node();  
  121.                 node.right.value = nv;  
  122.                 node.right.left = null;  
  123.                 node.right.right = null;  
  124.                 node.right.parent = node;  
  125.             }  
  126.         }  
  127.     }  
  128.   
  129.     // 找到等值,返回null  
  130.     // 否则返回父节点,将新插入的node做为其孩子  
  131.     public Node findPlace(int nv) {  
  132.         Node target = null;  
  133.         Node node = this.root;  
  134.   
  135.         while (node != null) {  
  136.             System.out.print(node.value + ",");  
  137.   
  138.             if (nv == node.value) {  
  139.                 target = node;  
  140.                 break;  
  141.             }  
  142.   
  143.             if (nv < node.value) {  
  144.                 if (node.left != null) {  
  145.                     node = node.left;  
  146.                     continue;  
  147.                 } else {  
  148.                     target = node;  
  149.                     break;  
  150.                 }  
  151.             }  
  152.   
  153.             if (nv > node.value) {  
  154.                 if (node.right != null) {  
  155.                     node = node.right;  
  156.                     continue;  
  157.                 } else {  
  158.                     target = node;  
  159.                     break;  
  160.                 }  
  161.             }  
  162.         }  
  163.   
  164.         return target;  
  165.     }  
  166.   
  167.     // 在二叉树中搜索某值  
  168.     // 如果找到,则返回匹配节点,不见返回null  
  169.     public Node search(int v) {  
  170.         System.out.print("\n二叉树查找经过的节点:");  
  171.         Node target = null;  
  172.         Node node = this.root;  
  173.   
  174.         while (node != null) {  
  175.             System.out.print(node.value + ",");  
  176.   
  177.             if (v == node.value) {  
  178.                 target = node;  
  179.                 break;  
  180.             }  
  181.   
  182.             if (v < node.value && node.left != null) {  
  183.                 if (node.left != null) {  
  184.                     node = node.left;  
  185.                     continue;  
  186.                 } else {  
  187.                     target = null;  
  188.                     break;  
  189.                 }  
  190.             }  
  191.   
  192.             if (v > node.value) {  
  193.                 if (node.right != null) {  
  194.                     node = node.right;  
  195.                     continue;  
  196.                 } else {  
  197.                     target = null;  
  198.                     break;  
  199.                 }  
  200.             }  
  201.         }  
  202.   
  203.         return target;  
  204.     }  
  205.   
  206.     // 先根遍历(根左右)  
  207.     public void dfsFirst(Node node) {  
  208.         if (node == null) {  
  209.             return;  
  210.         }  
  211.   
  212.         // 访问根  
  213.         System.out.print(node.value + ",");  
  214.   
  215.         // 访问左子树  
  216.         if (node.left != null) {  
  217.             this.dfsFirst(node.left);  
  218.         }  
  219.   
  220.         // 访问右子树  
  221.         if (node.right != null) {  
  222.             this.dfsFirst(node.right);  
  223.         }  
  224.     }  
  225.   
  226.     // 中根遍历(左根右)(从小到大)  
  227.     public void dfsMid(Node node) {  
  228.         if (node == null) {  
  229.             return;  
  230.         }  
  231.   
  232.         // 访问左子树  
  233.         if (node.left != null) {  
  234.             this.dfsMid(node.left);  
  235.         }  
  236.   
  237.         // 访问根  
  238.         System.out.print(node.value + ",");  
  239.   
  240.         // 访问右子树  
  241.         if (node.right != null) {  
  242.             this.dfsMid(node.right);  
  243.         }  
  244.     }  
  245.   
  246.     // ASC  
  247.     public void dfsASC(Node node) {  
  248.         this.dfsMid(node);  
  249.     }  
  250.   
  251.     // DESC,(右根左)(从大到小):  
  252.     public void dfsDESC(Node node) {  
  253.         if (node == null) {  
  254.             return;  
  255.         }  
  256.   
  257.         // 访问右子树  
  258.         if (node.right != null) {  
  259.             this.dfsDESC(node.right);  
  260.         }  
  261.   
  262.         // 访问根  
  263.         System.out.print(node.value + ",");  
  264.   
  265.         // 访问左子树  
  266.         if (node.left != null) {  
  267.             this.dfsDESC(node.left);  
  268.         }  
  269.     }  
  270.   
  271.     // 后根遍历(左右根)  
  272.     public void dfsLast(Node node) {  
  273.         if (node == null) {  
  274.             return;  
  275.         }  
  276.   
  277.         // 访问左子树  
  278.         if (node.left != null) {  
  279.             this.dfsLast(node.left);  
  280.         }  
  281.   
  282.         // 访问右子树  
  283.         if (node.right != null) {  
  284.             this.dfsLast(node.right);  
  285.         }  
  286.   
  287.         // 访问根  
  288.         System.out.print(node.value + ",");  
  289.     }  
  290.   
  291.     // 二叉树的广度优先遍历,顺序输出  
  292.     public void bfs() {  
  293.         System.out.print("\nBFS:");  
  294.         Queue<Node> queue = new LinkedList<Node>();  
  295.         queue.offer(this.root);  
  296.   
  297.         Node node = null;  
  298.         while (!queue.isEmpty()) {  
  299.             // 删除队列头结点  
  300.             node = queue.poll();  
  301.             System.out.print(node.value + ",");  
  302.   
  303.             // 将节点的左孩子加入队列  
  304.             if (node.left != null) {  
  305.                 queue.offer(node.left);  
  306.             }  
  307.             // 将节点的右孩子加入队列  
  308.             if (node.right != null) {  
  309.                 queue.offer(node.right);  
  310.             }  
  311.         }  
  312.     }  
  313.   
  314.     // 二叉树的广度优先遍历,按照层次,从上到下,从左到右,打印此树  
  315.     public void bfsLevel() {  
  316.         System.out.print("\n--------------- bfsLevel ---------------");  
  317.         System.out.print("\nBFS:[节点值,是否叶子,双亲节点,层次]");  
  318.         Queue<Node> queue = new LinkedList<Node>();  
  319.         queue.offer(this.root);  
  320.   
  321.         // 上一个节点的层次  
  322.         int prevLevel = 0;  
  323.         Node node = null;  
  324.   
  325.         while (!queue.isEmpty()) {  
  326.             // 删除队列头结点  
  327.             node = queue.poll();  
  328.             // 某节点在整棵树的第几层(ROOT为第1层)  
  329.             int levelOfFullTree = this.getLevelOfFullTree(node);  
  330.   
  331.             // 如果当前深度比前一个节点的尝试大,则开始的新一层的节点访问  
  332.             if (levelOfFullTree > prevLevel) {  
  333.                 System.out.print("\n");  
  334.             }  
  335.   
  336.             System.out.print("[");  
  337.             System.out.print(node.value + ",");  
  338.             System.out.print((node.parent == null ? null : node.parent.value) + ",");  
  339.             System.out.print(prevLevel);  
  340.             System.out.print("]");  
  341.             // System.out.printf("%3s," , node.value);  
  342.   
  343.             // 将节点的左孩子加入队列  
  344.             if (node.left != null) {  
  345.                 queue.offer(node.left);  
  346.             }  
  347.             // 将节点的右孩子加入队列  
  348.             if (node.right != null) {  
  349.                 queue.offer(node.right);  
  350.             }  
  351.   
  352.             prevLevel = levelOfFullTree;  
  353.         }  
  354.     }  
  355.   
  356.     // 树形本层节点打印  
  357.     private void printTreeLevel(int[] nodes) {  
  358.         System.out.print("\n|");  
  359.         for (int j = 0; j < nodes.length; j++) {  
  360.             if (nodes[j] == 0) {  
  361.                 // 打印两位数字的占位符  
  362.                 System.out.printf("--");  
  363.             } else {  
  364.                 // 打印节点  
  365.                 System.out.printf("%02d", nodes[j]);  
  366.                 // 重置数组  
  367.                 nodes[j] = 0;  
  368.             }  
  369.         }  
  370.         System.out.print("|");  
  371.     }  
  372.   
  373.     /** 
  374.      * 二叉树的广度优先遍历,按照树形打印此树 
  375.      *  
  376.      * <pre> 
  377.      * 算法用到的参数: 
  378.      * 1:二叉树的最大深度。 
  379.      * 2:每个节点在二叉树中的层次Level,从1开始。 
  380.      * 3:每个节点在该层中的序号indexOfLevel,从1开始。 
  381.      * 注: 
  382.      *  (1)Level和indexOfLevel可以在广度优先遍历时用计数器实现。 
  383.      *  (2)Level和indexOfLevel也可以在向树中插入新节点时,初始化到节点中。 
  384.      *      如果用数组存储二叉树,假设父节点下标为i,则其左孩子的下标是2*i-1,右孩子的下标是2*i+1。 
  385.      *  
  386.      * 算法基本思路: 
  387.      * (1):创建一个水平数组,水平数组的长度为 "满二叉树" 中的节点总数,将二叉树的所有节点,按满二叉树的样子,投影到水平数组上,每个节点在水平数组中都对就一个位置。 
  388.      * (2):我们总结一下,对于每一个层级,映射到水平数组后,第一个节点的开始下标=s,本层任意相邻节点的步长(间距)=d,如果下所示 
  389.      * 层级   起始下标        步长 
  390.      * 1    2^3-1=7     2^4=16 
  391.      * 2    2^2-1=3     2^3=8 
  392.      * 3    2^1-1=1     2^2=4 
  393.      * 4    2^0-1=0     2^1=2 
  394.      * (3):有了以上数据,我们可以计算出,任意一层,任意一节点在水平数组中的下标, 
  395.      * 下标=起始下标+(该节点所在层次-1)*步长 
  396.      * (4):OK,每一次每个节点的位置确定了,树形图自然也确定了。 
  397.      *  
  398.      * 另: 
  399.      * 如果想要让输出特别规矩,我们必须: 
  400.      * 1:先确定每个节点的值(即输出的内容)最多占多少个字符宽度,假设为flength。 
  401.      *     在输出树的过程中,不论遇到空值还是有值,都格式化输出,长度不足flength的,用空格补齐。 
  402.      * 2:可以适当的将水平数组扩大一倍,这样每层中的各节点之间的距离拉长了,最终看到的结果是整个树水平放大了。 
  403.      * </pre> 
  404.      */  
  405.     public void bfsTree() {  
  406.         System.out.print("\n------------------ 树形打印开始 ------------------");  
  407.   
  408.         if (this.root == null) {  
  409.             System.out.print("\n树为pw");  
  410.             return;  
  411.         }  
  412.   
  413.         // 二叉树的高度  
  414.         int maxLevel = this.getDepth(this.root);  
  415.         // 满二叉树时的总结点数  
  416.         int fullTotal = (int) Math.pow(2, maxLevel) - 1;  
  417.         // 水平数组  
  418.         int[] nodes = new int[fullTotal];  
  419.   
  420.         // 上一个节点的层次  
  421.         int prevLevel = 1;  
  422.         // 每层的起始下标  
  423.         int start = 0;  
  424.         // 每一层的元素的间距  
  425.         int stepSize = 0;  
  426.   
  427.         // 广度优先遍历  
  428.         Queue<Node> queue = new LinkedList<Node>();  
  429.         queue.offer(this.root);  
  430.         Node node = null;  
  431.         // 如果用数组存储二叉树,indexMap中存储各节点对应数组的下标  
  432.         Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();  
  433.         while (!queue.isEmpty()) {  
  434.             // 删除队列头结点  
  435.             node = queue.poll();  
  436.             // 某节点在整棵树的第几层(ROOT为第1层)  
  437.             int levelOfFullTree = this.getLevelOfFullTree(node);  
  438.   
  439.             // 如果当前深度比前一个节点的尝试大,则开始的新一层的节点访问  
  440.             if (levelOfFullTree > prevLevel) {  
  441.                 // 打印层次的节点  
  442.                 this.printTreeLevel(nodes);  
  443.             }  
  444.   
  445.             // 计算新的层次的开始下标与步长  
  446.             start = (int) Math.pow(2, maxLevel - levelOfFullTree) - 1;  
  447.             stepSize = (int) Math.pow(2, maxLevel - levelOfFullTree + 1);  
  448.             // System.out.print("\n" + "start:" + start + ",stepSize:" + stepSize);  
  449.   
  450.             // 记录节点的下标  
  451.             int idx = -1;  
  452.             if (node == this.root) {  
  453.                 indexMap.put(node.value, 1);  
  454.                 idx = 1;  
  455.             } else {  
  456.                 if (node == node.parent.left) {  
  457.                     idx = 2 * indexMap.get(node.parent.value) - 1;  
  458.                 } else if (node == node.parent.right) {  
  459.                     idx = 2 * indexMap.get(node.parent.value);  
  460.                 }  
  461.                 indexMap.put(node.value, idx);  
  462.             }  
  463.   
  464.             // 计算映射到水平数组的位置  
  465.             int y = start + (idx - 1) * stepSize;  
  466.             nodes[y] = node.value;  
  467.             // System.out.print("\n" + "node.value:" + node.value + ",y:" + y);  
  468.   
  469.             // 将节点的左孩子加入队列  
  470.             if (node.left != null) {  
  471.                 queue.offer(node.left);  
  472.             }  
  473.             // 将节点的右孩子加入队列  
  474.             if (node.right != null) {  
  475.                 queue.offer(node.right);  
  476.             }  
  477.   
  478.             // 保留层次数,为下次循环使用  
  479.             prevLevel = levelOfFullTree;  
  480.         }  
  481.   
  482.         // 打印最底层的节点,因为while的推出,最底层次的节点没有打印,在这里单独打印  
  483.         this.printTreeLevel(nodes);  
  484.         System.out.print("\n------------------ 树形打印结束 ------------------");  
  485.     }  
  486.   
  487.     // 计算以某节点为根的树的深度(从1开始)  
  488.     public int getDepth(Node node) {  
  489.         if (node == null) {  
  490.             return 0;  
  491.         }  
  492.   
  493.         return 1 + Math.max(this.getDepth(node.left), this.getDepth(node.right));  
  494.     }  
  495.   
  496.     // 计算某节点在整棵树的第几层(ROOT为第1层)  
  497.     public int getLevelOfFullTree(Node node) {  
  498.         int depth = 0;  
  499.         Node t = node;  
  500.         while (t != null) {  
  501.             depth++;  
  502.             t = t.parent;  
  503.         }  
  504.         return depth;  
  505.     }  
  506.   
  507.     // 计算以某节点为根的二叉树的总结点数(包含根节点)  
  508.     public int getTotalNode(Node node) {  
  509.         if (node == null) {  
  510.             return 0;  
  511.         }  
  512.   
  513.         int L = getTotalNode(node.left);  
  514.         int R = getTotalNode(node.right);  
  515.   
  516.         return 1 + (L + R);  
  517.     }  
  518.   
  519.     // 以该节点为根的树,找树中最大的元素  
  520.     public Node getMaxNode(Node node) {  
  521.         Node nodeMax = node;  
  522.         while (nodeMax != null) {  
  523.             if (nodeMax.right != null) {  
  524.                 nodeMax = nodeMax.right;  
  525.             } else {  
  526.                 break;  
  527.             }  
  528.         }  
  529.         return nodeMax;  
  530.     }  
  531.   
  532.     // 以该节点为根的树,找树中最大的元素  
  533.     public Node getMinNode(Node node) {  
  534.         Node nodeMax = node;  
  535.         while (nodeMax != null) {  
  536.             if (nodeMax.left != null) {  
  537.                 nodeMax = nodeMax.left;  
  538.             } else {  
  539.                 break;  
  540.             }  
  541.         }  
  542.         return nodeMax;  
  543.     }  
  544.   
  545.     /** 
  546.      * <pre> 
  547.      * 找到目标节点并删除,返回目标节点 
  548.      * 如果没找到,返回null 
  549.      * 注意,我们处理每一种分类时,总是先处理ROOT节点 
  550.      * </pre> 
  551.      *  
  552.      * @param v 
  553.      * @return 
  554.      */  
  555.     public Node delete(int v) {  
  556.         Node t = this.search(v);  
  557.   
  558.         if (t == null) {  
  559.             return t;  
  560.         }  
  561.   
  562.         // -------------------------------- 处理叶子节点,开始 --------------------------------  
  563.         if (t.isLeaf()) {  
  564.             // t是叶子节点,且是ROOT  
  565.             if (t.isRoot()) {  
  566.                 this.root = null;  
  567.                 return t;  
  568.             }  
  569.             // t是叶子节点,且是双亲的左孩子,但非ROOT  
  570.             if (t.isLeft()) {  
  571.                 t.parent.left = null;  
  572.                 return t;  
  573.             }  
  574.             // t是叶子节点,且是双亲的右孩子,但非ROOT  
  575.             if (t.isRight()) {  
  576.                 t.parent.right = null;  
  577.                 return t;  
  578.             }  
  579.         }  
  580.   
  581.         /** 
  582.          * -------------------------------- 处理有两个孩子的节点,开始 -------------------------------- 
  583.          *  
  584.          * <pre> 
  585.          * 如果目标节点t只有一个孩子,或有两个孩子,在这里我们统一处理,都当成有两个孩子。 
  586.          *  
  587.          * t有两个孩子的情况处理比较复杂点,当我们删除t后,必须找到一个合适的节点来顶替t的位置。 
  588.          * 这个节点的选择有两种方法。 
  589.          * 方法1:从目标节点的左子树中选择值最大的节点,去顶替t的位置。 
  590.          * 方法2:从目标节点的右子树中选择值最小的节点,顶替t的位置。 
  591.          *  
  592.          * 我们在这里是这样处理的: 
  593.          * 先从t的左右子树中,选择深度最深的子树(如果t.left==null,则左子树的尝试为0)。 
  594.          * 如果是t的左子树,选择值最大的节点。 
  595.          * 如果是t的右子树,选择值最小的节点。 
  596.          * 这样做可以尽量延迟二叉树出现极端不平衡的情况。 
  597.          *  
  598.          * <pre> 
  599.          */  
  600.         if (true) {  
  601.             int L = this.getDepth(t.left);  
  602.             int R = this.getDepth(t.right);  
  603.             // 新选出的节点  
  604.             Node nm = null;  
  605.   
  606.             if (L >= R) {  
  607.                 // 从t的左子树中选择一个最大的  
  608.                 nm = this.getMaxNode(t.left);  
  609.                 // 如果max是t的左子树的根,那么max一定没有右孩子  
  610.                 if (nm == t.left) {  
  611.                     nm.parent = t.parent;  
  612.                     nm.right = t.right;  
  613.                     t.right.parent = nm;  
  614.                 }  
  615.                 // 如果max是t的左子树的最右侧的叶子结点,max没有左孩子,或,max有左孩子  
  616.                 if (nm.isRight()) {  
  617.                     nm.parent.right = nm.left;  
  618.                     if (nm.left != null) {  
  619.                         nm.left.parent = nm.parent;  
  620.                     }  
  621.                     nm.left = t.left;  
  622.                     if (nm.left != null) {  
  623.                         nm.left.parent = nm;  
  624.                     }  
  625.                     nm.right = t.right;  
  626.                     if (nm.right != null) {  
  627.                         nm.right.parent = nm;  
  628.                     }  
  629.                     nm.parent = t.parent;  
  630.                 }  
  631.             } else {  
  632.                 // 从t的右子树中选择一个最小的  
  633.                 nm = this.getMinNode(t.right);  
  634.                 // min是t的右子树的根,那么min一定没有左孩子  
  635.                 if (nm == t.right) {  
  636.                     nm.parent = t.parent;  
  637.                     nm.left = t.left;  
  638.                 }  
  639.                 // min是t的右子树的最左侧的叶子结点,min没有右孩子,或,min有右孩子  
  640.                 if (nm.isLeft()) {  
  641.                     nm.parent.left = nm.right;  
  642.                     if (nm.right != null) {  
  643.                         nm.right.parent = nm.parent;  
  644.                     }  
  645.                     nm.left = t.left;  
  646.                     if (nm.left != null) {  
  647.                         nm.left.parent = nm;  
  648.                     }  
  649.                     nm.right = t.right;  
  650.                     if (nm.right != null) {  
  651.                         nm.right.parent = nm;  
  652.                     }  
  653.                     nm.parent = t.parent;  
  654.                 }  
  655.             }  
  656.   
  657.             // 调整选出的新节点与t的双亲节点的关系  
  658.             if (t.isRoot()) {  
  659.                 this.root = nm;  
  660.             } else {  
  661.                 if (t == t.parent.left) {  
  662.                     t.parent.left = nm;  
  663.                 } else {  
  664.                     t.parent.right = nm;  
  665.                 }  
  666.             }  
  667.         }  
  668.   
  669.         return t;  
  670.     }  
  671.   
  672.     public static void main(String[] args) {  
  673.         // 初始化二叉查找树  
  674.         BST bst = new BST();  
  675.         bst.insert(50);  
  676.         bst.insert(30);  
  677.         bst.insert(80);  
  678.         bst.insert(10);  
  679.         bst.insert(40);  
  680.         bst.insert(35);  
  681.         bst.insert(90);  
  682.         bst.insert(85);  
  683.         bst.insert(5);  
  684.         bst.insert(15);  
  685.         bst.insert(20);  
  686.         bst.insert(13);  
  687.         bst.insert(3);  
  688.         bst.insert(8);  
  689.         bst.insert(37);  
  690.         bst.insert(70);  
  691.         bst.insert(60);  
  692.         bst.insert(75);  
  693.         bst.insert(78);  
  694.         bst.insert(72);  
  695.         bst.insert(95);  
  696.         bst.insert(99);  
  697.   
  698.         bst.bfsLevel();  
  699.         bst.bfsTree();  
  700.   
  701.         // int v = 35;  
  702.         // Node node = bst.search(v);  
  703.   
  704.         // System.out.print("\ndepth:" + bst.getDepth(node));  
  705.   
  706.         // int k = bst.getMaxLevel(node);  
  707.         // System.out.print("\n树的深度,R:" + v + ",V:" + k);  
  708.         //  
  709.         // int k2 = bst.getTotalNode(node);  
  710.         // System.out.print("\n树总结点数,R:" + v + ",V:" + k2);  
  711.         //  
  712.         // Node nodeMax = bst.getMaxNode(node);  
  713.         // System.out.print("\n树中最大值,R:" + v + ",V:" + (nodeMax == null ? null : nodeMax.value));  
  714.   
  715. //      int value = 30;  
  716. //      bst.delete(value);  
  717. //      bst.bfsTree();  
  718.     }  
  719.   
  720. }  
0 0
原创粉丝点击