04-Dec-19 1 Lecture No. 12 2 Deletion in AVL Tree There are 5 cases to consider. Let us go through the cases graphically and determine what action to take. We will not develop the C++ code for deleteNode in AVL tree. This will be left as an exercise.
24
Embed
Lecture No. 12€¦ · Threaded Binary Tree Oddly, most of the pointer fields in our representation of binary trees are NULL! Since every node (except the root) is pointed to, there
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
04-Dec-19
1
Lecture No. 12
2
Deletion in AVL Tree
There are 5 cases to consider.
Let us go through the cases graphically and determine what action to take.
We will not develop the C++ code for deleteNode in AVL tree. This will be left as an exercise.
04-Dec-19
2
3
Deletion in AVL Tree
Case 1a: the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree.
Action: change the balance of the parent node and stop. No further effect on balance of any higher node.
Delete on this side
4
Deletion in AVL Tree
Here is why; the height of left tree does not change.
1
2
3
4
5
6
7
0
1
2
04-Dec-19
3
5
Deletion in AVL Tree
Here is why; the height of left tree does not change.
1
2
3
4
5
6
7
2
3
4
5
6
7
0
1
2
remove(1)
6
Deletion in AVL Tree
Case 1b: the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree.
Action: (same as 1a) change the balance of the parent node and stop. No further effect on balance of any higher node.
Delete on this side
04-Dec-19
4
7
Deletion in AVL Tree
Case 2a: the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s left subtree.
Action: change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree.
Delete on this side
8
Deletion in AVL Tree
Case 2b: the parent of the deleted node had a balance of -1 and the node was deleted in the parent’s right subtree.
Action: same as 2a: change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree.
Delete on this side
04-Dec-19
5
9
Deletion in AVL Tree
Case 3a: the parent had balance of -1 and the node was deleted in the parent’s left subtree, right subtree was balanced.
10
Deletion in AVL Tree
Case 3a: the parent had balance of -1 and the node was deleted in the parent’s left subtree, right subtree was balanced.
Action: perform single rotation, adjust balance. No effect on balance of higher nodes so stop here.
Single rotate
04-Dec-19
6
11
Deletion in AVL Tree
Case 4a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right subtree was unbalanced.
12
Deletion in AVL Tree
Case 4a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right subtree was unbalanced.
Action: Double rotation at B. May have effected the balance of higher nodes, so continue up the tree.
rotatedouble
04-Dec-19
7
13
Deletion in AVL Tree
Case 5a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right subtree was unbalanced.
14
Deletion in AVL Tree
Case 5a: parent had balance of -1 and the node was deleted in the parent’s left subtree, right subtree was unbalanced.
Action: Single rotation at B. May have effected the balance of higher nodes, so continue up the tree.
rotatesingle
04-Dec-19
8
Other Uses of Binary Trees
Expression Trees
15
16
Expression Trees
Expression trees, and the more general parse trees and abstract
syntax trees are significant components of compilers.
Let us consider the expression tree.
04-Dec-19
9
17
Expression Tree
(a+b*c)+((d*e+f)*g)
a
c
+
b
g
*
+
+
d
*
*
e
f
18
Parse Tree for an SQL query
Consider querying a movie database
Find the titles for movies with stars born in 1960
The database has tables
StarsIn(title, year, starName)
MovieStar(name, address, gender, birthdate)
SELECT title
FROM StarsIn, MovieStar
WHERE starName = name AND birthdate LIKE ‘%1960’ ;
04-Dec-19
10
19
SQL Parse Tree
20
Compiler Optimization
Common subexpression:(f+d*e)+((d*e+f)*g)
f
e
+
d
g
*
+
+
d
*
*
e
f
04-Dec-19
11
21
Compiler Optimization
(Common subexpression:(f+d*e)+((d*e+f)*g)
f
e
+
d
g
*
+
*
Graph!
22
Huffman Encoding
Huffman code is method for the compression for standard text documents.
It makes use of a binary tree to develop codes of varying lengths for the letters used in the original message.
Huffman code is also part of the JPEG image compression scheme.
The algorithm was introduced by David Huffman in 1952 as part of a course assignment at MIT.
04-Dec-19
12
23
Mathematical Properties of Binary Trees
24
Properties of Binary Tree
Property: A binary tree with N internal nodes
has N+1 external nodes.
04-Dec-19
13
25
Properties of Binary Tree
A binary tree with N internal nodes has N+1 external nodes.
D F
B C
G
A
E
FE
internal nodes: 9external nodes: 10
external node
internal node
26
Properties of Binary Tree
Property: A binary tree with N internal nodes has 2N links: N-1 links to internal nodes and N+1 links to external nodes.
04-Dec-19
14
27
Threaded Binary Tree
Property: A binary tree with N internal nodes has 2N links: N-1 links to internal nodes and N+1 links to external nodes.
D F
B C
G
A
E
FE
Internal links: 8External links: 10
external link
internal link
28
Properties of Binary Tree
Property: A binary tree with N internal nodes has
2N links: N-1 links to internal nodes and N+1 links
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.
• Thus N-1+N+1=2N links.
04-Dec-19
15
29
Threaded Binary Trees
30
Threaded Binary Tree
In many applications binary tree traversals are carried out repeatedly.
The overhead of stack operations during recursive calls can be costly.
The same would true if we use a non-recursive but stack-driven traversal procedure
It would be useful to modify the tree data structure which represents the binary tree so as to speed up, say, the inorder traversal process: make it "stack-free".
04-Dec-19
16
31
Threaded Binary Tree
Oddly, most of the pointer fields in our representation of binary
trees are NULL!
Since every node (except the root) is pointed to, there are only N-
1 non-NULL pointers out of a possible 2N (for an N node tree), so
that N+1 pointers are NULL.
32
Threaded Binary Tree
D F
B C
G
A
E
FE
Internal nodes: 9External nodes: 10
external node
internal node
04-Dec-19
17
33
Threaded Binary Tree
The threaded tree data structure will replace these NULL pointers
with pointers to the inorder successor (predecessor) of a node as
appropriate.
We'll need to know whenever formerly NULL pointers have been
replaced by non NULL pointers to successor/predecessor nodes,
since otherwise there's no way to distinguish those pointers from
the customary pointers to children.
34
Adding threads during insert
t->L = p->L; // copy the thread t->LTH = thread; t->R = p; // *p is successor of *tt->RTH = thread;
p->L = t; // attach the new leaf p->LTH = child;
14
15
18
16 20
p
t
04-Dec-19
18
35
Adding threads during insert
1. t->L = p->L; // copy the thread t->LTH = thread; t->R = p; // *p is successor of *t t->RTH = thread;
p->L = t; // attach the new leaf p->LTH = child;
14
15
18
16 20
p
t
1
36
Adding threads during insert
1. t->L = p->L; // copy the thread 2. t->LTH = thread;t->R = p; // *p is successor of *t t->RTH = thread;
p->L = t; // attach the new leaf p->LTH = child;
14
15
18
16 20
p
t
1
2
04-Dec-19
19
37
Adding threads during insert
1. t->L = p->L; // copy the thread 2. t->LTH = thread;3. t->R = p; // *p is successor of *tt->RTH = thread;
p->L = t; // attach the new leaf
p->LTH = child;
14
15
18
16 20
p
t
1
2
3
38
Adding threads during insert
1. t->L = p->L; // copy the thread 2. t->LTH = thread;3. t->R = p; // *p is successor of *t4. t->RTH = thread;
p->L = t; // attach the new leaf
p->LTH = child;
14
15
18
16 20
p
t
1
2
3
4
04-Dec-19
20
39
Adding threads during insert
1. t->L = p->L; // copy the thread 2. t->LTH = thread;3. t->R = p; // *p is successor of *t4. t->RTH = thread;
5. p->L = t; // attach the new leafp->LTH = child;
14
15
18
16 20
p
t
1
2
3
4
5
40
Adding threads during insert
1. t->L = p->L; // copy the thread 2. t->LTH = thread;3. t->R = p; // *p is successor of *t4. t->RTH = thread;
5. p->L = t; // attach the new leaf6. p->LTH = child;
14
15
18
16 20
p
t
1
2
3
4
5 6
04-Dec-19
21
41
Threaded Binary Tree
14
154
9
7
183
5
16 20
42
Where is inorder successor?
Inorder successor of 4.
14
154
9
7
183
5
16 20
04-Dec-19
22
43
Where is inorder successor?
Inorder successor of 4.
14
154
9
7
183
5
16 20
Left-most node in right subtree of 4
44
Where is inorder successor?
Inorder successor of 9.
14
154
9
7
183
5
16 20
Follow right thread to 14
04-Dec-19
23
45
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;
}
}
46
Inorder traversal
If we can get things started correctly, we can simply call
nextInorder repeatedly (in a simple loop) and move rapidly around
the tree inorder printing node labels (say) - without a stack.
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