二叉树遍历递归或者非递归

二叉树的定义:
二叉树(binary tree)是结点的有限集合,这个集合或者空,或者由一个根及两个互不相交的称为这个根的左子树或右子树构成. 
  从定义可以看出,二叉树包括:1.空树 2.只有一个根节点 3.只有左子树   4.只有右子树  5.左右子树都存在    有且仅有这5中表现形式    

 二叉树的特点:

  1. 性质1:在二叉树的第i层上至多有2^(i-1)个节点(i >= 1)
  2. 性质2:深度为k的二叉树至多有2^(k-1)个节点(k >=1)
  3. 性质3:对于任意一棵二叉树T而言,其叶子节点数目为N0,度为2的节点数目为N2,则有N0 = N2 + 1。
  4. 性质4:具有n个节点的完全二叉树的深度 。

二叉树的遍历

二叉树的遍历分为三种:前序遍历 中序遍历 后序遍历

  • 前序遍历:按照“根左右”,先遍历根节点,再遍历左子树 ,再遍历右子树
  • 中序遍历:按照“左根右“,先遍历左子树,再遍历根节点,最后遍历右子树
  • 后续遍历:按照“左右根”,先遍历左子树,再遍历右子树,最后遍历根节点 
    其中前,后,中指的是每次遍历时候的根节点被遍历的顺序 
    ============


特殊的二叉树:

      (1)斜树:顾名思义,斜树一定是要斜的;所有的结点都只有左子树的二叉树叫左斜树,所有的结点都只有右子树的二叉树叫右斜树;其实,线性表就可以理解为树的一种特殊的表现形式;

       (2)满二叉树:在一棵二叉树中,如果所有分支结点都存在左子树和右子树,并且所有叶子都在同一层上,这样的二叉树称为满二叉树;如图:

         (3)完全二叉树:对一棵具有n个结点的二叉树按层序编号,如果编号为i的结点与同样深度的满二叉树中编号为i的结点在二叉树中位置完全相同,那么这棵二叉树称为完全二叉树;或者这样理解:在一棵二叉树中,除最后一层外,若其余层都是满的,并且最后一层或者是满的,或者是右边缺少连续若干个结点,则称此树为完全二叉树;

所以我们可以这样判断完全二叉树:那就是看着树的示意图,心中默默给每个结点按照满二叉树的结构逐层顺序编号,如果编号出现空档,就说明不是完全二叉树,否则就是;


二叉树的实现:同样,二叉树也可以通过顺序存储和链式存储来实现;

          二叉树的顺序存储就是用一维数组存储二叉树中的结点,并且结点的存储位置,也就是数组的下标要能体现结点之间的逻辑关系,比如父结点与子结点的逻辑关系,子结点 与子结点之间的关系;但顺序存储的实用性不强;

          所以一般采用链式存储;


二叉树的遍历:是指从根结点出发,按照某种次序,依次访问二叉树中所有结点,使得每个结点被访问一次且仅被访问一次;

 二叉树的遍历方式有好多种,如果我们限制了从左到右的习惯方式,那么主要就有以下几种:

         (1)前序遍历:先访问子结点,然后前序遍历左子树,再前序遍历右子树;如下图,遍历顺序是:ABDGHCEIF


         (2)中序遍历:从根结点开始(但并不是先访问根结点),中序遍历根结点的左子树,然后方式根结点,最后中序遍历右树,如图,遍历的顺序是:GDHBAEICF

           (3)后序遍历:从左到右先叶子后结点的方式遍历访问左右子树,最后是访问根结点;如图,遍历的顺序是:GHDBIEFCA

          (4)层序遍历:从树的第一层,也就是根结点开始访问,从上而下逐层遍历,在同一层中,按从左到右的顺序对结点进行逐个访问;如图,遍历顺序为:ABCDEFGHI




 




二叉树遍历的java实现

