第十章 基本数据结构 练习 10.4-2

package chap10;

import static org.junit.Assert.*;

import java.util.Stack;

import org.junit.Test;

public class exec10_4_2 {
    static void printTree(Tree tree) {
        Root root = tree.root;
        if (root != null) {
            Node node = root.node;
            System.out.println(node.key);
            printNode(node);
        }
    }

    /**
     * 递归实现
     * 
     * @param node
     */
    static void printNode(Node node) {

        if (node == null)
            return;
        else {
            Node left = node.left;
            Node right = node.right;
            printLeft(left);
            printLeft(right);
            printNode(left);
            printNode(right);
        }

    }

    static void printLeft(Node n) {
        if (n != null)
            System.out.println(n.key);
    }

    static void printRight(Node n) {
        if (n != null)
            System.out.println(n.key);
    }

    static Tree creatTree() {
        Tree t = new Tree();
        Root r = new Root();
        Node n1, n2, n3, n4, n5, n6, n7, n8, n9, n0;
        n1 = new Node(0);
        n2 = new Node(1);
        n3 = new Node(2);
        n4 = new Node(3);
        n5 = new Node(4);
        n6 = new Node(5);
        n7 = new Node(6);
        n8 = new Node(7);
        n9 = new Node(8);
        n0 = new Node(9);
        t.root = r;
        r.node = n0;
        n0.left = n1;
        n0.right = n2;
        n1.left = n3;
        n1.right = n4;
        n2.left = n5;
        n2.right = n6;
        n3.left = n7;
        n4.left = n8;
        n5.right = n9;

        return t;
    }

    @Test
    public void testName() throws Exception {
        Tree t1 = creatTree();
        printTree(t1);
    }
}
posted @ 2014-06-11 22:58  JintaoXIAO  阅读(181)  评论(0编辑  收藏  举报