二叉排序树的实现

二叉排序树。它或者是一个空树,或者是一个具有下列性质的二叉树:

  • 若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值
  • 若它的右子树不空,则右子树上所有结点的值均大于它的根节点的值
  • 它的左、右子树也分别是二叉排序树

简单实现了二叉树的构建,中序遍历,先序遍历,后序遍历:代码纯手工,还请大佬多指导:

/**
 *  节点定义 数据,左节点,右节点
 */
class Node {
    public int data;
    public Node left;
    public Node right;
    public Node(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

public class BinaryTree {
    private Node root;
    public BinaryTree() {root = null;}

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        int[] data = {4,8,7,3,2,1,9,5,21};
        binaryTree.buildTree(data);
        System.out.print("二叉树中序遍历:");
        binaryTree.inOrder();
        System.out.print("二叉树先序遍历");
        binaryTree.preOrder();
        System.out.print("二叉树后序遍历");
        binaryTree.postOrder();
        
        System.out.print("\n层序遍历树:");
        binaryTree.layerTranverse();
    }

    //将数值输入构建二叉树
    public void buildTree(int[] data) {
        for(int i = 0; i < data.length; i++) {
            this.insert(data[i]);
        }
    }
    //新数据的插入
    public void insert(int data) {
        Node newNode = new Node(data);
        if(root == null ) {//空树 赋值root
            root = newNode;
        } else {
            Node current = root; //重根元素开始遍历
            Node parent; // 临时变量
            while(true) { //寻找插入位置
                parent = current;
                if(data < current.data) {
                    current = current.left;
                    if(current == null) {
                        parent.left = newNode;
                        return;
                    }
                } else {
                    current = current.right;
                    if(current == null) {
                        parent.right = newNode;
                        return;
                    }
                }
            }
        }
    }

    //中序遍历树 左-根-右
    public void inOrder(Node localRoot) {
        if(localRoot != null) {
            inOrder(localRoot.left);
            System.out.print(localRoot.data+" _ ");
            inOrder(localRoot.right);
        }
    }
    public void inOrder() {
        this.inOrder(this.root);
    }
    //先序遍历树 根-左-右
    public void preOrder(Node localRoot) {
        if(localRoot != null) {
            System.out.print(localRoot.data+" _ ");
            preOrder(localRoot.left);
            preOrder(localRoot.right);
        }
    }
    public void preOrder() {
        this.preOrder(this.root);
    }
    //后序遍历树 左-右-根
    public void postOrder(Node localRoot) {
        if(localRoot != null) {
            postOrder(localRoot.left);
            postOrder(localRoot.right);
            System.out.print(localRoot.data+" _ ");
        }
    }
    public void postOrder() {
        this.postOrder(this.root);
    }
    
  /**
 * 层序遍历二叉树
 * 先将根节点放入队列中,然后每次都从队列中取出一个节点打印;
 * 若这个节点有子节点,则将它放入队列尾,直到队列为空。
 */
  public void layerTranverse() {
      if(this.root ==null) {
          return ;
      }
      Queue <Node> q = new LinkedList<Node>();
      q.add(this.root);
      while(!q.isEmpty()) {
          Node n = q.poll();
          System.out.print(n.data + " ");
          if (n.left != null)
              q.add(n.left);
          if (n.right != null)
              q.add(n.right);
      }
  }
}

运行结果:
二叉树中序遍历:1 _ 2 _ 3 _ 4 _ 5 _ 7 _ 8 _ 9 _ 21 _ 二叉树先序遍历4 _ 3 _ 2 _ 1 _ 8 _ 7 _ 5 _ 9 _ 21 _ 二叉树后序遍历1 _ 2 _ 3 _ 5 _ 7 _ 21 _ 9 _ 8 _ 4 _
层序遍历树:4 3 8 2 7 9 1 5 21

Q: 已知先序遍历和中序遍历,如何求顺序树的后序遍历?

如果已知先序序列为ABDECF, 中序序列为DBEAFC,求解后序序列?

解题思路:根据规则可知先序:根-左-右,则先序的第一个元素A必为根节点。

中序:左-根-右,在根据根节点A,可知左子树包含DBE,右子树包含FC.

递归求解左子树(左子树的先序为:BDE,中序为DBE),右子树。得出树结构。

 

 

Q:求二叉树中结点的最大距离?

解题思路:最大距离=leftMaxDistence + rightMaxDistence

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值