二叉树非递归遍历总结

package BinaryTree;
import java.util.*;


public class BinaryTree
{
    private BinaryTree lchild;
    private BinaryTree rchild;
    private int data;
    private boolean secondPushTostack =false ;
    
    public BinaryTree(BinaryTree l, BinaryTree r, int data) 
    {
        lchild = l;
        rchild = r;
        this.data = data;
    }
    
    public static void main(String[] args)
    {
     // TODO Auto-generated method stub
        BinaryTree  G= new BinaryTree(null, null, 8);
        BinaryTree  H= new BinaryTree(null, null, 8);
        BinaryTree  F= new BinaryTree(G, H, 6);
        BinaryTree  D= new BinaryTree(null, F, 4);
        BinaryTree  E= new BinaryTree(null, null, 5);
        BinaryTree  B= new BinaryTree(D, E,  2);
        BinaryTree  C= new BinaryTree(null, null, 3);
        BinaryTree A = new BinaryTree(B, C, 1);
        Vector<BinaryTree> vector = new Vector<BinaryTree>();
        System.out.println();
        //boolean hasNode = HasNode(A, G);
        //System.out.println(hasNode);
        
        FindPathEqualsToExpectedValue(A, A.data, 22, vector);
        
        //FindPath(A, G, vector);
//        InOderTraverse(A);
//        
//        System.out.println();
//        
//        PreOderTraverse(A);
//        System.out.println();
//        System.out.println();
        
        //PostOderTraverse(A);
        
    }

    public static void PreOderTraverse(BinaryTree node)
    {
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        do
        {

            while (node != null)
            {
                System.out.println(node.data);
                stack.push(node);
                node = node.lchild;
            }

            node = (BinaryTree) stack.pop();

            node = node.rchild;

        } while (!stack.isEmpty() || node != null);
    }
    
    public static void InOderTraverse(BinaryTree node)
    {
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        do
        {
            
            while (node!= null)
            {
                stack.push(node);
                node = node.lchild;                
            }
            
            node= (BinaryTree)stack.pop();
            System.out.println(node.data);

            node = node.rchild;
            
            
        } while (!stack.isEmpty() || node != null);
    }
    
    public static void PostOderTraverse(BinaryTree node)
    {
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        do
        {            
            while (node!= null)
            {
                stack.push(node);
                node = node.lchild;                
            }            
            node= (BinaryTree)stack.pop();
            while (node.secondPushTostack == true)
            {
                System.out.println(node.data);
                if(!stack.isEmpty())
                    node= (BinaryTree)stack.pop();
                else 
                {
                    return;
                }
                
            }                     
            node.secondPushTostack =true;
            stack.push(node);
            node = node.rchild; 
            
        } while (!true);
    }
    
    
    public static boolean HasNode(BinaryTree node, BinaryTree expectNode)
    {
        if (node == null)
            return false;
        if (expectNode == null)
            return false;
        if (node.data == expectNode.data)
            return true;
        return HasNode(node.lchild, expectNode) || HasNode(node.rchild, expectNode);

    }
    
    public static void  FindPath(BinaryTree pHead, BinaryTree targetNode, Vector<BinaryTree> vector)
    {
        if (pHead == null || targetNode == null)
        {
            return;
        }
        vector.add(pHead);
        if (pHead.data == targetNode.data)
        {
            for (int i = 0; i < vector.size(); i++)
            {
                System.out.println(vector.get(i).data);
            }
        }
        FindPath(pHead.lchild, targetNode, vector);
        FindPath(pHead.rchild, targetNode, vector);
        
        vector.remove(vector.size()-1);
               
    }
    
    public static void  FindPathEqualsToExpectedValue(BinaryTree pHead, int CurrentValue, int ExpectedValue, Vector<BinaryTree> vector)    
    {
        if (pHead == null)
        {
            return ;
        }
        vector.add(pHead);
        CurrentValue = CurrentValue + pHead.data;
        if (pHead.rchild== null && pHead.lchild == null && CurrentValue == ExpectedValue)
        {
            for (int i = 0; i < vector.size(); i++)
            {
                System.out.print(vector.get(i).data + " ");
            }
            System.out.println();
        }
        FindPathEqualsToExpectedValue(pHead.lchild, CurrentValue, ExpectedValue, vector);
        FindPathEqualsToExpectedValue(pHead.rchild, CurrentValue, ExpectedValue, vector);
        
        CurrentValue = CurrentValue - pHead.data;
        vector.remove(vector.size()-1);
        
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值