第十章 基本数据结构 练习 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); } }
相关文章推荐
- 第十章 基本数据结构 练习 10.4-3
- 第十章 基本数据结构 练习 10.4-3
- 第十章 基本数据结构 练习 10.4-4
- 第十章 基本数据结构 练习 10.4-2
- 第十章 基本数据结构 练习 10.4-4
- 算法导论 第十章 基本数据结构 练习10.1-2
- 《算法导论》第十章----基本数据结构
- 算法练习:判断一个字符串中的字符是否唯一(只用基本数据结构)
- 算法导论第十章基本数据结构
- 算法练习:检测一个int(整型)数组中是否存在相同的数(只用基本数据结构)
- 第十章 基本数据结构
- 第十章 基本数据结构
- 算法导论 第十章:基本数据结构
- 算法导论详解(8) 第十章 基本数据结构
- 算法导论 第十章 基本数据结构
- 算法导论 第十章 基本数据结构
- 算法导论第十章 基本数据结构实现(栈,队列,链表),课后题答案
- 《算法导论》第十章——基本数据结构(一):栈与队列
- 第十章 基本数据结构
- 第十章 基本数据结构——栈和队列