二叉树的中序遍历、递归实现、非递归实现、层次遍历、二叉树的应用,来来来,都有都有

本文详细介绍了二叉树的多种遍历方法,包括递归与非递归的中序遍历、层次遍历及求解二叉树的高度等。此外还提供了输出特定视图(如左视图和顶部视图)的方法,并展示了如何通过前序序列构建二叉树。
/* you only have to complete the function given below.  
Node is defined as  

class Node {
    int data;
    Node left;
    Node right;
}
*/


//递归实现中序遍历

void inOrder(Node root) {
    if(root == null){
        return ;
    }else{
        inOrder(root.left);
        System.out.print(root.data+" ");
        inOrder(root.right);
    }
}

//非递归实现中序遍历

/*
current表示当前节点,并且指向栈顶,初始时指向root,root进栈
1.只要栈current有左孩子,左孩子进栈
2.否则,输出当前栈顶元素并且判断,当前栈顶元素是否有右孩子?
3.如果没有右孩子,弹出栈顶元素,直到找到有右孩子的节点,并且将该节点=current,进栈
只要栈不为空,重复1、2、3

*/

void inOrder(Node root) {
    if(root == null){
        return;
    }
    Node current = root;
    Stack<Node> stack = new Stack<Node>();
    stack.push(current);
    while(!stack.isEmpty()){
        //有左孩子,左孩子进站
        while(current.left != null){
            current = current.left;
            stack.push(current);
        }
        //弹出栈顶元素,直到找到右孩子
        while(current.right == null){
            if(!stack.isEmpty()){
                //pop的时候要再次检测栈是否为空
                current = stack.pop();
                System.out.print(current.data+" ");
            }else{
                return;
            }
        }
        current = current.right;
        stack.push(current);
    }
}


//层次遍历二叉树,利用队列暂存节点
/*只要队列不为空,就扫描队列,取出节点值并输出,只要该节点存在左孩子或者右孩子,就将其孩子进队*/
   void LevelOrder(Node root)
    {
       Queue<Node> queue = new LinkedList<Node>();
       queue.add(root);
       while(!queue.isEmpty()){
           Node node = queue.poll();
           System.out.print(node.data+" ");
           if(node.left != null){
               queue.add(node.left);
           }
           if(node.right != null){
               queue.add(node.right);
           }
       }

    }

//二叉树的应用之————输出树的最高高度值
//递归实现:输出左子树和右子树中高度最高者
   int longest(Node root){
       if(root ==  null){
           return 0;
       }else{
           return  Math.max((longest(root.left)+1),(longest(root.right)+1));
       }
   }
   int height(Node root)
    {
       //减去1,只是表示除去root那一层
       return longest(root)-1;
    }


//二叉树的应用之————从左到右,从上到下,只输出左子树的最左边的分支和右子树//的最右边的分支
void left_view(Node root){
    if(root == null){
        return ;
    }
    left_view(root.left);
    System.out.print(root.data+" ");
}
void right_view(Node root){
    if(root == null){
        return;
    }
    System.out.print(root.data+" ");
    right_view(root.right);
}
void top_view(Node root){
    left_view(root.left);
    System.out.print(root.data+" ");
    right_view(root.right);

}

//再附加一个二叉树的创建吧——输入一个前序序列,然后用递归创建,和前序遍历类似
//这里是摘录网上的代码,但只要我们理解思想就好了
     public void createBiTree(){
            Scanner scn = null;

            try {
              scn = new Scanner(new File("input.txt"));
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            }

            this.root = createBiTree(root, scn);
          }
          private Node<T> createBiTree(Node<T> node, Scanner scn) {

            String temp = scn.next();
            if(temp.trim().equals("#")){
              return null;
            }
            else{
              node = new Node<T>((T)temp);
              node.setLeft(createBiTree(node.getLeft(), scn));
              node.setRight(createBiTree(node.getRight(), scn));
              return node;
            }
          }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值