7 Trees 1 By Juthawut Chantharamalee Data Structure
บทท 7 ทร (Trees) )สวนท 1( By Juthawut Chantharamalee วชาโครงสรางขอมล (Data Structure) รหส 4122202 1
Root Level 0 A Bran ch A Level 1 B E F F nch Bra FI Level 2 C D รปท G H I 7. 1 ทร (Trees) วชาโครงสรางขอมล (Data Structure) รหส 4122202 3
A Subtree B B E F Root of Subtree I C D รปท G 7. 2 ซบทร วชาโครงสรางขอมล H I (Subtree) (Data Structure) รหส 4122202 4
Computer Case . . . Controller CPU 3. 5”DISK ALU (a) แบบโครงสรางทรทวไป CD-ROM . . . ROM (Indent List( List Computer case CPU Controller ALU ROM) 3. 5 “DISK(…) CD-ROM )b) แบบยอหนา (Indent List( List Computer ((case(…) CPU(Controller ALU … ROM) 3. 5 “DISK(…) … CD-ROM(…)” ) รปท )c) แบบวงเลบ (Parenthetical List( List 7. 3 การนำเสนอสวนประกอบของคอมพวเตอรในรปแบตางๆ วชาโครงสรางขอมล (Data Structure) รหส 4122202 7
Algorithm Convert. To. Paren (root, output) Convert a general tree to parenthetical notation. Pre root is a pointer to a tree node Post output contains parenthetical notation 1 Place root in output 2 if (root is a parent) 1 Place an open parenthesis in the output 2 Convert. To. Paren (root's first child) 3 loop (more siblings) 1 Convert. To. Paren (root's next child) 4 end loop 5 place close parenthesis in the output 3 end if 4 return end Convert. To. Paren อกอรทมท 7. 1 การแปลงโครงสรางทรแบบทวไปมาเปนวงเลบ วชาโครงสรางขอมล (Data Structure) รหส 4122202 8
HL = 0 A B HL = 2 D C A B HR = 1 C E HR = 2 Balance Factor = 1 รปท Balance Factor = -2 7. 6 ไบนารทรรปแบบตาง วชาโครงสรางขอมล (Data Structure) รหส 4122202 17
Algorithm Pre. Order (root) Traverse a binary tree in node-left-right sequence. Pre root is the entry node of a tree or subtree Post each node has been processed in order 1 if (root is not null) 1 process (root) 2 pre. Order (left. Subtree) 3 pre. Order (right. Subtree) 2 end if end pre. Order อกอรทมท 7. 2 การทองไปยงไบนารทรแบบ วชาโครงสรางขอมล Preorder (Data Structure) รหส 4122202 29
Algorithm in. Order (root) Traverse a jbinary tree in left-node-right sequence. Pre root is the entry node of a tree or subtree Post each node has been processed in order 1 if (root is not null) 1 in. Order (left. Sub. Tree) 2 process (root) 3 in. Order (right. Sub. Tree) 2 end if end in. Order อกอรทมท 7. 3 การทองไปยงไบนารทรแบบ วชาโครงสรางขอมล Inorder (Data Structure) รหส 4122202 31
Algorithm post. Order (root) Traverse a binary tree in left-right-node sequence. Pre root is the entry node of a tree or subtree Post each node has. been processed in order 1 if (root is not null) 1 post. Order (left. Sub. Tree) 2 post. Order (right. Subtree) 3 process (root) 2 end if end post. Order อกอรทมท 7. 4 การทองไปยงไบนารทรแบบ วชาโครงสรางขอมล Postorder (Data Structure) รหส 4122202 33
Preorder “Walking” order A A B C B D D A B E CD F รปท 7. 13 การทองไปยงไบนารทรดวยวธ First Traversals วชาโครงสรางขอมล Breadth- (Data Structure) รหส 4122202 38
Algorithm breadth. First (root) Process tree using breadth-first traversal, Pre root is node to be processed Post tree has been processed 1 set current. Node to root 2 create. Queue (bf. Queue) 3 loop (current. Node not null) 1 process (current. Node) 2 if (left subtree not null) 1 enqueue (bf. Queue, leftsubtree) 3 end if 4 if (right subtree not null) 1 enqueue (bf Queue, right-Subtree) 5 end if 6 if (not empty. Queue(bf. Queue)) 1 set current. Node to dequeue (bf. Queue) 7 else 1 set current. Node to null 8 end if 4 end loop 5 destroy. Queue (bf. Queue) end breadth. First อกอรทมท 7. 5 การทองไปยงไบนารทรแบบ วชาโครงสรางขอมล Breath-First (Data Structure) รหส 4122202 40
Algorithm infix (tree) Print the infix expression for an expression tree. Pre tree is a pointer to an expression tree Post the infix expression has been printed 1 if (tree not empty) 1 if (tree token is an operand) 1 print (tree-token) 2 else 1 print (open parenthesis) 2 infix (tree left subtree) 3 print (tree token) 4 infix (tree right subtree) 5 print (close parenthesis) 3 end if 2 end if end infix อกอรทมท 7. 6 การทองไปยงในเอกซเพรสชนทรแบบ วชาโครงสรางขอมล (Data Structure) รหส 4122202 Infix 46
Algorithm postfix (tree) Print the postfix expression for an expression tree. Pre tree is a pointer to an egression tree Post the postfix expression has been printed 1 if (tree not empty) 1 postfix (tree left subtree) 2 postfix (tree right subtree) 3 print (tree token) 2 end if end postfix อกอรทมท 7. 7 การทองไปยงในเอกซเพรสชนทรแบบ วชาโครงสรางขอมล (Data Structure) รหส 4122202 Postfix 48
Algorithm prefix (tree) Print the prefix expression for an expression tree. Pre tree is a pointer to an expression tree Post the prefix expression has been printed 1 if (tree not empty) 1 print (tree token) 2 prefix (tree left subtree) 3 prefix (tree right subtree) 2 end if end prefix อกอรทมท 7. 8 การทองไปยงในเอกซเพรสชนทรแบบ วชาโครงสรางขอมล (Data Structure) รหส 4122202 Prefix 50
The End Lesson 7 (Session 1) วชาโครงสรางขอมล (Data Structure) รหส 4122201 51
- Slides: 51