首先定义二叉树对象类
[java]  view plain  copy
  1. package test.tree;  
  2.   
  3. public class TreeNode {  
  4.     public int key;  
  5.     public String data;  
  6.     public TreeNode leftChild;  
  7.     public TreeNode rightChild;  
  8.     public boolean isVisted=false;    
  9.       
  10.     public TreeNode() {  
  11.     }  
  12.       
  13.       
  14.     public TreeNode(int key, String data) {  
  15.         this.key = key;  
  16.         this.data = data;  
  17.     }  
  18.   
  19.   
  20.     public TreeNode(int key, String data, TreeNode leftChild,  
  21.             TreeNode rightChild) {  
  22.         this.key = key;  
  23.         this.data = data;  
  24.         this.leftChild = leftChild;  
  25.         this.rightChild = rightChild;  
  26.     }  
  27.   
  28. }  



二叉树处理遍历 
[java]  view plain  copy
  1.   
[java]  view plain  copy
  1. package test.tree;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.Queue;  
  5. import java.util.Stack;  
  6.   
  7. public class BinaryTree {  
  8.             
  9.         private TreeNode root=null;    
  10.             
  11.         public BinaryTree(){    
  12.             root=new TreeNode(1,"rootNode(A)");    
  13.         }    
  14.             
  15.         /**  
  16.          * 创建一棵二叉树  
  17.          * <pre>  
  18.          *           A  
  19.          *     B          C  
  20.          *  D     E            F  
  21.          *   X  M   N      
  22.          *  </pre>  
  23.          * @param root  
  24.          */   
  25.         public void createBinTree(TreeNode root){    
  26.             TreeNode newNodeB = new TreeNode(2,"B");    
  27.             TreeNode newNodeC = new TreeNode(3,"C");    
  28.             TreeNode newNodeD = new TreeNode(4,"D");    
  29.             TreeNode newNodeE = new TreeNode(5,"E");    
  30.             TreeNode newNodeF = new TreeNode(6,"F");    
  31.             root.leftChild=newNodeB;    
  32.             root.rightChild=newNodeC;    
  33.             root.leftChild.leftChild=newNodeD;    
  34.             root.leftChild.rightChild=newNodeE;    
  35.             root.rightChild.rightChild=newNodeF;    
  36.             root.leftChild.rightChild.leftChild = new TreeNode(7"M");  
  37.             root.leftChild.rightChild.rightChild = new TreeNode(8,"N");  
  38.               
  39.             root.leftChild.leftChild.rightChild= new TreeNode(9,"X");  
  40.         }    
  41.             
  42.         public boolean isEmpty(){    
  43.             return root==null;    
  44.         }    
  45.         
  46.         //树的高度    
  47.         public int height(){    
  48.             return height(root);    
  49.         }    
  50.             
  51.         //节点个数    
  52.         public int size(){    
  53.             return size(root);    
  54.         }    
  55.             
  56.             
  57.         private int height(TreeNode subTree){    
  58.             if(subTree==null)    
  59.                 return 0;//递归结束:空树高度为0    
  60.             else{    
  61.                 int i=height(subTree.leftChild);    
  62.                 int j=height(subTree.rightChild);    
  63.                 return (i<j)?(j+1):(i+1);    
  64.             }    
  65.         }    
  66.             
  67.         private int size(TreeNode subTree){    
  68.             if(subTree==null){    
  69.                 return 0;    
  70.             }else{    
  71.                 return 1+size(subTree.leftChild)    
  72.                         +size(subTree.rightChild);    
  73.             }    
  74.         }    
  75.             
  76.         //返回双亲结点    
  77.         public TreeNode parent(TreeNode element){    
  78.             return (root==null|| root==element)?null:parent(root, element);    
  79.         }    
  80.             
  81.         public TreeNode parent(TreeNode subTree,TreeNode element){    
  82.             if(subTree==null)    
  83.                 return null;    
  84.             if(subTree.leftChild==element||subTree.rightChild==element)    
  85.                 //返回父结点地址    
  86.                 return subTree;    
  87.             TreeNode p;    
  88.             //现在左子树中找,如果左子树中没有找到,才到右子树去找    
  89.             if((p=parent(subTree.leftChild, element))!=null)    
  90.                 //递归在左子树中搜索    
  91.                 return p;    
  92.             else    
  93.                 //递归在右子树中搜索    
  94.                 return parent(subTree.rightChild, element);    
  95.         }    
  96.             
  97.         public TreeNode getLeftChildNode(TreeNode element){    
  98.             return (element!=null)?element.leftChild:null;    
  99.         }    
  100.             
  101.         public TreeNode getRightChildNode(TreeNode element){    
  102.             return (element!=null)?element.rightChild:null;    
  103.         }    
  104.             
  105.         public TreeNode getRoot(){    
  106.             return root;    
  107.         }    
  108.             
  109.         //在释放某个结点时,该结点的左右子树都已经释放,    
  110.         //所以应该采用后续遍历,当访问某个结点时将该结点的存储空间释放    
  111.         public void destroy(TreeNode subTree){    
  112.             //删除根为subTree的子树    
  113.             if(subTree!=null){    
  114.                 //删除左子树    
  115.                 destroy(subTree.leftChild);    
  116.                 //删除右子树    
  117.                 destroy(subTree.rightChild);    
  118.                 //删除根结点    
  119.                 subTree=null;    
  120.             }    
  121.         }    
  122.             
  123.         public void traverse(TreeNode subTree){    
  124.             System.out.println("key:"+subTree.key+"--name:"+subTree.data);;    
  125.             traverse(subTree.leftChild);    
  126.             traverse(subTree.rightChild);    
  127.         }    
  128.             
  129.         //前序遍历    
  130.         public void preOrder(TreeNode subTree){   
  131.             if(subTree!=null){    
  132.                 visted(subTree);    
  133.                 preOrder(subTree.leftChild);    
  134.                 preOrder(subTree.rightChild);    
  135.             }    
  136.         }    
  137.           
  138.         //中序遍历    
  139.         public void inOrder(TreeNode subTree){    
  140.             if(subTree!=null){    
  141.                 inOrder(subTree.leftChild);    
  142.                 visted(subTree);    
  143.                 inOrder(subTree.rightChild);    
  144.             }    
  145.         }    
  146.             
  147.         //后续遍历    
  148.         public void postOrder(TreeNode subTree) {    
  149.             if (subTree != null) {    
  150.                 postOrder(subTree.leftChild);    
  151.                 postOrder(subTree.rightChild);    
  152.                 visted(subTree);    
  153.             }    
  154.         }    
  155.           
  156.            
  157.         //前序遍历的非递归实现  ABDXEMNCF  
  158.         public void nonRecPreOrder(TreeNode p){    
  159.             Stack<TreeNode> stack=new Stack<TreeNode>();    
  160.             TreeNode node=p;    
  161.             while(node!=null||stack.size()>0){    
  162.                 while(node!=null){    
  163.                     visted(node);    
  164.                     stack.push(node);   
  165.                     node=node.leftChild;    
  166.                 }    
  167.                 if(stack.size()>0){   
  168.                     node=stack.pop();    
  169.                     node=node.rightChild;   
  170.                 }    
  171.             }    
  172.         }    
  173.           
  174.           
  175.         public void preTraversal(TreeNode p ){  
  176.             Stack<TreeNode>  a = new Stack<TreeNode>();  
  177.             a.push(p);  
  178.             TreeNode t;  
  179.             while( !a.isEmpty() ){  
  180.                 t = a.pop();  
  181.                 while( t!=null){  
  182.                     System.out.println(t.data);  
  183.                     if(t.rightChild!=null)  
  184.                         {a.push(t.rightChild);}  
  185.                     t = t.leftChild;       
  186.                 }  
  187.             }  
  188.           }  
  189.           
  190.           
  191.          
  192.         //中序遍历的非递归实现    
  193.         public void nonRecInOrder(TreeNode p){    
  194.             Stack<TreeNode> stack =new Stack<TreeNode>();    
  195.             TreeNode node =p;    
  196.             while(node!=null||stack.size()>0){    
  197.                 //存在左子树    
  198.                 while(node!=null){    
  199.                     stack.push(node);    
  200.                     node=node.leftChild;    
  201.                 }    
  202.                 //栈非空    
  203.                 if(stack.size()>0){    
  204.                     node=stack.pop();    
  205.                     visted(node);    
  206.                     node=node.rightChild;    
  207.                 }    
  208.             }    
  209.         }    
  210.             
  211.         //后序遍历的非递归实现    
  212.         public void noRecPostOrder(TreeNode p){    
  213.             Stack<TreeNode> stack=new Stack<TreeNode>();    
  214.             TreeNode node =p;    
  215.             while(p!=null){    
  216.                 //左子树入栈    
  217.                 for(;p.leftChild!=null;p=p.leftChild){    
  218.                     stack.push(p);    
  219.                 }    
  220.                 //当前结点无右子树或右子树已经输出    
  221.                 while(p!=null&&(p.rightChild==null||p.rightChild==node)){    
  222.                     visted(p);    
  223.                     //纪录上一个已输出结点    
  224.                     node =p;    
  225.                     if(stack.empty())    
  226.                         return;    
  227.                     p=stack.pop();    
  228.                 }    
  229.                 //处理右子树    
  230.                 stack.push(p);    
  231.                 p=p.rightChild;    
  232.             }    
  233.         }    
  234.         public void visted(TreeNode subTree){    
  235.             subTree.isVisted=true;    
  236.             System.out.println("key:"+subTree.key+"--name:"+subTree.data);;    
  237.         }    
  238.           
  239.           
  240.         //层次遍历  
  241.         public void levelIterator(TreeNode n){  
  242.             Queue<TreeNode> queue = new LinkedList<TreeNode>();  
  243.             queue.offer(n);  
  244.             while (!queue.isEmpty()) {  
  245.                 TreeNode t = queue.poll();  
  246.                 if (t !=null) {  
  247.                     visted(t);  
  248.                 }  
  249.                 if (t.leftChild !=null) {  
  250.                     queue.offer(t.leftChild);  
  251.                 }  
  252.                 if (t.rightChild !=null) {  
  253.                     queue.offer(t.rightChild);  
  254.                 }  
  255.             }  
  256.         }  
  257.           
  258.           
  259.         //测试    
  260.         public static void main(String[] args) {    
  261.             BinaryTree bt = new BinaryTree();    
  262.             bt.createBinTree(bt.root);    
  263.             System.out.println("the size of the tree is " + bt.size());    
  264.             System.out.println("the height of the tree is " + bt.height());    
  265.                 
  266.             System.out.println("*******(前序遍历)遍历*****************");    
  267.             bt.preOrder(bt.root);    
  268.                 
  269.             System.out.println("*******(中序遍历)遍历*****************");    
  270.             bt.inOrder(bt.root);    
  271.                
  272.             System.out.println("*******(后序遍历)遍历*****************");    
  273.             bt.postOrder(bt.root);    
  274.                 
  275.             System.out.println("***非递归实现****(前序遍历)遍历*****************");    
  276.             bt.nonRecPreOrder(bt.root);   
  277.             bt.preTraversal(bt.root);  
  278.                 
  279.             System.out.println("层次遍历*****************");    
  280.             bt.levelIterator(bt.root);  
  281.               
  282.             System.out.println("***非递归实现****(中序遍历)遍历*****************");    
  283.             bt.nonRecInOrder(bt.root);    
  284.                 
  285.             System.out.println("***非递归实现****(后序遍历)遍历*****************");    
  286.             bt.noRecPostOrder(bt.root);    
  287.         }   
  288.           
  289.           
  290.   
  291. }  

猜你喜欢

转载自blog.csdn.net/runrun117/article/details/80304880