Java建立非遞歸實現二叉排序樹並實現非遞歸,先序,中序,後序遍歷

題目描述
輸入一系列整數,建立二叉排序樹,並進行前序,中序,後序遍歷。
輸入描述:
輸入第一行包括一個整數n(1<=n<=100)。
接下來的一行包括n個整數。
輸出描述:
可能有多組測試數據,對於每組數據,將題目所給數據建立一個二叉排序樹,並對二叉排序樹進行前序、中序和後序遍歷。
每種遍歷結果輸出一行。每行最後一個數據之後有一個空格。

輸入中可能有重複元素,但是輸出的二叉樹遍歷序列中重複元素不用輸出。
示例1
輸入
5
1 6 5 9 8
輸出
1 6 5 9 8
1 5 6 8 9
5 8 9 6 1

package com.echo;

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

class BinaryTreeNode {
    int weight;
    BinaryTreeNode leftChild;
    BinaryTreeNode rightChild;

    public BinaryTreeNode() {

    }

    public BinaryTreeNode(int weight, BinaryTreeNode leftChild, BinaryTreeNode rightChild) {
        this.weight = weight;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }
}

public class BinarySortTree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            BinaryTreeNode bt = null;
            while (n > 0) {
                int weight = scanner.nextInt();
                bt = createSortTree(bt, weight);
                n--;
            }
            preorder(bt);
            System.out.println();
            inorder(bt);
            System.out.println();
            postorder(bt);
            System.out.println();
        }

    }

    public static BinaryTreeNode createSortTree(BinaryTreeNode root, int weight) {
        BinaryTreeNode node = new BinaryTreeNode(weight, null, null);
        if (root == null) {
            root = node;
        } else {
            BinaryTreeNode currentNode = root;
            BinaryTreeNode parentNode;
            while (true) {
                //記錄父節點
                parentNode = currentNode;
                if (weight > parentNode.weight) {
                    //當前節點往右走
                    currentNode = currentNode.rightChild;
                    if (currentNode == null) {
                        //走到頭了
                        parentNode.rightChild = node;
                        return root;
                    }
                }
                if (weight < parentNode.weight) {
                    currentNode = currentNode.leftChild;
                    if (currentNode == null) {
                        parentNode.leftChild = node;
                        return root;
                    }
                }
                if (weight == parentNode.weight) {
                    return root;
                }
            }
        }
        return root;
    }

    public static void inorder(BinaryTreeNode root) {
        Stack<BinaryTreeNode> stack = new Stack<>();
        BinaryTreeNode node = root;
        while (!stack.empty() || node != null) {
            if (node != null) {
                //左,壓棧一次就是往左走一次
                stack.push(node);
                node = node.leftChild;
            } else {
                //左走到頭了 該根了
                node = stack.pop();
                System.out.print(node.weight + " ");
                //右
                node = node.rightChild;
            }
        }
    }

    public static void preorder(BinaryTreeNode root) {
        Stack<BinaryTreeNode> stack = new Stack<>();
        BinaryTreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            if (node != null) {
                //根
                System.out.print(node.weight + " ");
                stack.push(node);
                //左
                node = node.leftChild;
            } else {
                //右
                node = stack.pop();
                node = node.rightChild;
            }
        }
    }

    public static void postorder(BinaryTreeNode root) {
        BinaryTreeNode node = root;
        BinaryTreeNode preNode = null; //記錄上一次輸出的節點
        Stack<BinaryTreeNode> stack = new Stack<>();
        stack.push(node);   //根入棧
        while (!stack.isEmpty()) {
            node = stack.peek();
            if ((node.leftChild == null && node.rightChild == null) ||
                    (preNode != null && (preNode == node.leftChild || preNode == node.rightChild))) {
                //結點的左右孩子都爲空,證明遍歷到了葉子上
                //或者前驅的記錄不爲空,且前驅節點是當前節點的左孩子或者右孩子
                System.out.print(node.weight + " ");
                preNode = node;
                stack.pop();
            }else{
                if (node.rightChild != null) {
                    stack.push(node.rightChild);
                }

                if (node.leftChild != null) {
                    stack.push(node.leftChild);
                }

            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章