90 likes | 178 Views
Ics202 Data Structures. A. C. B. D. E. F. G. The OutPut : PreOrder Traversal A B D E C F G InOrder Traversal D B E A F C G PostOrder Traversal D E B F G C A Breadth Traversal A B C D E F G.
E N D
A C B D E F G • The OutPut: • PreOrder Traversal • A • B • D • E • C • F • G • InOrder Traversal • D • B • E • A • F • C • G • PostOrder Traversal • D • E • B • F • G • C • A • Breadth Traversal • A • B • C • D • E • F • G import ADTs.*; class BinaryTreeTest { public static void main(String[] args) { BinaryTree D = newBinaryTree(new Chr('D'));BinaryTree E = newBinaryTree(new Chr('E'));BinaryTree B = newBinaryTree(new Chr('B'),D,E);BinaryTree F = newBinaryTree(new Chr('F'));BinaryTree G = newBinaryTree(new Chr('G'));BinaryTree C = newBinaryTree(new Chr('C'),F,G);BinaryTree A = (BinaryTree) newBinaryTree(new Chr('A'),B,C); Visitor v = newChrPrintingVisitor();System.out.println("PreOrder Traversal");A.depthFirstTraversal(newPreOrder(v));System.out.println("InOrder Traversal"); A.depthFirstTraversal(newInOrder(v)); System.out.println("PostOrder Traversal");A.depthFirstTraversal(newPostOrder(v)); System.out.println("Breadth Traversal");A.breadthFirstTraversal(v); } }
1. Depth-First Traversal i.Preorder Traversal ii.Inorder Traversal iv.Postorder Traversal 2. Breadth-First Traversal
1. Depth-First Traversal Preorder Traversal{ root , left , right }
2. Breadth-First Traversal Breadth-first Traversal
QuestionsImplement the general tree using the example below (test the different kinds of traversals).
import ADTs.*;public class GeneralTreeTest { public static void main(String[] args) { GeneralTree D = new GeneralTree(new Chr('D')); // Complete for other GeneralTreeH.attachSubtree(I);J.attachSubtree(K);J.attachSubtree(L); // Complete for other subtreeVisitor v = new ChrPrintingVisitor();System.out.println("PreOrder Traversal");D.depthFirstTraversal(new PreOrder(v));System.out.println("PostOrder Traversal");D.depthFirstTraversal(new PostOrder(v));System.out.println("Breadth First Traversal");D.breadthFirstTraversal(v); }}