您的位置:首页 > 理论基础 > 数据结构算法

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

2017-01-02 00:00 134 查看
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); } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: