二叉树的遍历

public BTNode(int data, Node lchild, Node rchild){  
        this.data = data;  
        this.lchild= lchild;  
        this.rchild= rchild;  
    }  

1、二叉树的先序遍历

递归

 public void preOrder(BTNode root) {   
        printNode(root);  
        if (root.lchild() != null) {  
            preOrder(root.lchild());  
        }  
        if (root.rchild() != null) {  
            preOrder(root.rchild());  
        }  
    }  

非递归

public void preOrder1(BTNode Node)
    {
        Stack stack = new Stack<>();
        while(Node != null || !stack.empty())
        {
            while(Node != null)
            {
                System.out.print(Node.data + "   ");
                stack.push(Node);
                Node = Node.lchild;
            }
            if(!stack.empty())
            {
                Node = stack.pop();
                Node = Node.rchild;
            }
        }
    }

2、二叉树的中序遍历

递归

public void inOrder(BTNode root) {          
        if (root.lchild() != null) {  
            inOrder(root.lchild());  
        }  
        printNode(root); 
        if (root.rchild() != null) {  
            inOrder(root.rchild());  
        }  
    }  

非递归

void InOder(BTNode root){
if(root == null){
   return;
}
BTNode p=root;
Stack s=new Stack;
while(!s.empty || p){
  if(p){
    s.push(p);
    p=p.lchild;
  }else{
    p=s.top();
    s.pop();
    System.out.print(p.data);
    p=p.rchild;
  }
}
}

3、二叉树的后序遍历

递归

public void postOrder(BTNode root) {          
        if (root.lchild() != null) {  
            postOrder(root.lchild());  
        }  
        if (root.rchild() != null) {  
            postOrder(root.rchild());  
        }  
       printNode(root); 
    }  

非递归

public void postOrder(BTNode Node)
    {
        Stack stack1 = new Stack<>();
        Stack stack2 = new Stack<>();
        int i = 1;
        while(Node != null || !stack1.empty())
        {
            while (Node != null)
            {
                stack1.push(Node);
                stack2.push(0);
                Node = Node.lchild;
            }

            while(!stack1.empty() && stack2.top() == i)
            {
                stack2.pop();
                System.out.print(stack1.pop().data+ "   ");
            }

            if(!stack1.empty())
            {
                stack2.pop();
                stack2.push(1);
                Node = stack1.top();
                Node = Node.rchild;
            }
        }
    }

4、二叉树的层序遍历

递归

 public void levelOrder(BTNode Node) {
        if (Node == null) {
            return;
        }

        int depth = depth(Node);

        for (int i = 1; i <= depth; i++) {
            levelOrder(Node, i);
        }
    }

    private void levelOrder(BTNode Node, int level) {
        if (Node == null || level < 1) {
            return;
        }

        if (level == 1) {
            System.out.print(Node.data + "  ");
            return;
        }
        // 左子树
        levelOrder(Node.lchild, level - 1);
        // 右子树
        levelOrder(Node.rchild, level - 1);
    }

    public int depth(BTNode Node) {
        if (Node == null) {
            return 0;
        }

        int l = depth(Node.lchild);
        int r = depth(Node.rchild);
        if (l > r) {
            return l + 1;
        } else {
            return r + 1;
        }
    }

非递归

public void levelOrder(BTNode Node) {
        if (Node == null) {
            return;
        }

        BTNode binaryNode;
        Queue queue = new LinkedList<>();
        queue.add(Node);

        while (queue.size() != 0) {
            binaryNode = queue.poll();

            System.out.print(binaryNode.data+ "  ");

            if (binaryNode.lchild!= null) {
                queue.offer(binaryNode.lchild);
            }
            if (binaryNode.rchild!= null) {
                queue.offer(binaryNode.rchild);
            }
        }
    }

你可能感兴趣的:(数据结构)