JAVA二叉排序树的建立,遍历(递归,非递归)

import java.util.Scanner;
import java.util.Stack;


class Node{
    private int data;
    private Node right;
    private Node left;
    public int getData(){
        return this.data;
    }
    public Node getRight(){
        return this.right;
    }
    public Node getLeft(){
        return this.left;
    }
    public Node(int data){
        this.data=data;
    }
    public void setRight(Node right){
        this.right=right;
    }
    public void setLeft(Node left){
        this.left=left;
    }
}

public class BSTree {
    Node root;
    //构造二叉排序树
    public void Insert(int data){
        Node node = new Node(data);
        if (root==null)
            root = node;
        else{
            Node current = root;
            Node parent;
            while(true){
                parent = current;
                if(data>parent.getData())
                {
                    current = current.getRight();
                    if(current==null)
                    {
                        parent.setRight(node);
                        return;
                    }
                }
                if(dataif(current==null)
                    {
                        parent.setLeft(node);
                        return;
                    }
                }
                if(data==parent.getData())
                    return;
            }
        }
    }
    //递归前序
    public void preorder(Node localroot){
        if(localroot!=null){
            System.out.print(localroot.getData()+" ");
            preorder(localroot.getLeft());
            preorder(localroot.getRight());
        }
    }
    //非递归前序
    public void preorder2(Node localroot){
        Node node = localroot;
        Stack st = new Stack();
        while(!st.empty() || node != null){
            if(node!=null){
                System.out.print(node.getData()+" ");
                st.push(node);
                node = node.getLeft();
            }
            else{
                node = (Node) st.pop();
                node = node.getRight();
            }
        }
    }
    //递归中序
    public void inorder(Node localroot){
        if(localroot!=null){
            inorder(localroot.getLeft());
            System.out.print(localroot.getData()+" ");
            inorder(localroot.getRight());
        }
    }
    //非递归中序
    public void inorder2(Node localroot){
        Stack st = new Stack();
        Node node = localroot;
        while(!st.empty() || node!=null){
            if(node!=null){
                st.push(node);
                node = node.getLeft();
            }
            else{
                node = (Node) st.pop();
                System.out.print(node.getData()+" ");
                node = node.getRight();
            }
        }
    }
    //递归后序
    public void postorder(Node localroot){
        if(localroot!=null){
            postorder(localroot.getLeft());
            postorder(localroot.getRight());
            System.out.print(localroot.getData()+" ");
        }
    }
    public void postorder2(Node localroot){
        Node prenode = null;      //上一次输出的节点
        Stack st = new Stack();
        Node node = localroot;      //现在访问的节点
        st.push(node);    //根节点入栈
        while(!st.empty()){
            node = (Node) st.peek();
            if((node.getLeft()==null&&node.getRight()==null)||
                    (prenode!=null&&(prenode==node.getLeft()|| prenode==node.getRight()))){
                System.out.print(node.getData()+" ");
                prenode = node;
                st.pop();
            }
            else{
                if(node.getRight()!=null)
                    st.push(node.getRight());
                if(node.getLeft()!=null)
                    st.push(node.getLeft());
            }
        }
    }
    public static void main(String[] args){
        int n = 0;
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            BSTree tree = new BSTree();
            n=in.nextInt();
            for(int i=0;iin.nextInt());
            }
            tree.preorder(tree.root);
            System.out.println();
            tree.preorder2(tree.root);
            System.out.println();
            tree.inorder(tree.root);
            System.out.println();
            tree.inorder2(tree.root);
            System.out.println();
            tree.postorder(tree.root);
            System.out.println();
            tree.postorder2(tree.root);
            System.out.println();
        }
    }
}

你可能感兴趣的:(机试题)