Data Structure & Algorithm Binary Search Tree(BST)

Published  . 0 views
↓ Download
Data Structure & Algorithm Binary Search Tree(BST)
1 / 1
Data Structure & Algorithm Binary Search Tree(BST) - slide 1 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 2 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 3 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 4 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 5 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 6 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 7 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 8 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 9 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 10 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 11 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 12 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 13 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 14 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 15 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 16 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 17 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 18 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 19 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 20 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 21 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 22 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 23 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 24 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 25 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 26 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 27 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 28 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 29 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 30 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 31 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 32 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 33 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 34 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 35 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 36 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 37 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 38 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 39 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 40 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 41 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 42 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 43 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 44 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 45 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 46 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 47 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 48 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 49 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 50 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 51 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 52 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 53 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 54 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 55 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 56 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 57 of 58 Data Structure & Algorithm Binary Search Tree(BST) - slide 58 of 58
Description: Data Structure Algorithm Binary Search Tree(BST) AVL Tree 1 Binary Search Tree (BST) Binary Search Tree, is a binary tree data structure which has the following properties: The left subtree of a node contains only nodes with values lesser

Related Topics

Download Presentation

"Data Structure & Algorithm Binary Search Tree(BST)" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. Data Structure & Algorithm Binary Search Tree(BST)
AVL Tree 1<br>
slide2. Binary Search Tree (BST) Binary Search Tree, is a binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with values lesser than the node’s value.
The right subtree of a node contains only nodes with value greater than the node’s value.
The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes. 2<br>
slide3. Example of BST 3<br>
slide4. Skewed BST If a tree which is dominated by left child node or right child node, is said to be a Skewed Binary Tree.
In a left skewed tree, most of the nodes have the left child without corresponding right child.
In a right skewed  tree, most of the nodes have the right child without corresponding left child. 4<br>
slide5. Limitation of BST The average search time for a binary search tree is directly proportional to its height: O(h). Most of the operation average case time is O(log2n).
BST’s are not guaranteed to be balanced. It may be skewed tree also.
For skewed BST, the average search time becomes O(n). So, it is working like an linear array.
To improve average search time and make BST balanced, AVL trees are used. 5<br>
slide6. AVL Tree AVL tree is a height balanced tree.
It is a self-balancing binary search tree.
It was invented by Adelson-Velskii and Landis.
AVL trees have a faster retrieval.
It takes O(logn) time for insertion and deletion operation.
In AVL tree, difference between heights of left and right subtree cannot be more than one for all nodes. 6<br>
slide7. AVL Tree Balance Factor of node is:
Height of left subtree – Height of Right subtree
Balance Factor is calculated for every node of AVL tree.
At every node, height of left and right subtree can differ by no more than 1.
For AVL tree, the possible values of balance factor are -1, 0, 1
Balance Factor of leaf nodes is 0 (zero). 7<br>
slide8. Example Every AVL Tree is a binary search tree but all the Binary Search Tree need not to be AVL trees. BST and AVL BST but not AVL 8<br>
slide9. Finding Balance Factor BF= hl - hr 9<br>
slide10. Height of AVL Tree By the definition of complete trees, any complete binary search tree is an AVL tree

Thus, an upper bound on the number of nodes in an AVL tree of height h a perfect binary tree with 2h + 1 – 1 nodes.
What is a lower bound? 10<br>
slide11. Height of AVL Tree 11 Let F(h) be the fewest number of nodes in a tree of height h.

From a previous slide:
F(0) = 1
F(1) = 2
F(2) = 4

Then what is F(h) in general?<br>
slide12. Height of AVL Tree 12 The worst-case AVL tree of height h would have:
A worst-case AVL tree of height h – 1 on one side,
A worst-case AVL tree of height h – 2 on the other, and
The root node


