zoukankan      html  css  js  c++  java
  • 二叉树的5道面试题

    1.在二叉树中查找元素值为x的节点的操作

     1 package com.neusoft.Tree;
     2 
     3 import java.awt.image.RescaleOp;
     4 
     5 /**
     6  * @author zhao-chj
     7  * 完成在二叉树中查找元素值为x的节点的操作
     8  *  在二叉树中查找数据元素的操作要求:
     9  *   在以T为根节点的二叉树中查找数据元素为x的节点
    10  *  如果找到则返回该元素,否则返回空值
    11  */
    12 public class Example1 {
    13     //采用先跟遍历的方式对二叉树进行比较
    14     public BiTreeNode searchNode(BiTreeNode T,Object x){
    15         if (T!=null) {
    16             if (T.data.equals(x)) {
    17                 return T;
    18             }else {
    19                 //查找左子树
    20                 BiTreeNode lresult=searchNode(T.lchild, x);
    21                 //查找右子树
    22                 BiTreeNode rchildresult=searchNode(T.rchild, x);
    23                 return lresult !=null?lresult:rchildresult;
    24                 //如果在左子树中,则返回值为x的元素,否则在右子树查找并返回结果
    25             }
    26         }
    27         return null;
    28     }
    29     public static void main(String[] args) {
    30         BiTree biTree=new BiTreeCreate().createBitree();
    31         BiTreeNode root=biTree.getRoot();//取得根节点元素
    32         //Debug
    33         Example1 example1 = new Example1();
    34         if (example1.searchNode(root, 'B')!=null) {
    35             System.out.println("树中包含指定元素的节点"
    36             +example1.searchNode(root, 'B').data);
    37         }else {
    38             System.out.println("树中不包含指定元素的值~");
    39         }
    40     }
    41     
    42 }

    点击可复制代码

     1 package com.neusoft.Tree;
     2 
     3 import java.awt.image.RescaleOp;
     4 
     5 /**
     6  * @author zhao-chj
     7  * 完成在二叉树中查找元素值为x的节点的操作
     8  *  在二叉树中查找数据元素的操作要求:
     9  *   在以T为根节点的二叉树中查找数据元素为x的节点
    10  *  如果找到则返回该元素,否则返回空值
    11  */
    12 public class Example1 {
    13     //采用先跟遍历的方式对二叉树进行比较
    14     public BiTreeNode searchNode(BiTreeNode T,Object x){
    15         if (T!=null) {
    16             if (T.data.equals(x)) {
    17                 return T;
    18             }else {
    19                 //查找左子树
    20                 BiTreeNode lresult=searchNode(T.lchild, x);
    21                 //查找右子树
    22                 BiTreeNode rchildresult=searchNode(T.rchild, x);
    23                 return lresult !=null?lresult:rchildresult;
    24                 //如果在左子树中,则返回值为x的元素,否则在右子树查找并返回结果
    25             }
    26         }
    27         return null;
    28     }
    29     public static void main(String[] args) {
    30         BiTree biTree=new BiTreeCreate().createBitree();
    31         BiTreeNode root=biTree.getRoot();//取得根节点元素
    32         //Debug
    33         Example1 example1 = new Example1();
    34         if (example1.searchNode(root, 'B')!=null) {
    35             System.out.println("树中包含指定元素的节点"
    36             +example1.searchNode(root, 'B').data);
    37         }else {
    38             System.out.println("树中不包含指定元素的值~");
    39         }
    40     }
    41     
    42 }
    点击+复制代码

    运行上述代码需要添加二叉树创建类BtreeCreate类

     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 }
    点击+可复制BtreeCreate代码

     运行结果:

    2.求解二叉树节点的个数

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * 求解二叉树节点的个数
     5  */
     6 public class Example2 {
     7     //统计节点的数目--先跟遍历的方式遍历
     8         public int countNode(BiTreeNode T){
     9             int count=0;
    10             if (T!=null) {
    11                 count++;
    12                 count+=countNode(T.lchild);
    13                 count+=countNode(T.rchild);
    14             }
    15             return count;
    16         }
    17     //递归模型求解
    18         public int countNode1(BiTreeNode T) {
    19             if (T==null) {
    20                 return 0;
    21             }else {
    22                 return countNode1(T.lchild)
    23                         +countNode1(T.rchild)+1;
    24             }
    25         }
    26         public static void main(String[] args) {
    27             BiTree biTree =new BiTreeCreate().createBitree();
    28             BiTreeNode root = biTree.getRoot();
    29             Example2 example2 = new Example2();
    30             System.out.println("树中的节点个数为"+example2.countNode(root));
    31             System.out.println("树中的节点个数为"+example2.countNode1(root));
    32         }
    33 }

    单击复制代码

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * 求解二叉树节点的个数
     5  */
     6 public class Example2 {
     7     //统计节点的数目--先跟遍历的方式遍历
     8         public int countNode(BiTreeNode T){
     9             int count=0;
    10             if (T!=null) {
    11                 count++;
    12                 count+=countNode(T.lchild);
    13                 count+=countNode(T.rchild);
    14             }
    15             return count;
    16         }
    17     //递归模型求解
    18         public int countNode1(BiTreeNode T) {
    19             if (T==null) {
    20                 return 0;
    21             }else {
    22                 return countNode1(T.lchild)
    23                         +countNode1(T.rchild)+1;
    24             }
    25         }
    26         public static void main(String[] args) {
    27             BiTree biTree =new BiTreeCreate().createBitree();
    28             BiTreeNode root = biTree.getRoot();
    29             Example2 example2 = new Example2();
    30             System.out.println("树中的节点个数为"+example2.countNode(root));
    31             System.out.println("树中的节点个数为"+example2.countNode1(root));
    32         }
    33 }
    点击+可复制代码

     运行上述代码需要添加创建二叉树的类BtreeCreate类

     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 }
    点击+可复制BtreeCreeate代码

    运行结果:

    3.求解二叉树的深度

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * shift+Alt+J
     5  * 求解二叉树的深度
     6  */
     7 public class Example3 {
     8     //采用
     9     public int getDepth(BiTreeNode T){
    10         if (T!=null) {
    11             int lDepth=getDepth(T.lchild);
    12             int rDepth=getDepth(T.rchild);
    13             return 1+(lDepth>rDepth?lDepth:rDepth);
    14         }
    15         return 0;
    16     }
    17     //采用递归模型
    18     public int getDepth1( BiTreeNode T){
    19         if (T==null) {
    20             return 0;
    21         }else if (T.lchild==null&&T.rchild==null) {
    22             return 1;
    23         }else {
    24             int length=
    25             getDepth(T.lchild)>getDepth(T.rchild)
    26             ?getDepth(T.lchild):getDepth(T.rchild);
    27             return 1+length;
    28         }
    29     }
    30     public static void main(String[] args) {
    31         BiTree biTree =new BiTreeCreate().createBitree();
    32         BiTreeNode root = biTree.getRoot();
    33         Example3 e3 = new Example3();
    34         System.out.println("树中的深度为"+e3.getDepth(root));
    35         System.out.println("树中的深度为"+e3.getDepth1(root));
    36     }
    37 }

    点击可复制代码

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * shift+Alt+J
     5  * 求解二叉树的深度
     6  */
     7 public class Example3 {
     8     //采用
     9     public int getDepth(BiTreeNode T){
    10         if (T!=null) {
    11             int lDepth=getDepth(T.lchild);
    12             int rDepth=getDepth(T.rchild);
    13             return 1+(lDepth>rDepth?lDepth:rDepth);
    14         }
    15         return 0;
    16     }
    17     //采用递归模型
    18     public int getDepth1( BiTreeNode T){
    19         if (T==null) {
    20             return 0;
    21         }else if (T.lchild==null&&T.rchild==null) {
    22             return 1;
    23         }else {
    24             int length=
    25             getDepth(T.lchild)>getDepth(T.rchild)
    26             ?getDepth(T.lchild):getDepth(T.rchild);
    27             return 1+length;
    28         }
    29     }
    30     public static void main(String[] args) {
    31         BiTree biTree =new BiTreeCreate().createBitree();
    32         BiTreeNode root = biTree.getRoot();
    33         Example3 e3 = new Example3();
    34         System.out.println("树中的节点个数为"+e3.getDepth(root));
    35         System.out.println("树中的节点个数为"+e3.getDepth1(root));
    36     }
    37 }
    点击+可复制代码

    运行上述代码需要添加创建二叉树的类

     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 }
    点击+复制二叉树创建的BtreeCreate类

     运行结果:

    4.判断两颗二叉树是否相等

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * 判断两颗二叉树是否相等
     5  * 采用递归方法
     6  */
     7 public class Example4 {
     8     //普通方法
     9     public boolean isEqual(BiTreeNode T1,BiTreeNode T2){
    10         if (T1==null && T2==null) {
    11             return true;
    12         }
    13         if (T1!=null&&T2!=null) {
    14             if (T1.data.equals(T2.data)) {
    15                 if (isEqual(T1.lchild, T2.lchild)) {
    16                     if (isEqual(T1.rchild, T2.rchild)) {
    17                         return true;
    18                     }
    19                 }
    20             }
    21         }
    22         return false;
    23     }
    24     //递归模型
    25     public boolean isEqual2(BiTreeNode T1,BiTreeNode T2){
    26         if (T1==null && T2==null) {
    27             return true;
    28         }else if (T1!=null&&T2!=null) {
    29             return (T1.data.equals(T2.data))&&
    30                     (isEqual(T1.lchild,T2.lchild))&&
    31                     (isEqual(T1.rchild, T2.rchild));
    32         }else{
    33             return false;
    34         }
    35     }
    36     public static void main(String[] args) {
    37         BiTreeNode T1 =new BiTreeCreate().createBitree().getRoot();
    38         BiTreeNode T2=new BiTreeCreate().createBitree2().getRoot();
    39         Example4 e4 = new Example4();
    40         if (e4.isEqual(T1, T2)) {
    41             System.out.println("两棵树相等");
    42         }else {
    43             System.out.println("两棵树不相等");
    44         }
    45         if (e4.isEqual2(T1, T2)) {
    46             System.out.println("两棵树相等");
    47         }else {
    48             System.out.println("两棵树不相等");
    49         }
    50     }
    51 }

    点击复制代码

     1 package com.neusoft.Tree;
     2 /**
     3  * @author zhao-chj
     4  * 判断两颗二叉树是否相等
     5  * 采用递归方法
     6  */
     7 public class Example4 {
     8     //普通方法
     9     public boolean isEqual(BiTreeNode T1,BiTreeNode T2){
    10         if (T1==null && T2==null) {
    11             return true;
    12         }
    13         if (T1!=null&&T2!=null) {
    14             if (T1.data.equals(T2.data)) {
    15                 if (isEqual(T1.lchild, T2.lchild)) {
    16                     if (isEqual(T1.rchild, T2.rchild)) {
    17                         return true;
    18                     }
    19                 }
    20             }
    21         }
    22         return false;
    23     }
    24     //递归模型
    25     public boolean isEqual2(BiTreeNode T1,BiTreeNode T2){
    26         if (T1==null && T2==null) {
    27             return true;
    28         }else if (T1!=null&&T2!=null) {
    29             return (T1.data.equals(T2.data))&&
    30                     (isEqual(T1.lchild,T2.lchild))&&
    31                     (isEqual(T1.rchild, T2.rchild));
    32         }else{
    33             return false;
    34         }
    35     }
    36     public static void main(String[] args) {
    37         BiTreeNode T1 =new BiTreeCreate().createBitree().getRoot();
    38         BiTreeNode T2=new BiTreeCreate().createBitree2().getRoot();
    39         Example4 e4 = new Example4();
    40         if (e4.isEqual(T1, T2)) {
    41             System.out.println("两棵树相等");
    42         }else {
    43             System.out.println("两棵树不相等");
    44         }
    45         if (e4.isEqual2(T1, T2)) {
    46             System.out.println("两棵树相等");
    47         }else {
    48             System.out.println("两棵树不相等");
    49         }
    50     }
    51 }
    点击+复制代码

    运行上述代码需要加入二叉树创建类BtreeCreate类

     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 }
    点击+复制BtreeCreate代码

    运行结果:

    5.由先根遍历和中跟遍历建立二叉树,然后输出后序遍历

     1 package com.neusoft.Tree;
     2 
     3 /**
     4  * @author zhao-chj 由先根遍历和中跟遍历建立二叉树,然后输出后序遍历
     5  */
     6 public class Example5 {
     7     public static void main(String[] args) {
     8         String preOrder="ABDEGCFH";
     9         String inorder="DBGEAFHC";
    10         BiTree t =new BiTree(preOrder,inorder,0,0,preOrder.length());
    11         System.out.println("后根遍历为:");
    12         t.postRootTraverse();
    13     }
    14 }

    点击可复制代码

     1 package com.neusoft.Tree;
     2 
     3 /**
     4  * @author zhao-chj 由先根遍历和中跟遍历建立二叉树,然后输出后序遍历
     5  */
     6 public class Example5 {
     7     public static void main(String[] args) {
     8         String preOrder="ABDEGCFH";
     9         String inorder="DBGEAFHC";
    10         BiTree t =new BiTree(preOrder,inorder,0,0,preOrder.length());
    11         System.out.println("后根遍历为:");
    12         t.postRootTraverse();
    13     }
    14 }
    点击+复制代码

    运行上述代码需要添加Btree的基本操作

      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 }
    点击+复制Btree代码

    运行结果:

  • 相关阅读:
    微信企业号第三方应用开发[二]——创建应用
    微信企业号第三方应用开发[一]——创建套件
    微信企业号第三方应用开发[目录]
    微信企业号第三方应用开发[前言]
    基于netty的心跳机制实现
    Jquery使select、radio某项选中
    画廊插件baguetteBox
    HTML5实现下载文件且指定下载文件名
    Python的生成器和生成器表达式
    Python的闭包以及迭代器
  • 原文地址:https://www.cnblogs.com/jackchen-Net/p/6815978.html
Copyright © 2011-2022 走看看