zoukankan      html  css  js  c++  java
  • 二叉链表(双叉链表)实现二叉树

    1.二叉树的节点声明

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * 保存Node节点的数据域指针域
     5  */
     6 public class BiTreeNode {
     7     public Object data;//数据域
     8     public BiTreeNode lchild,rchild;
     9     public BiTreeNode() {
    10         // TODO 构造函数
    11         this(null);
    12     }
    13     public BiTreeNode(Object data){
    14         this(data,null,null);
    15     }
    16     public BiTreeNode(Object data,BiTreeNode lchild,BiTreeNode rchild){
    17         this.data=data;
    18         this.lchild=lchild;
    19         this.rchild=rchild;
    20     }
    21 }

    点击+复制代码

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * 保存Node节点的数据域指针域
     5  */
     6 public class BiTreeNode {
     7     public Object data;//数据域
     8     public BiTreeNode lchild,rchild;
     9     public BiTreeNode() {
    10         // TODO 构造函数
    11         this(null);
    12     }
    13     public BiTreeNode(Object data){
    14         this(data,null,null);
    15     }
    16     public BiTreeNode(Object data,BiTreeNode lchild,BiTreeNode rchild){
    17         this.data=data;
    18         this.lchild=lchild;
    19         this.rchild=rchild;
    20     }
    21 }
    点击+复制代码

    2.二叉树的各种操作(遍历及节点的统计)

      1 package com.neusoft.Tree;
      2 import com.neusoft.Queue.LinkQueue;
      3 import com.neusoft.stack.LinkStack;
      4 /**
      5  * @author zhao-chj
      6  * 二叉链表存储二叉树
      7  * 1.导入链表和链栈的工具类
      8  */
      9 public class BiTree {
     10     private BiTreeNode root;
     11     public BiTreeNode getRoot() {
     12         return root;
     13     }
     14     public void setRoot(BiTreeNode root) {
     15         this.root = root;
     16     }
     17     public BiTree(){
     18         this.root=null;
     19     }
     20     public BiTree(BiTreeNode root){
     21         this.root=root;
     22     }
     23     //由先跟遍历和中根遍历创建一棵二叉树算法
     24     /**
     25      * @param preOrder 代表整棵树的先跟遍历
     26      * @param inOrder  整棵树的中根遍历
     27      * @param preIndex  先跟遍历从preOrder字符串中的开始位置
     28      * @param inIndex  是中根遍历从字符串inOrder中 的开始位置
     29      * @param count 树节点的个数
     30      */
     31     public BiTree(String preOrder,String inOrder,int preIndex,
     32             int inIndex,int count){
     33         if (count >0) {//先跟和中跟非空
     34             //取出先跟遍历字符串中的第一个元素作为根节点
     35             char r= preOrder.charAt(preIndex);
     36             int i=0;
     37             for(;i<count;i++){
     38                 //寻找根节点在中根遍历字符串中的索引
     39                 if (r==inOrder.charAt(inIndex)) {
     40                     break;
     41                 }
     42             }
     43             root = new BiTreeNode(r);//建立根节点
     44             root.lchild=new BiTree(preOrder,inOrder,preIndex
     45                     ,inIndex,i).root;//建立树的左子树
     46             root.rchild=new BiTree(preOrder,inOrder,preIndex
     47                     ,inIndex+i+1,count-i-1).root;//建立树的右子树
     48         }
     49     }
     50     //由标明空子树的先跟遍历创建一棵二叉树的算法
     51     private static int index=0;
     52     public BiTree(String preStr){
     53         //取出字符串索引为index的字符,且index自增1
     54         char c= preStr.charAt(index++);
     55         if (c!='#') {
     56             root=new BiTreeNode(c);
     57             root.lchild=new BiTree(preStr).root;
     58             root.rchild=new BiTree(preStr).root;
     59         }else {
     60             root=null;
     61         }
     62     }
     63     //先跟遍历二叉树的递归算法
     64     public void preOrderTraverse(BiTreeNode T){
     65         if (T!=null) {
     66             System.out.print(T.data);
     67             preOrderTraverse(T.lchild);
     68             preOrderTraverse(T.rchild);
     69         }
     70     }
     71     //先跟遍历二叉树的非递归算法
     72     
     73     //中跟遍历二叉树的递归算法
     74     public void inRootTraverse(BiTreeNode T){
     75         if (T!=null) {
     76             inRootTraverse(T.lchild);
     77             System.out.print(T.data);
     78             inRootTraverse(T.rchild);
     79         }
     80     }
     81     //中跟遍历二叉树的非递归算法
     82     
     83     //后跟遍历二叉树的递归算法
     84     public void postRootTraverse(BiTreeNode T){
     85         if (T!=null) {
     86             postRootTraverse(T.lchild);
     87             postRootTraverse(T.rchild);
     88             System.out.print(T.data);
     89         }
     90     }
     91     //后跟遍历二叉树的非递归算法
     92     
     93     //层次遍历二叉树(左到右,上到小)
     94     public void levelTraverse(){
     95         BiTreeNode T=root;
     96         if (T!=null) {
     97             LinkQueue L=new LinkQueue();//构造队列
     98             L.push(T);
     99             while (!L.isEmpty()) {
    100                 T=(BiTreeNode)L.poll();
    101                 System.out.print(T.data);
    102                 if (T.lchild!=null) {
    103                     L.push(T.lchild);
    104                 }
    105                 if (T.rchild!=null) {
    106                     L.push(T.rchild);
    107                 }
    108             }
    109         }
    110     }
    111     //统计叶节点的数目
    112     public int countLeafNode(BiTreeNode T){
    113         int count =0;
    114         if (T!=null) {
    115             if (T.lchild==null && T.rchild==null) {
    116                 count++;
    117             }else {
    118                 //加上左子树上面的叶子结点数目
    119                 count +=countLeafNode(T.lchild);
    120                 //加上右子树上面的叶子结点数目
    121                 count +=countLeafNode(T.rchild);
    122             }
    123         }
    124         return count;
    125     }
    126     //统计节点的数目
    127     public int countNode(BiTreeNode T){
    128         int count=0;
    129         if (T!=null) {
    130             count++;
    131             count+=countNode(T.lchild);
    132             count+=countNode(T.rchild);
    133         }
    134         return count;
    135     }
    136     
    137     
    138 }

    点击+可复制代码

      1 package com.neusoft.Tree;
      2 import com.neusoft.Queue.LinkQueue;
      3 import com.neusoft.stack.LinkStack;
      4 /**
      5  * @author zhao-chj
      6  * 二叉链表存储二叉树
      7  * 1.导入链表和链栈的工具类
      8  */
      9 public class BiTree {
     10     private BiTreeNode root;
     11     public BiTreeNode getRoot() {
     12         return root;
     13     }
     14     public void setRoot(BiTreeNode root) {
     15         this.root = root;
     16     }
     17     public BiTree(){
     18         this.root=null;
     19     }
     20     public BiTree(BiTreeNode root){
     21         this.root=root;
     22     }
     23     //由先跟遍历和中根遍历创建一棵二叉树算法
     24     /**
     25      * @param preOrder 代表整棵树的先跟遍历
     26      * @param inOrder  整棵树的中根遍历
     27      * @param preIndex  先跟遍历从preOrder字符串中的开始位置
     28      * @param inIndex  是中根遍历从字符串inOrder中 的开始位置
     29      * @param count 树节点的个数
     30      */
     31     public BiTree(String preOrder,String inOrder,int preIndex,
     32             int inIndex,int count){
     33         if (count >0) {//先跟和中跟非空
     34             //取出先跟遍历字符串中的第一个元素作为根节点
     35             char r= preOrder.charAt(preIndex);
     36             int i=0;
     37             for(;i<count;i++){
     38                 //寻找根节点在中根遍历字符串中的索引
     39                 if (r==inOrder.charAt(inIndex)) {
     40                     break;
     41                 }
     42             }
     43             root = new BiTreeNode(r);//建立根节点
     44             root.lchild=new BiTree(preOrder,inOrder,preIndex
     45                     ,inIndex,i).root;//建立树的左子树
     46             root.rchild=new BiTree(preOrder,inOrder,preIndex
     47                     ,inIndex+i+1,count-i-1).root;//建立树的右子树
     48         }
     49     }
     50     //由标明空子树的先跟遍历创建一棵二叉树的算法
     51     private static int index=0;
     52     public BiTree(String preStr){
     53         //取出字符串索引为index的字符,且index自增1
     54         char c= preStr.charAt(index++);
     55         if (c!='#') {
     56             root=new BiTreeNode(c);
     57             root.lchild=new BiTree(preStr).root;
     58             root.rchild=new BiTree(preStr).root;
     59         }else {
     60             root=null;
     61         }
     62     }
     63     //先跟遍历二叉树的递归算法
     64     public void preOrderTraverse(BiTreeNode T){
     65         if (T!=null) {
     66             System.out.print(T.data);
     67             preOrderTraverse(T.lchild);
     68             preOrderTraverse(T.rchild);
     69         }
     70     }
     71     //先跟遍历二叉树的非递归算法
     72     
     73     //中跟遍历二叉树的递归算法
     74     public void inRootTraverse(BiTreeNode T){
     75         if (T!=null) {
     76             inRootTraverse(T.lchild);
     77             System.out.print(T.data);
     78             inRootTraverse(T.rchild);
     79         }
     80     }
     81     //中跟遍历二叉树的非递归算法
     82     
     83     //后跟遍历二叉树的递归算法
     84     public void postRootTraverse(BiTreeNode T){
     85         if (T!=null) {
     86             postRootTraverse(T.lchild);
     87             postRootTraverse(T.rchild);
     88             System.out.print(T.data);
     89         }
     90     }
     91     //后跟遍历二叉树的非递归算法
     92     
     93     //层次遍历二叉树(左到右,上到小)
     94     public void levelTraverse(){
     95         BiTreeNode T=root;
     96         if (T!=null) {
     97             LinkQueue L=new LinkQueue();//构造队列
     98             L.push(T);
     99             while (!L.isEmpty()) {
    100                 T=(BiTreeNode)L.poll();
    101                 System.out.print(T.data);
    102                 if (T.lchild!=null) {
    103                     L.push(T.lchild);
    104                 }
    105                 if (T.rchild!=null) {
    106                     L.push(T.rchild);
    107                 }
    108             }
    109         }
    110     }
    111     //统计叶节点的数目
    112     public int countLeafNode(BiTreeNode T){
    113         int count =0;
    114         if (T!=null) {
    115             if (T.lchild==null && T.rchild==null) {
    116                 count++;
    117             }else {
    118                 //加上左子树上面的叶子结点数目
    119                 count +=countLeafNode(T.lchild);
    120                 //加上右子树上面的叶子结点数目
    121                 count +=countLeafNode(T.rchild);
    122             }
    123         }
    124         return count;
    125     }
    126     //统计节点的数目
    127     public int countNode(BiTreeNode T){
    128         int count=0;
    129         if (T!=null) {
    130             count++;
    131             count+=countNode(T.lchild);
    132             count+=countNode(T.rchild);
    133         }
    134         return count;
    135     }
    136     
    137     
    138 }
    点击+复制代码

    3.模拟创建树的代码

     1 package com.neusoft.Tree;
     2 /*
     3  * 初始化一棵树
     4  */
     5 public class BiTreeCreate {
     6    public BiTree createBitree(){
     7        BiTreeNode A=new BiTreeNode('A');
     8        BiTreeNode B=new BiTreeNode('B');
     9        BiTreeNode C=new BiTreeNode('C');
    10        BiTreeNode D=new BiTreeNode('D');
    11        BiTreeNode E=new BiTreeNode('E');
    12        BiTreeNode F=new BiTreeNode('F',null,A);
    13        BiTreeNode G=new BiTreeNode('G',B,null);
    14        BiTreeNode H=new BiTreeNode('H',G,null);
    15        BiTreeNode I=new BiTreeNode('I',null,E);
    16        BiTreeNode J=new BiTreeNode('J',D,I);
    17        BiTreeNode K=new BiTreeNode('K',F,H);
    18        BiTreeNode L=new BiTreeNode('L',C,J);
    19        BiTreeNode root=new BiTreeNode('M',K,L);
    20        return new BiTree(root);
    21    }
    22    public BiTree createBitree2(){
    23        BiTreeNode d=new BiTreeNode('D');
    24        BiTreeNode g=new BiTreeNode('G');
    25        BiTreeNode h=new BiTreeNode('H');
    26        BiTreeNode e=new BiTreeNode('E',g,null);
    27        BiTreeNode b=new BiTreeNode('B',d,e);
    28        BiTreeNode f=new BiTreeNode('F',null,h);
    29        BiTreeNode c=new BiTreeNode('C',f,null);
    30        BiTreeNode a=new BiTreeNode('A',b,c);
    31        return  new BiTree(a);
    32    }
    33 }

    点击+可复制代码

     1 package com.neusoft.Tree;
     2 /*
     3  * 初始化一棵树
     4  */
     5 public class BiTreeCreate {
     6    public BiTree createBitree(){
     7        BiTreeNode A=new BiTreeNode('A');
     8        BiTreeNode B=new BiTreeNode('B');
     9        BiTreeNode C=new BiTreeNode('C');
    10        BiTreeNode D=new BiTreeNode('D');
    11        BiTreeNode E=new BiTreeNode('E');
    12        BiTreeNode F=new BiTreeNode('F',null,A);
    13        BiTreeNode G=new BiTreeNode('G',B,null);
    14        BiTreeNode H=new BiTreeNode('H',G,null);
    15        BiTreeNode I=new BiTreeNode('I',null,E);
    16        BiTreeNode J=new BiTreeNode('J',D,I);
    17        BiTreeNode K=new BiTreeNode('K',F,H);
    18        BiTreeNode L=new BiTreeNode('L',C,J);
    19        BiTreeNode root=new BiTreeNode('M',K,L);
    20        return new BiTree(root);
    21    }
    22    public BiTree createBitree2(){
    23        BiTreeNode d=new BiTreeNode('D');
    24        BiTreeNode g=new BiTreeNode('G');
    25        BiTreeNode h=new BiTreeNode('H');
    26        BiTreeNode e=new BiTreeNode('E',g,null);
    27        BiTreeNode b=new BiTreeNode('B',d,e);
    28        BiTreeNode f=new BiTreeNode('F',null,h);
    29        BiTreeNode c=new BiTreeNode('C',f,null);
    30        BiTreeNode a=new BiTreeNode('A',b,c);
    31        return  new BiTree(a);
    32    }
    33 }
    点击+可复制代码

    4.二叉树的二叉链表表示法测试代码

     1 package com.neusoft.Tree;
     2 
     3 public class DebugBiTree {
     4     public BiTree createBitree2(){
     5            BiTreeNode d=new BiTreeNode('D');
     6            BiTreeNode g=new BiTreeNode('G');
     7            BiTreeNode h=new BiTreeNode('H');
     8            BiTreeNode e=new BiTreeNode('E',g,null);
     9            BiTreeNode b=new BiTreeNode('B',d,e);
    10            BiTreeNode f=new BiTreeNode('F',null,h);
    11            BiTreeNode c=new BiTreeNode('C',f,null);
    12            BiTreeNode a=new BiTreeNode('A',b,c);
    13            return  new BiTree(a);
    14        }
    15     public static void main(String[] args) {
    16         DebugBiTree debug=new DebugBiTree();
    17         BiTree biTree=debug.createBitree2();
    18         BiTreeNode root=biTree.getRoot();
    19         //Debug先根遍历
    20         System.out.println("(递归)先根遍历序列为:");
    21         biTree.preOrderTraverse(root);
    22         System.out.println();
    23         //Debug中根遍历
    24         System.out.println("(递归)中根遍历序列为:");
    25         biTree.inRootTraverse(root);
    26         System.out.println();
    27         //Debug后根遍历
    28         System.out.println("(递归)后根遍历序列为:");
    29         biTree.postRootTraverse(root);
    30         System.out.println();
    31         //Debug层次遍历
    32         System.out.println("(层次遍历序列为:)");
    33         biTree.levelTraverse();
    34         System.out.println();
    35     }
    36 }

    点击+可复制代码

     1 package com.neusoft.Tree;
     2 
     3 public class DebugBiTree {
     4     public BiTree createBitree2(){
     5            BiTreeNode d=new BiTreeNode('D');
     6            BiTreeNode g=new BiTreeNode('G');
     7            BiTreeNode h=new BiTreeNode('H');
     8            BiTreeNode e=new BiTreeNode('E',g,null);
     9            BiTreeNode b=new BiTreeNode('B',d,e);
    10            BiTreeNode f=new BiTreeNode('F',null,h);
    11            BiTreeNode c=new BiTreeNode('C',f,null);
    12            BiTreeNode a=new BiTreeNode('A',b,c);
    13            return  new BiTree(a);
    14        }
    15     public static void main(String[] args) {
    16         DebugBiTree debug=new DebugBiTree();
    17         BiTree biTree=debug.createBitree2();
    18         BiTreeNode root=biTree.getRoot();
    19         //Debug先根遍历
    20         System.out.println("(递归)先根遍历序列为:");
    21         biTree.preOrderTraverse(root);
    22         System.out.println();
    23         //Debug中根遍历
    24         System.out.println("(递归)中根遍历序列为:");
    25         biTree.inRootTraverse(root);
    26         System.out.println();
    27         //Debug后根遍历
    28         System.out.println("(递归)后根遍历序列为:");
    29         biTree.postRootTraverse(root);
    30         System.out.println();
    31         //Debug层次遍历
    32         System.out.println("(层次遍历序列为:)");
    33         biTree.levelTraverse();
    34         System.out.println();
    35     }
    36 }
    点击+可复制代码

    5.运行结果显示

        

     6.补充:二叉树的非递归实现及测试

    (1)二叉树的非递归实现

      1 package com.neusoft.Tree;
      2 import com.neusoft.Queue.LinkQueue;
      3 import com.neusoft.stack.LinkStack;
      4 /**
      5  * @author zhao-chj
      6  * 二叉链表存储二叉树
      7  * 1.导入链表和链栈的工具类
      8  */
      9 public class BiTree {
     10     private BiTreeNode root;
     11     public BiTreeNode getRoot() {
     12         return root;
     13     }
     14     public void setRoot(BiTreeNode root) {
     15         this.root = root;
     16     }
     17     public BiTree(){
     18         this.root=null;
     19     }
     20     public BiTree(BiTreeNode root){
     21         this.root=root;
     22     }
     23     //由先跟遍历和中根遍历创建一棵二叉树算法
     24     /**
     25      * @param preOrder 代表整棵树的先跟遍历
     26      * @param inOrder  整棵树的中根遍历
     27      * @param preIndex  先跟遍历从preOrder字符串中的开始位置
     28      * @param inIndex  是中根遍历从字符串inOrder中 的开始位置
     29      * @param count 树节点的个数
     30      */
     31     public BiTree(String preOrder,String inOrder,int preIndex,
     32             int inIndex,int count){
     33         if (count >0) {//先跟和中跟非空
     34             //取出先跟遍历字符串中的第一个元素作为根节点
     35             char r= preOrder.charAt(preIndex);
     36             int i=0;
     37             for(;i<count;i++){
     38                 //寻找根节点在中根遍历字符串中的索引
     39                 if (r==inOrder.charAt(inIndex+i)) {
     40                     break;
     41                 }
     42             }
     43             root = new BiTreeNode(r);//建立根节点
     44             root.lchild=new BiTree(preOrder,inOrder,preIndex+1
     45                     ,inIndex,i).root;//建立树的左子树
     46             root.rchild=new BiTree(preOrder,inOrder,preIndex+i+1
     47                     ,inIndex+i+1,count-i-1).root;//建立树的右子树
     48         }
     49     }
     50     //由标明空子树的先跟遍历创建一棵二叉树的算法
     51     private static int index=0;
     52     public BiTree(String preStr){
     53         //取出字符串索引为index的字符,且index自增1
     54         char c= preStr.charAt(index++);
     55         if (c!='#') {
     56             root=new BiTreeNode(c);
     57             root.lchild=new BiTree(preStr).root;
     58             root.rchild=new BiTree(preStr).root;
     59         }else {
     60             root=null;
     61         }
     62     }
     63     //先跟遍历二叉树的递归算法
     64     public void preOrderTraverse(BiTreeNode T){
     65         if (T!=null) {
     66             System.out.print(T.data);
     67             preOrderTraverse(T.lchild);
     68             preOrderTraverse(T.rchild);
     69         }
     70     }
     71     //先跟遍历二叉树的非递归算法
     72     public void preRootTraverse(){
     73         BiTreeNode T=root;
     74         if (T!=null) {
     75             LinkStack S=new LinkStack();//构造栈
     76             S.push(T);//根节点入栈
     77             while(!S.isEmpty()){
     78                 T=(BiTreeNode)S.pop();//移除栈顶元素,并返回其值
     79                 System.out.print(T.data);
     80                 while(T!=null){
     81                     if (T.lchild!=null) {//访问左孩子
     82                         System.out.print(T.lchild.data);
     83                     }
     84                     if (T.rchild!=null) {//右孩子非空入栈
     85                         S.push(T.rchild);
     86                     }
     87                     T=T.lchild;
     88                 }
     89             }
     90         }
     91     }
     92     //中跟遍历二叉树的递归算法
     93     public void inRootTraverse(BiTreeNode T){
     94         if (T!=null) {
     95             inRootTraverse(T.lchild);
     96             System.out.print(T.data);
     97             inRootTraverse(T.rchild);
     98         }
     99     }
    100     //中跟遍历二叉树的非递归算法
    101     public void inRootTraverse(){
    102         BiTreeNode T=root;
    103         if (T!=null) {
    104             LinkStack S=new LinkStack();//构造栈
    105             S.push(T);//根节点入栈
    106             while(!S.isEmpty()){
    107                 while(S.peek()!=null){
    108                     //将栈顶节点的所有左孩子入栈
    109                     S.push(((BiTreeNode)S.peek()).lchild);
    110                 }
    111                 S.pop();//空节点退栈
    112                 if (!S.isEmpty()) {
    113                     T=(BiTreeNode)S.pop();
    114                     System.out.print(T.data);
    115                     S.push(T.rchild);
    116                 }
    117             }
    118         }
    119     }
    120     //后跟遍历二叉树的递归算法
    121     public void postRootTraverse(BiTreeNode T){
    122         if (T!=null) {
    123             postRootTraverse(T.lchild);
    124             postRootTraverse(T.rchild);
    125             System.out.print(T.data);
    126         }
    127     }
    128     //后跟遍历二叉树的非递归算法
    129     public void postRootTraverse(){
    130         BiTreeNode T=root;
    131         if (T!=null) {
    132             LinkStack S=new LinkStack();//构造栈
    133             S.push(T);//根节点入栈
    134             Boolean flag;//访问标记
    135             BiTreeNode p=null;//p节点指向刚刚被访问过的节点
    136             while(!S.isEmpty()){
    137                 while(S.peek()!=null){
    138                     //将栈顶节点的所有左孩子入栈
    139                     S.push(((BiTreeNode)S.peek()).lchild);
    140                 }
    141                 S.pop();//空节点退栈
    142                 while(!S.isEmpty()){
    143                     T=(BiTreeNode)S.peek();
    144                     if (T.rchild==null || T.rchild==p) {
    145                         System.out.print(T.data);//访问节点
    146                         S.pop();//移除栈顶元素
    147                         p=T;//p指向刚被访问的节点
    148                         flag=true;//设置访问变量
    149                     }else {
    150                         S.push(T.rchild);//右孩子节点入栈
    151                         flag=false;//设置未被访问的变量
    152                     }
    153                     if (!flag) {
    154                         break;
    155                     }
    156                 }
    157             }
    158         }
    159     }
    160     //层次遍历二叉树(左到右,上到小)
    161     public void levelTraverse(){
    162         BiTreeNode T=root;
    163         if (T!=null) {
    164             LinkQueue L=new LinkQueue();//构造队列
    165             L.push(T);
    166             while (!L.isEmpty()) {
    167                 T=(BiTreeNode)L.poll();
    168                 System.out.print(T.data);
    169                 if (T.lchild!=null) {
    170                     L.push(T.lchild);
    171                 }
    172                 if (T.rchild!=null) {
    173                     L.push(T.rchild);
    174                 }
    175             }
    176         }
    177     }
    178     //统计叶节点的数目
    179     public int countLeafNode(BiTreeNode T){
    180         int count =0;
    181         if (T!=null) {
    182             if (T.lchild==null && T.rchild==null) {
    183                 count++;
    184             }else {
    185                 //加上左子树上面的叶子结点数目
    186                 count +=countLeafNode(T.lchild);
    187                 //加上右子树上面的叶子结点数目
    188                 count +=countLeafNode(T.rchild);
    189             }
    190         }
    191         return count;
    192     }
    193     //统计节点的数目
    194     public int countNode(BiTreeNode T){
    195         int count=0;
    196         if (T!=null) {
    197             count++;
    198             count+=countNode(T.lchild);
    199             count+=countNode(T.rchild);
    200         }
    201         return count;
    202     }
    203     
    204     
    205 }

    二叉链表的递归实现和非递归实现

      1 package com.neusoft.Tree;
      2 import com.neusoft.Queue.LinkQueue;
      3 import com.neusoft.stack.LinkStack;
      4 /**
      5  * @author zhao-chj
      6  * 二叉链表存储二叉树
      7  * 1.导入链表和链栈的工具类
      8  */
      9 public class BiTree {
     10     private BiTreeNode root;
     11     public BiTreeNode getRoot() {
     12         return root;
     13     }
     14     public void setRoot(BiTreeNode root) {
     15         this.root = root;
     16     }
     17     public BiTree(){
     18         this.root=null;
     19     }
     20     public BiTree(BiTreeNode root){
     21         this.root=root;
     22     }
     23     //由先跟遍历和中根遍历创建一棵二叉树算法
     24     /**
     25      * @param preOrder 代表整棵树的先跟遍历
     26      * @param inOrder  整棵树的中根遍历
     27      * @param preIndex  先跟遍历从preOrder字符串中的开始位置
     28      * @param inIndex  是中根遍历从字符串inOrder中 的开始位置
     29      * @param count 树节点的个数
     30      */
     31     public BiTree(String preOrder,String inOrder,int preIndex,
     32             int inIndex,int count){
     33         if (count >0) {//先跟和中跟非空
     34             //取出先跟遍历字符串中的第一个元素作为根节点
     35             char r= preOrder.charAt(preIndex);
     36             int i=0;
     37             for(;i<count;i++){
     38                 //寻找根节点在中根遍历字符串中的索引
     39                 if (r==inOrder.charAt(inIndex+i)) {
     40                     break;
     41                 }
     42             }
     43             root = new BiTreeNode(r);//建立根节点
     44             root.lchild=new BiTree(preOrder,inOrder,preIndex+1
     45                     ,inIndex,i).root;//建立树的左子树
     46             root.rchild=new BiTree(preOrder,inOrder,preIndex+i+1
     47                     ,inIndex+i+1,count-i-1).root;//建立树的右子树
     48         }
     49     }
     50     //由标明空子树的先跟遍历创建一棵二叉树的算法
     51     private static int index=0;
     52     public BiTree(String preStr){
     53         //取出字符串索引为index的字符,且index自增1
     54         char c= preStr.charAt(index++);
     55         if (c!='#') {
     56             root=new BiTreeNode(c);
     57             root.lchild=new BiTree(preStr).root;
     58             root.rchild=new BiTree(preStr).root;
     59         }else {
     60             root=null;
     61         }
     62     }
     63     //先跟遍历二叉树的递归算法
     64     public void preOrderTraverse(BiTreeNode T){
     65         if (T!=null) {
     66             System.out.print(T.data);
     67             preOrderTraverse(T.lchild);
     68             preOrderTraverse(T.rchild);
     69         }
     70     }
     71     //先跟遍历二叉树的非递归算法
     72     public void preRootTraverse(){
     73         BiTreeNode T=root;
     74         if (T!=null) {
     75             LinkStack S=new LinkStack();//构造栈
     76             S.push(T);//根节点入栈
     77             while(!S.isEmpty()){
     78                 T=(BiTreeNode)S.pop();//移除栈顶元素,并返回其值
     79                 System.out.print(T.data);
     80                 while(T!=null){
     81                     if (T.lchild!=null) {//访问左孩子
     82                         System.out.print(T.lchild.data);
     83                     }
     84                     if (T.rchild!=null) {//右孩子非空入栈
     85                         S.push(T.rchild);
     86                     }
     87                     T=T.lchild;
     88                 }
     89             }
     90         }
     91     }
     92     //中跟遍历二叉树的递归算法
     93     public void inRootTraverse(BiTreeNode T){
     94         if (T!=null) {
     95             inRootTraverse(T.lchild);
     96             System.out.print(T.data);
     97             inRootTraverse(T.rchild);
     98         }
     99     }
    100     //中跟遍历二叉树的非递归算法
    101     public void inRootTraverse(){
    102         BiTreeNode T=root;
    103         if (T!=null) {
    104             LinkStack S=new LinkStack();//构造栈
    105             S.push(T);//根节点入栈
    106             while(!S.isEmpty()){
    107                 while(S.peek()!=null){
    108                     //将栈顶节点的所有左孩子入栈
    109                     S.push(((BiTreeNode)S.peek()).lchild);
    110                 }
    111                 S.pop();//空节点退栈
    112                 if (!S.isEmpty()) {
    113                     T=(BiTreeNode)S.pop();
    114                     System.out.print(T.data);
    115                     S.push(T.rchild);
    116                 }
    117             }
    118         }
    119     }
    120     //后跟遍历二叉树的递归算法
    121     public void postRootTraverse(BiTreeNode T){
    122         if (T!=null) {
    123             postRootTraverse(T.lchild);
    124             postRootTraverse(T.rchild);
    125             System.out.print(T.data);
    126         }
    127     }
    128     //后跟遍历二叉树的非递归算法
    129     public void postRootTraverse(){
    130         BiTreeNode T=root;
    131         if (T!=null) {
    132             LinkStack S=new LinkStack();//构造栈
    133             S.push(T);//根节点入栈
    134             Boolean flag;//访问标记
    135             BiTreeNode p=null;//p节点指向刚刚被访问过的节点
    136             while(!S.isEmpty()){
    137                 while(S.peek()!=null){
    138                     //将栈顶节点的所有左孩子入栈
    139                     S.push(((BiTreeNode)S.peek()).lchild);
    140                 }
    141                 S.pop();//空节点退栈
    142                 while(!S.isEmpty()){
    143                     T=(BiTreeNode)S.peek();
    144                     if (T.rchild==null || T.rchild==p) {
    145                         System.out.print(T.data);//访问节点
    146                         S.pop();//移除栈顶元素
    147                         p=T;//p指向刚被访问的节点
    148                         flag=true;//设置访问变量
    149                     }else {
    150                         S.push(T.rchild);//右孩子节点入栈
    151                         flag=false;//设置未被访问的变量
    152                     }
    153                     if (!flag) {
    154                         break;
    155                     }
    156                 }
    157             }
    158         }
    159     }
    160     //层次遍历二叉树(左到右,上到小)
    161     public void levelTraverse(){
    162         BiTreeNode T=root;
    163         if (T!=null) {
    164             LinkQueue L=new LinkQueue();//构造队列
    165             L.push(T);
    166             while (!L.isEmpty()) {
    167                 T=(BiTreeNode)L.poll();
    168                 System.out.print(T.data);
    169                 if (T.lchild!=null) {
    170                     L.push(T.lchild);
    171                 }
    172                 if (T.rchild!=null) {
    173                     L.push(T.rchild);
    174                 }
    175             }
    176         }
    177     }
    178     //统计叶节点的数目
    179     public int countLeafNode(BiTreeNode T){
    180         int count =0;
    181         if (T!=null) {
    182             if (T.lchild==null && T.rchild==null) {
    183                 count++;
    184             }else {
    185                 //加上左子树上面的叶子结点数目
    186                 count +=countLeafNode(T.lchild);
    187                 //加上右子树上面的叶子结点数目
    188                 count +=countLeafNode(T.rchild);
    189             }
    190         }
    191         return count;
    192     }
    193     //统计节点的数目
    194     public int countNode(BiTreeNode T){
    195         int count=0;
    196         if (T!=null) {
    197             count++;
    198             count+=countNode(T.lchild);
    199             count+=countNode(T.rchild);
    200         }
    201         return count;
    202     }
    203     
    204     
    205 }
    点击+复制代码

    (2)测试代码

     1 package com.neusoft.Tree;
     2 
     3 public class DebugBiTree {
     4     public BiTree createBitree2(){
     5            BiTreeNode d=new BiTreeNode('D');
     6            BiTreeNode g=new BiTreeNode('G');
     7            BiTreeNode h=new BiTreeNode('H');
     8            BiTreeNode e=new BiTreeNode('E',g,null);
     9            BiTreeNode b=new BiTreeNode('B',d,e);
    10            BiTreeNode f=new BiTreeNode('F',null,h);
    11            BiTreeNode c=new BiTreeNode('C',f,null);
    12            BiTreeNode a=new BiTreeNode('A',b,c);
    13            return  new BiTree(a);
    14        }
    15     public static void main(String[] args) {
    16         DebugBiTree debug=new DebugBiTree();
    17         BiTree biTree=debug.createBitree2();
    18         BiTreeNode root=biTree.getRoot();
    19         //Debug先根遍历
    20         System.out.println("(递归)先根遍历序列为:");
    21         biTree.preOrderTraverse(root);
    22         System.out.println();
    23         System.out.println("(非递归)先根遍历序列为:");
    24         biTree.preRootTraverse();
    25         System.out.println();
    26         //Debug中根遍历
    27         System.out.println("(递归)中根遍历序列为:");
    28         biTree.inRootTraverse(root);
    29         System.out.println();
    30         System.out.println("(非递归)中根遍历序列为:");
    31         biTree.inRootTraverse();
    32         System.out.println();
    33         //Debug后根遍历
    34         System.out.println("(递归)后根遍历序列为:");
    35         biTree.postRootTraverse(root);
    36         System.out.println();
    37         System.out.println("(非递归)后根遍历序列为:");
    38         biTree.postRootTraverse();
    39         System.out.println();
    40         //Debug层次遍历
    41         System.out.println("(层次遍历序列为:)");
    42         biTree.levelTraverse();
    43         System.out.println();
    44     }
    45 }

    测试二叉树的递归和非递归实现方式

     1 package com.neusoft.Tree;
     2 
     3 public class DebugBiTree {
     4     public BiTree createBitree2(){
     5            BiTreeNode d=new BiTreeNode('D');
     6            BiTreeNode g=new BiTreeNode('G');
     7            BiTreeNode h=new BiTreeNode('H');
     8            BiTreeNode e=new BiTreeNode('E',g,null);
     9            BiTreeNode b=new BiTreeNode('B',d,e);
    10            BiTreeNode f=new BiTreeNode('F',null,h);
    11            BiTreeNode c=new BiTreeNode('C',f,null);
    12            BiTreeNode a=new BiTreeNode('A',b,c);
    13            return  new BiTree(a);
    14        }
    15     public static void main(String[] args) {
    16         DebugBiTree debug=new DebugBiTree();
    17         BiTree biTree=debug.createBitree2();
    18         BiTreeNode root=biTree.getRoot();
    19         //Debug先根遍历
    20         System.out.println("(递归)先根遍历序列为:");
    21         biTree.preOrderTraverse(root);
    22         System.out.println();
    23         System.out.println("(非递归)先根遍历序列为:");
    24         biTree.preRootTraverse();
    25         System.out.println();
    26         //Debug中根遍历
    27         System.out.println("(递归)中根遍历序列为:");
    28         biTree.inRootTraverse(root);
    29         System.out.println();
    30         System.out.println("(非递归)中根遍历序列为:");
    31         biTree.inRootTraverse();
    32         System.out.println();
    33         //Debug后根遍历
    34         System.out.println("(递归)后根遍历序列为:");
    35         biTree.postRootTraverse(root);
    36         System.out.println();
    37         System.out.println("(非递归)后根遍历序列为:");
    38         biTree.postRootTraverse();
    39         System.out.println();
    40         //Debug层次遍历
    41         System.out.println("(层次遍历序列为:)");
    42         biTree.levelTraverse();
    43         System.out.println();
    44     }
    45 }
    点击+复制代码

    (3)验证代码

        

  • 相关阅读:
    调试ASP
    规范的又一个例子
    不同单位转换问题
    客户处出现一个错误
    SQL视图嵌套视图很容易出错
    安装包得到大进步
    两年前的产品现在长得不错
    不用报表的理由
    PowerPoint笔记(五)
    文本编辑器vi
  • 原文地址:https://www.cnblogs.com/jackchen-Net/p/6800065.html
Copyright © 2011-2022 走看看