查找二叉树的建立与遍历方式

package learn.java.com.java.datastru;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class BinarySearchTree {
    public static void main(String[] args){
        BinarySortTree<Integer> BST = new BinarySortTree<Integer>();
        BST.insertNode(35);

        BST.insertNode(20);
        BST.insertNode(15);
        BST.insertNode(16);
        BST.insertNode(29);
        BST.insertNode(28);
        BST.insertNode(30);
        BST.insertNode(40);
        BST.insertNode(50);
        BST.insertNode(45);
        BST.insertNode(55);
        System.out.print("先序遍历(递归):");
        BST.preRec(BST.getRoot());
        System.out.println();
        System.out.print("中序遍历(递归):");
        BST.midRec(BST.getRoot());
        System.out.println();
        System.out.print("后序遍历(递归):");
        BST.postRec(BST.getRoot());
        System.out.println();
        System.out.print("先序遍历(非递归):");
        BST.preNoRec(BST.getRoot());
        System.out.println();
        System.out.print("中序遍历(非递归):");
        BST.midNoRec(BST.getRoot());
        System.out.println();
        System.out.print("后序遍历(非递归):");
        System.out.println(BST.postNoRec(BST.getRoot()).toString());
        System.out.println();

    }
}

//结点构造
class Node<E extends Comparable<E>>{
    E nodeValue;
    Node<E> left;
    Node<E> right;
    public Node(E value){
        this.nodeValue=value;
        left=right=null;
    }

}
/*/
创建二叉查找树
 */
class BinarySortTree<E extends Comparable<E>>{
    private Node<E> root;
    BinarySortTree(){
        root=null;
    }

    public void insertNode(E value){
        if(root==null){
            root=new Node<E>(value);
            return;
        }
        Node<E> curr=root;
        while(true){
            if (value.compareTo(curr.nodeValue)>0){
                if(curr.right==null){
                    curr.right=new Node<E>(value);
                    return;
                }
                curr=curr.right;
            }
            else{
                if(curr.left==null){
                    curr.left=new Node<E>(value);
                    return;
                }
                curr=curr.left;
            }
        }
    }

    public Node<E> getRoot(){
        return root;
    }

    /*
    先序遍历二叉树(递规)
     */

    public void preRec(Node<E> root){
        if(root!=null){
            System.out.print(root.nodeValue+" ");
            preRec(root.left);
            preRec(root.right);

        }
    }


    /**
     * 中序遍历(递 规)
     */

    public void midRec(Node<E> root){
        if(root!=null){
            midRec(root.left);
            System.out.print( root.nodeValue +" ");
            midRec(root.right);
        }
    }
    /**
     * 后序遍历(递 规)
     */
    public void postRec(Node<E> root){
        if(root!=null){
            postRec(root.left);
            postRec(root.right);
            System.out.print(root.nodeValue+" ");

        }
    }

    /**
     * 先序非递规
     */
    public void preNoRec(Node<E> root){
        Stack<Node<E>> stack=new Stack<Node<E>>();
        Node<E> p=root;
        if(p==null)
            return;
        stack.push(p);
        while(!stack.isEmpty()){
            p=(Node<E>)stack.pop();
            System.out.print(p.nodeValue+" ");
            if(p.right!=null)
                stack.push(p.right);
            if(p.left!=null)
                stack.push(p.left);
        }
    }
    /**
     * 中序非递 规
     */
    public void midNoRec(Node<E> root){
        Stack<Node<E>> stack =new Stack<Node<E>>();
        Node<E> curr=root;
        while(curr!=null||!stack.isEmpty()){
            while(curr!=null){
                stack.push(curr);
                curr=curr.left;
            }
            curr=stack.pop();
            System.out.print(curr.nodeValue+" ");
            curr=curr.right;
        }
    }
    /**
     * 后序遍历非递规
     */

    public List<E> postNoRec(Node<E> root){
        Stack<Node>  stack =new Stack<Node>();
        LinkedList<E> resul=new LinkedList<E>();
        Node<E> p=root;
        if (root==null)
            return null ;
        stack.push(p);
        while(!stack.isEmpty()){
            p=stack.pop();
            resul.add(p.nodeValue);
            if(p.left!=null)
                stack.push(p.left);
            if(p.right!=null)
                stack.push(p.right);
        }
        Collections.reverse(resul);
        return resul;
    }
}

先序遍历(递归):35 20 15 16 29 28 30 40 50 45 55
中序遍历(递归):15 16 20 28 29 30 35 40 45 50 55
后序遍历(递归):16 15 28 30 29 20 45 55 50 40 35
先序遍历(非递归):35 20 15 16 29 28 30 40 50 45 55
中序遍历(非递归):15 16 20 28 29 30 35 40 45 50 55
后序遍历(非递归):[16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]
参考:https://www.jb51.net/article/140486.htm

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值