INE2011 Data Structures
Data Structures
Electronics and Communication EngineeringHanyang University
Haewoon Nam
Lecture 7
(INE2011)
1
INE2011 Data Structures
Binary Tree Traversal
• Many binary tree operations are done by performing a traversal of the binary tree.
• In a traversal, each element of the binary tree is visited exactly once.
• Binary tree traversal methods– Pre-order– In-order– Post-order– Level order
2
INE2011 Data Structures
Binary Tree Traversal
• Traversing each node and subtree– L : moving left– V : visit– R : moving right
• Six possible combinations of traversal– LVR, LRV, VLR, VRL, RVL, RLV
• Traverse left before right– VLR : pre-order– LVR : in-order– LRV : post-order
3
INE2011 Data Structures
a b c
Binary Tree Traversal
• Pre-order example
a
b c
a
b
d e
c
f
jg h i
a b d g h e i c f j
4
INE2011 Data Structures
b a c
Binary Tree Traversal
• In-order example
a
b c
a
b
d e
c
f
jg h i
g d h b e i a f j c
5
INE2011 Data Structures
b c a
Binary Tree Traversal
• Post-order example
a
b c
a
b
d e
c
f
jg h i
g h d i e b j f c a
6
INE2011 Data Structures
Binary Tree Traversal
• Pre-order, in-order, post-order
void PreOrder(TreeNode *t){
if (t != NULL){
Visit(t); PreOrder(t->leftChild); PreOrder(t->rightChild);
}}
void InOrder(TreeNode *t){
if (t != NULL){
InOrder(t->leftChild); Visit(t);InOrder(t->rightChild);
}}
void PostOrder(TreeNode *t){
if (t != NULL){
PostOrder(t->leftChild); PostOrder(t->rightChild);Visit(t);
}}
7
INE2011 Data Structures
a b c
Binary Tree Traversal
• Level order example
a
b c
a
b
d e
c
f
jg h i
a b c d e f g h i j
8
INE2011 Data Structures
Threaded Binary Trees
• Threads– Many null pointers in a binary tree– Replace null pointers with the in-order predecessor or the in-
order successor
a
b
d e
c
f
jg h i
g d h b e i a f j c
g h i j
d e f
b c
a
9
INE2011 Data Structures
Threaded Binary Trees
• Class for threaded node
g d h b e i a f j c
g h i j
d e f
b c
aclass treenode{private:
int data;treenode *left;bool leftThread;treenode *right;bool rightThread;
};
left child
data
right childleftThread
rightThread
- If leftThread is true, *left containsa thread, otherwise a pointer.
- Same for rightThread.
10
INE2011 Data Structures
Binary Tree Construction
• Can you construct the binary tree from a given traversal sequence?– When a traversal sequence has more than one element, the
binary tree is not uniquely defined– Therefore, the tree from which the sequence was obtained
cannot be reconstructed uniquely
• Can you construct the binary tree, given two traversal sequences?
a
b
a bPre-order = a
b
11
INE2011 Data Structures
Binary Tree Construction
Pre-order = g d h b e i a f j cIn-order =
a b d g h e i c f j
• Scan the pre-order left to right using the in-order to separate left and right subtrees– Step 1
• a is the root of the tree. gdhbei are in the left subtree. fjc are in the right subtree.
a
g d h b e i f j c
12
INE2011 Data Structures
Binary Tree Construction
Pre-order = g d h b e i a f j cIn-order =
a b d g h e i c f j
– Step 2• b is the next root of the tree. gdh are in the left subtree. ei are in the right
subtree.
a
g d h
f j cb
e i
13
INE2011 Data Structures
Binary Tree Construction
Pre-order = g d h b e i a f j cIn-order =
a b d g h e i c f j
– Step 3• d is the next root of the tree. g is in the left subtree. h is in the right subtree.
a
f j cb
d
g h
e i
14
INE2011 Data Structures
Binary Tree Construction
Pre-order = g d h b e i a f j cIn-order =
a b d g h e i c f j
– Step 4• e is the next root of the tree. i is in the right subtree.
a
g
f j cb
h
d
i
e
15
INE2011 Data Structures
Binary Tree Construction
Pre-order = g d h b e i a f j cIn-order =
a b d g h e i c f j
– Step 5• c is the next root of the tree. fj is in the left subtree.
a
g
b
h
d
i
e
c
f j
16
INE2011 Data Structures
Binary Tree Construction
Pre-order = g d h b e i a f j cIn-order =
a b d g h e i c f j
– Step 6• f is the next root of the tree. j is in the right subtree.
a
g
b
h
d
i
e
c
j
f
17
INE2011 Data Structures
Binary Tree Construction
• Scan the pre-order left to right using the in-order to separate left and right subtrees– Step 1
• a is the root of the tree. gdhbei are in the left subtree. fjc are in the right subtree.
– Step 2 – 6• Similar to the previous example with pre-order and in-order
a
g d h b e i f j c
Post-order = g d h b e i a f j cIn-order =
g h d i e b j f c a
18
INE2011 Data Structures
Binary Tree Construction
• Scan the pre-order left to right using the in-order to separate left and right subtrees– Step 1
• a is the root of the tree. gdhbei are in the left subtree. fjc are in the right subtree.
– Step 2 – 6• Similar to the previous example with pre-order and in-order
a
g d h b e i f j c
Level order = g d h b e i a f j cIn-order =
a b c d e f g h i j
19
INE2011 Data Structures
Joining and Splitting Binary Tree
• Joining(Grafting) and Splitting(Pruning)
a
b
e
c
f j
a
b
e
c
f js
p q
s
p q
Grafting
Pruning
INE2011 Data Structures
Binary Search Tree
• Dictionary operations– IsEmpty( )– Get(key)– Insert(key, value)– Delete(key)
• Treenode– Each node has a (key, value) pair
21
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9class treenode{private:
int key;char value;treenode *left;treenode *right;
};
INE2011 Data Structures
Binary Search Tree
• Ascend( )– Do an in-order traversal
g d h b e i a f j c
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9
22
INE2011 Data Structures
Binary Search Tree
• Get(key)– Search the node with the key (input)– Return the value of the node
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9
Get(12) returns e
23
INE2011 Data Structures
Binary Search Tree
• Insert(key, value)– Insert a node at a proper position based on the key (input)
Insert(23, k)
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9
k23
24
INE2011 Data Structures
Binary Search Tree
• Delete(key)– No element with the delete key– Element is in a leaf– Element is in a degree 1 node– Element is in a degree 2 node
25
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a leaf
Delete(9)
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9
26
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 1 node
Delete(20)
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9
27
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 1 node
Delete(12)
a
b
d e
c
f
jg h i
15
10
8 12
20
17
19135 9
28
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 2 node– Example 1
Delete(10)
a
b
d e
c
f
jg h i
15
10
7 12
20
17
19135 9Find the largest key in the left subtree
m8
Or the smallest key in the right subtree
29
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 2 node– Example 1
Delete(10)
a
b
d e
c
f
jg h i
15
10
7 12
20
17
19135 9Find the largest key in the left subtree
m8
Or the smallest key in the right subtree
30
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 2 node– Example 1
Delete(10)
a
h
d e
c
f
jg i
15
9
7 12
20
17
19135m
8
31
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 2 node– Example 2
Delete(15)
a
b
d e
c
f
jg h i
15
10
7 12
20
17
19135 9
m8
32
INE2011 Data Structures
Binary Search Tree
• Delete(key)– Element is in a degree 2 node– Example 2
Delete(15)
a
b
d e
c
f
jg h i
15
10
7 12
20
17
19135 9
m8
Find the largest key in the left subtree
33