0% found this document useful (0 votes)
14 views

cs2133 Lec12

Uploaded by

Kashif Alirana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

cs2133 Lec12

Uploaded by

Kashif Alirana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

04-Dec-19

Lecture No. 12

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.

1
04-Dec-19

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.

Delete on
this side

Action: change the balance of the parent node and stop. No


further effect on balance of any higher node.
3

Deletion in AVL Tree

Here is why; the height of left tree does not change.

4 0

2 6 1

1 3 5 7 2

2
04-Dec-19

Deletion in AVL Tree

Here is why; the height of left tree does not change.

4 0 4

2 6 1 2 6

1 3 5 7 2 3 5 7

remove
(1) 5

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.

Delete on
this side

Action: (same as 1a) change the balance of the parent node


and stop. No further effect on balance of any higher node.
6

3
04-Dec-19

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.

Delete on
this side

Action: change the balance of the parent node. May have


caused imbalance in higher nodes so continue up the tree.
7

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.

Delete on
this side

Action: same as 2a: change the balance of the parent node.


May have caused imbalance in higher nodes so continue up
the tree. 8

4
04-Dec-19

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.

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.

Single
rotate

Action: perform single rotation, adjust balance. No effect on


balance of higher nodes so stop here. 10

5
04-Dec-19

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.

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.

double
rotate

Action: Double rotation at B. May have effected the balance of


higher nodes, so continue up the tree. 12

6
04-Dec-19

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.

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.

single
rotate

Action: Single rotation at B. May have effected the balance of


higher nodes, so continue up the tree. 14

7
04-Dec-19

Other Uses of Binary Trees

Expression Trees

15

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.

16

8
04-Dec-19

Expression Tree

(a+b*c)+((d*e+f)*g)

+ *

a * + g

b c * f

d e
17

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’ ;
18

9
04-Dec-19

SQL Parse Tree

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

 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.
22

11
04-Dec-19

Mathematical Properties of Binary


Trees

23

Properties of Binary Tree

Property: A binary tree with N internal nodes

has N+1 external nodes.

24

12
04-Dec-19

Properties of Binary Tree

A binary tree with N internal nodes has N+1 external nodes.

A internal nodes: 9
external nodes: 10

B C
internal node

D E F

G E F

25 external node

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.

26

13
04-Dec-19

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.

B C internal link

D E F
external link

G E F

Internal links: 8
27
External links: 10

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.

28
• Thus N-1+N+1=2N links.

14
04-Dec-19

Threaded Binary Trees

29

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".
30

15
04-Dec-19

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.

31

Threaded Binary Tree

A Internal nodes: 9
External nodes: 10
B C
internal node

D E F

G E F

external node
32

16
04-Dec-19

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.

33

Adding threads during insert

14

15

p 18

t 16 20

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 34


p->LTH = child;

17
04-Dec-19

Adding threads during insert

14

15

p 18
1

t 16 20

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 35


p->LTH = child;

Adding threads during insert

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;

p->L = t; // attach the new leaf 36


p->LTH = child;

18
04-Dec-19

Adding threads during insert

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->L = t; // attach the new leaf 37

p->LTH = child;

Adding threads during insert

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->L = t; // attach the new leaf 38

p->LTH = child;

19
04-Dec-19

Adding threads during insert

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;

5. p->L = t; // attach the new leaf 39


p->LTH = child;

Adding threads during insert

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;

5. p->L = t; // attach the new leaf 40


6. p->LTH = child;

20
04-Dec-19

Threaded Binary Tree

14

4 15

3 9 18

7 16 20

41

Where is inorder successor?

Inorder successor of 4.

14

 4 15

3 9 18

7 16 20

42

21
04-Dec-19

Where is inorder successor?

Inorder successor of 4.

14

 4 15

3 9 18

7 16 20

43

Left-most node in right subtree of 4

Where is inorder successor?

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 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

want.

46

23
04-Dec-19

Calling nextInorder with root


TreeNode* nextInorder(TreeNode* p){
if(p->RTH == thread) return(p->R);
else {
p = p->R;
while(p->LTH == child)
p = p->L;
return p;
}
14 p
}

4 15

3 9 18

7 16 20

5
47

Calling nextInorder with root


TreeNode* nextInorder(TreeNode* p){
if(p->RTH == thread) return(p->R);
else {
p = p->R;
while(p->LTH == child)
p = p->L;
return p;
}
14
}

4 15 p?

3 9 18

7 16 20

5
48

24

You might also like