Download presentation

Presentation is loading. Please wait.

Published byRichard Grant Modified over 2 years ago

1
1 Trees © Dave Bockus

2
2 Linked Representation of Trees b a c d e f g a cb fed g

3
3 Null Pointers in a Binary Tree b a c d e f g Null pointers in a Binary = number of nodes + 1

4
4 N-ary Trees General Case of all trees. Vertex can have –Fixed number of siblings –Variable number of siblings Applications include –Game trees, Chess, Checkers, Go [Board Games] –Organizational Structures, hierarchies –Directory Structures

5
5 N-ary Tree Example - Chess List Board List Board List Board List Board

6
6 Breadth First Search of Binary Tree b a c d e g f TQ is a Queue of Tnode; Print_Breadth (Tnode T){ TQ.Enter(T); while (!TQ.Is_Empty()){ Temp = TQ.Remove(); Print(temp); if (temp.LT!=null) TQ.Enter(temp.LT); if (temp.RT!=null) TQ.Enter(temp.RT); } TQ Output a a c b de c f de g fg b

7
7 General Case Pre-Order Traversal Based on N-ary Tree Node public void Preprint(Node ptr) { Node tptr = List; Print(ptr.data); while (tptr != null){ Preprint(tptr.down); tptr = tptr.next; } On entry tptr is initialized to List which contains links to the sibling nodes. Data is printed or processed Recursive Preprint on sibling until tptr hits end of List

8
8 Recursive Pre-Order Traversal - Binary Tree PrePrint(TreeType Ptr) { System.out.println(Ptr.data); if (Ptr.left!= null) PrePrint(Ptr.left); if (Ptr.right!=null) PrePrint(Ptr.right); } Go Left Go Right Visit Node Nodes are recursively processed –Enumerate or Visit –Left Branch Second –Right Branch Last VLR

9
9 d i c a e h f b g m Pre-Order Traversal of a Binary Tree

10
10 General Case Post-Order Traversal Based on N-ary Tree Node public void Postprint(Node ptr) { Node tptr = List; while (tptr != null){ Postprint(tptr.down); tptr = tptr.next; } Print(ptr.data); } On entry tptr is initialized to List which contains links to the sibling nodes. Recursive Postprint on sibling until tptr hits end of List. Data is printed after siblings have been processed.

11
11 Recursive Post-Order Traversal - Binary Tree PostPrint(TreeType Ptr) { if (Ptr.left!= null) PostPrint(Ptr.left); if (Ptr.right!=null) PostPrint(Ptr.right); System.out.println(Ptr.data); } Go Left Go Right Visit Node Nodes are recursively processed –Left Branch First –Right Branch Second –Enumerate or Visit LRV

12
12 d i c a e h f b g m LRV Post-Order Traversal of a Binary Tree

13
13 Recursive In-Order Traversal - Binary Tree InorderPrint(TreeType Ptr) { if (Ptr.left!= null) InorderPrint(Ptr.left); System.out.println(Ptr.data); if (Ptr.right!=null) InorderPrint(Ptr.right); } Go Left Go Right Visit Node Nodes are recursively processed –Left Branch First –Enumerate or Visit –Right Branch Last LVR

14
14 LVR d i c a e h f b g m In-Order Traversal of a Binary Tree

15
15 In Order Threading d i c a e h f b g m Right null pointers point to the successor Left null pointer point to the predecessor.

16
16 Successor Algorithm 1) ptr = ptr.rt; 2) if (T.is_thread(ptr) || ptr == null) stop 3) while (!T.is_thread(ptr.lt)) ptr = ptr.lt; Right Once If we followed a thread then we found the successor Check to see if we hit the end Go Left until we hit a thread.

17
17 Traversing a Threaded Tree - SOT class node{ {some data} public node left; public node right; boolean ltag,rtag; } public static Inorder_T(node ptr) { node tptr; while (!ptr.ltag) ptr = ptr.left; while (ptr != null) { //visit the node tptr = ptr; ptr = ptr.right; if (!tptr.rtag) while (!ptr.ltag) ptr = ptr.left; } Go to far left Node Successor Algorithm

18
18 Threading a Tree Which is Built for SOT node last_right = null; public static void Thread(node ptr) { if (ptr.left != null) { Thread(ptr.left); //Visit Node if (last_right != null) { last_right.right = ptr; last_right.rtag = true; last_right = null; } if (ptr.right != null) Thread(ptr.right); else last_right = ptr; }

19
19 Threading a Tree as it is Built Input: i h b m e c f a d k i h b m e c f a d k

20
20 Link Inversion on Lists- Going Down D A B C Head PresPrev tmp = pres.next pres.next = prev prev = pres pres = tmp tmpPrevPrestmp Prev Pres tmp Prev Pres tmp Prev Pres

21
21 Link Inversion on Lists - Coming Back D A B C Head Points Prev & Pres are reversed tmp = pres.next pres.next = prev prev = pres pres = tmp PresPrevtmp Prev Pres tmp Prev Pres tmp Prev Pres tmp Prev Pres

22
22 a cb fed g Link Inversion with Trees Prev Pres Only Tag=1 is shown, those not present are zero Temp Pres Prev Temp Prev Pres Temp PresPrev Temp Tag = 1 Prev Pres Temp PresPrev Temp PresPrev Temp Tag = 1 Pres Prev Temp Prev Pres Temp Tag = 1 Prev Pres Temp Pres Prev Temp Pres Prev Temp PresPrev

23
23 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k

Similar presentations

OK

Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,

Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on human nutrition and digestion for kids Ppt on rain detector alarm Ppt on logic gates class 12 Pdf to ppt online tool Ppt on diode characteristics curve Ppt on 9/11 conspiracy theory By appt only movie page Ppt on thermal conductivity of insulating powder Ppt on land pollution Ppt on means of transport for class 2