1 / 9

Ics202 Data Structures

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.

dorjan
Download Presentation

Ics202 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ics202Data Structures

  2. 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); } }

  3. 1. Depth-First Traversal i.Preorder Traversal ii.Inorder Traversal iv.Postorder Traversal 2. Breadth-First Traversal

  4. 1. Depth-First Traversal Preorder Traversal{ root , left , right }

  5. Inorder Traversal{left, root, right}

  6. Postorder Traversal{left, right ,root}

  7. 2. Breadth-First Traversal Breadth-first Traversal

  8. QuestionsImplement the general tree using the example below (test the different kinds of traversals).

  9. 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); }}

More Related