cs2133 Lec12
cs2133 Lec12
Lecture No. 12
1
04-Dec-19
Delete on
this side
4 0
2 6 1
1 3 5 7 2
2
04-Dec-19
4 0 4
2 6 1 2 6
1 3 5 7 2 3 5 7
remove
(1) 5
Delete on
this side
3
04-Dec-19
Delete on
this side
Delete on
this side
4
04-Dec-19
Case 3a: the parent had balance of -1 and the node was
deleted in the parent’s left subtree, right subtree was
balanced.
Case 3a: the parent had balance of -1 and the node was
deleted in the parent’s left subtree, right subtree was
balanced.
Single
rotate
5
04-Dec-19
Case 4a: parent had balance of -1 and the node was deleted
in the parent’s left subtree, right subtree was unbalanced.
11
Case 4a: parent had balance of -1 and the node was deleted
in the parent’s left subtree, right subtree was unbalanced.
double
rotate
6
04-Dec-19
Case 5a: parent had balance of -1 and the node was deleted
in the parent’s left subtree, right subtree was unbalanced.
13
Case 5a: parent had balance of -1 and the node was deleted
in the parent’s left subtree, right subtree was unbalanced.
single
rotate
7
04-Dec-19
Expression Trees
15
Expression Trees
Expression trees, and the more general parse trees and abstract
16
8
04-Dec-19
Expression Tree
(a+b*c)+((d*e+f)*g)
+ *
a * + g
b c * f
d e
17
SELECT title
FROM StarsIn, MovieStar
WHERE starName = name AND birthdate LIKE ‘%1960’ ;
18
9
04-Dec-19
19
Compiler Optimization
Common subexpression:
(f+d*e)+((d*e+f)*g)
+ *
f * + g
d e * f
d e
20
10
04-Dec-19
Compiler Optimization
(Common subexpression:
(f+d*e)+((d*e+f)*g)
+
+ *
f * g
d e
Graph!
21
Huffman Encoding
11
04-Dec-19
23
24
12
04-Dec-19
A internal nodes: 9
external nodes: 10
B C
internal node
D E F
G E F
25 external node
26
13
04-Dec-19
B C internal link
D E F
external link
G E F
Internal links: 8
27
External links: 10
to external nodes.
• In every rooted tree, each node, except the root, has a unique parent.
• Every link connects a node to its parent, so there are N-1 links connecting internal
nodes.
• Similarly, each of the N+1 external nodes has one link to its parent.
28
• Thus N-1+N+1=2N links.
14
04-Dec-19
29
15
04-Dec-19
Since every node (except the root) is pointed to, there are only N-
31
A Internal nodes: 9
External nodes: 10
B C
internal node
D E F
G E F
external node
32
16
04-Dec-19
The threaded tree data structure will replace these NULL pointers
appropriate.
33
14
15
p 18
t 16 20
17
04-Dec-19
14
15
p 18
1
t 16 20
14
15
p 18
1
t 16 20
2
1. t->L = p->L; // copy the thread
2. t->LTH = thread;
t->R = p; // *p is successor of *t
t->RTH = thread;
18
04-Dec-19
14
15
p 18
1
t 16 20
2
1. t->L = p->L; // copy the thread 3
2. t->LTH = thread;
3. t->R = p; // *p is successor of *t
t->RTH = thread;
p->LTH = child;
14
15
p 18
1
t 16 20
2 4
1. t->L = p->L; // copy the thread 3
2. t->LTH = thread;
3. t->R = p; // *p is successor of *t
4. t->RTH = thread;
p->LTH = child;
19
04-Dec-19
14
15
p 18
1 5
t 16 20
2 4
1. t->L = p->L; // copy the thread 3
2. t->LTH = thread;
3. t->R = p; // *p is successor of *t
4. t->RTH = thread;
14
15
p 18
1 5 6
t 16 20
2 4
1. t->L = p->L; // copy the thread 3
2. t->LTH = thread;
3. t->R = p; // *p is successor of *t
4. t->RTH = thread;
20
04-Dec-19
14
4 15
3 9 18
7 16 20
41
Inorder successor of 4.
14
4 15
3 9 18
7 16 20
42
21
04-Dec-19
Inorder successor of 4.
14
4 15
3 9 18
7 16 20
43
Inorder successor of 9.
14
4 15
3
9 18
7 16 20
5
Follow right thread to 14
44
22
04-Dec-19
Routine: nextInorder
TreeNode* nextInorder(TreeNode* p)
{
if(p->RTH == thread)
return(p->R);
else {
p = p->R;
while(p->LTH == child)
p = p->L;
return p;
}
} 45
Inorder traversal
If we call nextInorder with the root of the binary tree, we're going
to have some difficulty. The code won't work at all the way we
want.
46
23
04-Dec-19
4 15
3 9 18
7 16 20
5
47
4 15 p?
3 9 18
7 16 20
5
48
24