두 갈래 나무는 차례차례 총결산하지 않는다.

5162 단어
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);
        
    }

}

좋은 웹페이지 즐겨찾기