This is a recurrence relation: We get: F(h) = F(h – 1) + F(h – 2) + 1<br>
slide13. Imbalance After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types
LL: new node is in the left subtree of the left subtree of A
LR: new node is in the right subtree of the left subtree of A
RR: new node is in the right subtree of the right subtree of A
RL: new node is in the left subtree of the right subtree of A 13<br>
slide14. AVL Tree Example Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 17 11 7 53 4 14 17 7 4 53 11 13 14<br>
slide15. Types of Rotation 15 Rotation- To switch children and parents among two or three adjacent nodes to restore balance of a tree.<br>
slide16. Types of Rotation Single Rotation is applied when imbalanced node and child has same sign of BF (in the direction of new inserted node).
LL Rotation is applied in case of +ve sign. It mean left tree is heavy and so LL rotation is done.
RR Rotation is applied in case of -ve sign. It mean right tree is heavy and so RR rotation is done.
Double Rotation is applied when imbalanced node and child has different signs of BF (in the direction of new inserted node). 16<br>
slide17. LL Rotation Imbalanced AVL Tree LL Rotation Balanced AVL Tree Insert 3,2,1 in AVL Tree 17 L L<br>
slide18. RR Rotation Imbalanced AVL Tree RR Rotation Balanced AVL Tree Insert 1,2,3 in AVL Tree 18 R R<br>
slide19. LR Rotation Imbalanced AVL Tree RR Rotation LL Rotation Balanced AVL Tree Insert 3, 1, 2 in AVL Tree 19 L R<br>
slide20. RL Rotation Imbalanced AVL Tree LL Rotation RR Rotation Balanced AVL Tree Insert 1, 3, 2 in AVL Tree 20 L R<br>
slide21. Construct a AVL Tree by inserting from 1 to 5 numbers 1 0 1 2 3 1 2 2 3 0 -1 -1 -2 0 Not AVL
Apply RR Rotation 21<br>
slide22. Construct a AVL Tree by inserting from 1 to 5 numbers 1 2 3 After Rotation 0 0 0 4 1 2 3 4 0 0 -1 -1 1 2 3 4 0 -1 -2 -2 5 5 0 22<br>
slide23. Construct a AVL Tree by inserting from 1 to 5 numbers 1 2 3 4 0 0 -1 5 0 0 AVL Tree 23<br>
slide24. Construct AVL Tree with data items: 51, 26, 11, 6, 8, 4, 31 24<br>
slide25. Insertion in AVL Tree Insert 2 LL Rotation 25<br>
slide26. Insertion in AVL Tree Insert 4 LR Rotation
(3, 5, 6) 26<br>
slide27. Deletion in AVL Tree 27 Delete 8 Balanced AVL Balanced AVL It is also possible to delete an item from AVL Tree.<br>
slide28. Deletion in AVL Tree 28 Delete 8 Balanced AVL Imbalanced AVL Just like insertion, deletion can cause an imbalance, which will need to be fixed by applying one of the four rotations. R1 Rotation<br>
slide29. Deletion in AVL Tree The deletion is also the same as in BST. However, in imbalanced tree due to deletion, one or more rotations need to be applied to balance the AVL tree.

The Right(R) imbalance is classified into R0, R1, R-1
The Left(L) imbalance is classified into L0, L1, L-1 29<br>
slide30. Deletion in AVL Tree LL Rotation is same to R0 and R1
RR Rotation is same to L0 and L-1
LR Rotation is same to R-1
RL Rotation is same to L1 30<br>
slide31. R0 Rotation B AR h BL BR (0) (+1)
A Delete node X h x B AR h (0) (+2)
A BL BR
Unbalanced AVL search tree after deletion of node x h -1<br>
slide32. R0 Rotation R0 Rotation Balanced AVL search tree after rotation B AR h (0) (+2)
A BL BR
Unbalanced AVL search tree after deletion of x A R h B R (+1)
A B L (-1) B BF(B) == 0, use
R0 rotation<br>
slide33. R0 Rotation Example Unbalanced AVL search tree after deletion (0) (0) (+1) 46 20 54 (-1)
Delete 60 (+1)
18 7 (0) 23 (-1) 60

(0) 24 (0) (+2)
46 20 (0)
54
(-1) (+1)
18 23 7 (0) (0) 24<br>
slide34. R0 Rotation Example R0 (0) (+2) 46 20 (0)
54 (+1)
18 7 (0) 23 (-1)

(0) 24 Balanced AVL search tree
after deletion (+1) (-1)
20 18 23 (-1) (0)
7 (+1)
46
(0)
54 (0) 24<br>
slide35. R1 Rotation h -1 (+1) B (+1)
A Delete node X h AR x h
BL BR h
BL BR (+1) B (+2)
A Unbalanced AVL search tree after deletion of node x h -1 AR h -1<br>
slide36. R1 Rotation R1 Rotation Balanced AVL search tree after rotation h -1 BL BR (+1) B (+2)
A R h-1 A B R (0)
A B B L (0) BF(B) == 1, use
R1 rotation h h -1 AR<br>
slide37. R1 Rotation Example Unbalanced AVL search tree after deletion (+1) 37 (+1)
26 41 (+1)
Delete 39 (+1)
18 (0) 16 28 39 (0)
(0) (+1) (+2)
37 26 (0)
41
(0) (+1)
18 28 (0) 16<br>
slide38. R1 Rotation Example Balanced AVL search tree
after deletion (+2) 37 (+1)
26 28 (0) (0)
41 (+1)
18 (0) 16 (+1) (0)
26 18 16 28 (0) (0)
37 R1 Rotation

