二叉树的建立,及相关遍历

/**
 * 二叉树的建立(广度优先遍历)
 */
package com.wang.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class ErCharTree<T> {
    // 建立内部类作为二叉树的节点
    private class ErCharTreeNode {
        T data;
        ErCharTreeNode llink, rlink;

        public ErCharTreeNode(T data) {
            this.data = data;
            llink = null;
            rlink = null;
        }
    }

    ErCharTreeNode root = null;

    public ErCharTreeNode getRoot() {
        return root;
    }

    @SuppressWarnings("unchecked")
    public void create() {
        /* 建立根节点 */
        Scanner scan = new Scanner(System.in);
        ErCharTreeNode q = new ErCharTreeNode((T) new Integer(scan.nextInt()));
        Queue<ErCharTreeNode> queue = new LinkedList<ErCharTreeNode>();
        queue.offer(q);
        root = q;
        while (!queue.isEmpty()) {
            ErCharTreeNode p = queue.poll();
            int num = scan.nextInt();
            if ((Integer) num == -1) {
                break;
            }
            if (p.llink == null) {
                q = new ErCharTreeNode((T) new Integer(num));
                p.llink = q;
                queue.offer(q);
            }
            int num1 = scan.nextInt();
            if ((Integer) num1 == -1) {
                break;
            }
            if (p.rlink == null) {
                q = new ErCharTreeNode((T) new Integer(num1));
                p.rlink = q;
                queue.offer(q);
            }
        }

    }

    public void print() {
        System.out.print("广度遍历顺序:");
        Queue<ErCharTreeNode> queue = new LinkedList<ErCharTreeNode>();
        if (root != null) {
            ErCharTreeNode q = root;
            System.out.print(q.data + " ");
            queue.offer(q);
        }
        while (!queue.isEmpty()) {
            ErCharTreeNode p = queue.poll();
            if (p.llink != null) {
                System.out.print(p.llink.data + " ");
                ErCharTreeNode q = p.llink;
                queue.offer(q);
            }
            if (p.rlink != null) {
                System.out.print(p.rlink.data + " ");
                ErCharTreeNode q = p.rlink;
                queue.offer(q);
            }
        }
        System.out.println();
    }

    // 先序遍历非递归算法(用栈)
    public void perOrder(ErCharTreeNode root) {
        System.out.print("先序遍历顺序:");
        Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
        if (root != null) {
            stack.push(root);
        }
        while (!stack.isEmpty()) {
            ErCharTreeNode p = stack.pop();
            System.out.print(p.data + " ");
            if (p.rlink != null) {
                stack.push(p.rlink);
            }
            if (p.llink != null) {
                stack.push(p.llink);
            }
        }
        System.out.println();
    }

    // 中序遍历非递归算法
    public void midOrder1(ErCharTreeNode root) {
        System.out.print("中序遍历顺序:");
        ErCharTreeNode p = root;
        Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
        while (p != null) {
            while (p != null) {
                if (p.rlink != null) {
                    stack.push(p.rlink);
                }
                stack.push(p);
                p = p.llink;
            }
            p = stack.pop();
            while (!stack.isEmpty() && p.rlink == null) {
                System.out.print(p.data + " ");
                p = stack.pop();
            }
            System.out.print(p.data + " ");
            if (!stack.isEmpty()) {
                p = stack.pop();
            } else {
                p = null;
            }
        }
        System.out.println();
    }

    // 后序遍历非递归算法
    void postOrder(ErCharTreeNode p) {
        System.out.print("后序遍历顺序:");
        Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
        ErCharTreeNode prev = p;
        while (p != null || !stack.isEmpty()) {
            while (p != null) {
                stack.push(p);
                p = p.llink;
            }
            if (!stack.isEmpty()) {
                ErCharTreeNode temp = stack.peek().rlink;
                if (temp == null || temp == prev) {
                    p = stack.pop();
                    System.out.print(p.data + " ");
                    prev = p;
                    p = null;
                } else {
                    p = temp;
                }
            }
        }
    }

    // 先序遍历递归算法
    // public void perOrder1(ErCharTreeNode root) {
    // if (root != null) {
    // System.out.print(root.data + " ");
    // perOrder1(root.llink);
    // perOrder1(root.rlink);
    // }
    // }
    // 中序遍历递归算法
    // public void midOrder(ErCharTreeNode root) {
    // if (root != null) {
    // midOrder(root.llink);
    // System.out.print(root.data + " ");
    // midOrder(root.rlink);
    // }
    // }

    public static void main(String[] args) {
        ErCharTree<Integer> tree = new ErCharTree<Integer>();
        tree.create();
        tree.print();
        // tree.perOrder1(tree.getRoot());
        tree.perOrder(tree.getRoot());
        // tree.midOrder(tree.getRoot());
        tree.midOrder1(tree.getRoot());
        tree.postOrder(tree.getRoot());

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值