Transcript Trees

Trees
© Dave Bockus
1
Linked Representation of Trees
a
a
b
c
b
d
e
f
d
c
e
f
g
g
2
Null Pointers in a Binary Tree
a
b
d
c
e
f
g
Null pointers in a Binary = number of nodes + 1
3
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
4
N-ary Tree Example - Chess
Board
List
Board
List
Board
List
Board
•••
List
5
Breadth First Search of Binary Tree
TQ
a
a
c
e
d
g
c
d
e
f
g
a b c d e f g
Output
b
b
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)
f
TQ.Enter(temp.LT);
if (temp.RT!=null)
TQ.Enter(temp.RT);
}
}
6
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
7
Recursive Pre-Order Traversal - Binary Tree
PrePrint(TreeType Ptr) {
Visit Node
System.out.println(Ptr.data);
if (Ptr.left!= null)
Go Left
PrePrint(Ptr.left);
if (Ptr.right!=null)
PrePrint(Ptr.right);
Go Right
}
• Nodes are recursively processed
– Enumerate or Visit
– Left Branch Second
VLR
– Right Branch Last
8
Pre-Order Traversal of a Binary Tree
VLR
i
VLR
d
g
VLR
VLR
a
VLR
m
f
VLR
h
VLR
b
e
c
VLR
VLR
VLR
9
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.
10
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
LRV
– Enumerate or Visit
11
Post-Order Traversal of a Binary Tree
LRV
i
LRV
d
g
LRV
LRV
a
LRV
m
f
LRV
h
LRV
b
e
c
LRV
LRV
LRV
12
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
Visit Node
Go Right
• Nodes are recursively processed
– Left Branch First
– Enumerate or Visit
LVR
– Right Branch Last
13
In-Order Traversal of a Binary Tree
LVR
i
LVR
LVR
d
g
LVR
a
m
LVR
LVR
f
h
LVR
LVR
b
e
c
LVR
LVR
14
In Order Threading
i
d
g
a
• Right null pointers
point to the
successor
m
f
• Left null pointer
point to the
predecessor.
h
b
e
c
15
Successor Algorithm
Right Once
1) ptr = ptr.rt;
If we followed a
2) if (T.is_thread(ptr) || ptr == null) thread then we found
stop
the successor
3) while (!T.is_thread(ptr.lt))
ptr = ptr.lt;
Check to see if we
hit the end
Go Left until we hit a
thread.
16
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;
Go to far left
while (!ptr.ltag)
Node
ptr = ptr.left;
while (ptr != null) {
//visit the node
tptr = ptr;
ptr = ptr.right;
if (!tptr.rtag)
while (!ptr.ltag)
ptr = ptr.left;
}
}
Successor
17
Algorithm
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;
}
18
Threading a Tree as it is Built
i
h
m
k
b
a
e
f
c
d
Input: i h b m e c f a 19
d k
Link Inversion on Lists- Going Down
tmp = pres.next
pres.next = prev
prev = pres
pres = tmp
Pres
Prev
Pres
Prev
tmp
Prev
Prev
Pres tmp
Pres Pres
tmp tmp
Prev
Head
A
B
C
D
20
Link Inversion on Lists - Coming Back
Points Prev & Pres are reversed
tmp = pres.next
pres.next = prev
prev = pres
pres = tmp
tmp
Pres
tmp
Pres
tmp Prev
Pres
Prev Pres
Prev
tmp
Pres
Prev
Prev
Head
A
B
C
D
21
Link Inversion with Trees
Temp Temp
Prev
Temp
Temp
Prev
Prev
Prev Prev
Pres
Pres
Prev
Temp Prev
Prev
Pres
Tag = 1
Pres
Temp
Prev
a
Prev Prev
Tag = 1
PresPres
b
Temp
Pres
Pres
c
Prev Temp
Prev
Pres
Pres
Tag = 1
Temp
d
e
f
PresTemp
Pres
Pres
Temp
g
Temp
22
Only Tag=1 is shown, those not present are zero
Building a Binary Search Tree
i
h
m
b
k
a
e
f
c
d
Input: i h b m e c f a d k
23