(0) 41 (0)<br>
slide39. Delete X BL C (0) (-1)
B h-1 h R-1 Rotation
A (+1) CL CR AR x C C L CR (0) (-1)
B Unbalanced AVL search tree after deletion BL (+2)
A AR h-1<br>
slide40. R-1 Rotation R -1 BL C (0) (-1)
B h-1 h -1 A (+2) CL CR AR CR (+1) B L (+2)
A AR BF(B) == -1, use R-1 rotation C B CL (0)<br>
slide41. R-1 Rotation R -1 CL CR (0) Balanced AVL search tree after Rotation B L (0)
C AR (0)
B

h -1 A
h -1 (0)<br>
slide42. R-1 Rotation Example (+1)
44 (-1)
22 (-1) Delete 52
48 (0)
18 52 28 (0)
29 23 (+2)
44 (-1)
22 28 (0) (0)
48 (0)
18 Unbalanced AVL search
tree after deletion 23 (0) 29<br>
slide43. R-1 Rotation Example (+2)
44 (-1)
22 (-1) R-1 Rotation
48 (0)
18 28 (0)
29 23 (+2)
44 (+1)
28 29 (0) (0)
48 (0)
22 Unbalanced AVL search
tree after deletion 23 (0) (0)
18<br>
slide44. R-1 Rotation Example (0) (0)
28 22 48 (0)
44 Balanced AVL search tree
after rotation (0) (0) (0)
18 23 29<br>
slide45. L0 Rotation AL h BL BR B (0) (-1)
A Delete X h x B L BR B (0) (-2)
A h Unbalanced AVL search tree after deletion AL h-1<br>
slide46. L0 Rotation AL h BL BR (-2)
A
B (0) L0 Rotation h -1 BR (0) (+1)
B h BL
Balanced AVL search tree after deletion h-1 (-1)
AL A<br>
slide47. L0 Rotation 47 (-1) 54 50 48 52 44 (0) (-1) (0) (1) 22 (0) (-2) 54 50 48 52 44 (0) (-1) (1) (0) (0) 56 56 49 49 (0) (1) Delete 22 (0)<br>
slide48. L0 Rotation 48 (1) (1) 54 48 52 44 (0) (0) 49 56 50 (-1) (0) (-1) L0 Rotation<br>
slide49. L1 Rotation L h-1 C L BR B (+1) (-1)
A Delete X h x
A CR h-1 (0)
C R B (+1) (-2)
A h-1
B search tree after deletion AL h-1 CL CR
Unbalanced AVL (0)
C<br>
slide50. L1 Rotation A L h-1 C L BR B (+1) (-2)
A L1 Rotation h-1 CR h-1 (0)
C (0) A B C L R (0) (0)
C h-1
B Unbalanced AVL search tree after deletion A L h-1 CR<br>
slide51. L1 Rotation 51 (-2) 52 50 48 51 44 (1) (0) (1) (0) Delete 22 (-1) 52 50 44 (1) (0) (0) (1) 49 48 51 22 (-1) (0) 49 (0)<br>
slide52. L1 Rotation 52 50 44 48 (-2) (-1) 51 (0) (0) (-1) L1 Rotation 52 (0) 49<br>
slide53. L1 Rotation 53 51 48 50 (0) (-1) 49 52 44 (0) (0) (0) (0) L1 Rotation<br>
slide54. L-1 Rotation AL h BL BR B (-1) (-1)
A Delete X h x h-1 B L BR B (-1) (-2)
A h Unbalanced AVL search tree after deletion AL h-1 h-1<br>
slide55. L-1 Rotation AL h BL BR B (-1) (-2)
A L-1 Rotation h-1 A B R (-1) (0)
B h Balanced AVL search tree after deletion A L h-1 BL h-1<br>
slide56. 56 (1)
22 (-1) Delete 18
48 (0)
18 52 (-1)
44 54 50 (0)
22 (-1) (-2)
44 54 50 48 52 47 47 L-1 Rotation (0) (0) (0) (0) (0) (0) (0) (0)<br>
slide57. L-1 Rotation 57 (0) 54 50 48 52 44 (0) (0) (0) (0) 47 22 (0) L-1 Rotation<br>
slide58. Summary Binary Search Tree and its Limitation
AVL Tree
Definition
Rotation & its Types
Insertion
Deletion 58<br>