1 / 18

BST and AVL short illustrative repetition

Learn about Binary Search Trees (BST) and AVL Trees, their properties, and the operations that can be performed on them, including find, insert, and delete. Understand rotation techniques for balancing the trees.

sherrid
Download Presentation

BST and AVL short illustrative repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BST and AVL short illustrative repetition 0

  2. 51 34 76 13 40 68 92 22 73 8 45 18 25 70 74 Binary search tree For each node Y it holds: Y Keys in the left subtree of Y are smaller than the key of Y. > Y Keys in the right subtree of Y are bigger than the key of Y. < Y 8 13 18 22 25 34 40 45 51 68 70 73 74 76 92 1

  3. Binary search tree BST may not be balanced and usually it is not. 51 34 76 BST may not be regular and usually it is not. 13 40 68 92 Apply the INORDER traversal to obtain sorted list of the keys of BST. 8 22 45 73 74 18 25 70 BST is flexible due to operations: Find – return the pointer to the node with the given key (or null). Insert – insert a node with the given key. Delete – (find and) removethe node with the given key. 2

  4. Operation Find in BST Find 18 51 34 76 Each BST operation starts in the root. 13 40 68 92 8 22 45 73 74 18 25 70 3

  5. Operation Insert in BST Insert 42 51 34 76 13 40 68 92 8 22 45 73 42 74 18 25 70 Key 42 belongs here Insert 1. Find the place (like in Find) for the leaf where the key belongs. 2. Create this leaf and connect it to the tree. 4

  6. Operation Delete in BST (I.) Delete a nodewith 0 children (= leaf) Delete 25 51 34 76 13 40 68 92 8 22 45 73 42 74 18 25 70 Leaf with key 25 disappears Delete I. Find the node (like in Find operation) with the given key and set the reference to it from its parent to null. 5

  7. Operation Delete in BST (II.) Delete a nodewith1child. Delete 68 51 34 76 13 40 68 92 8 22 45 73 Node with key 68 disappears 42 74 18 70 Change the 76-->68 reference to 76 --> 73 reference. Delete II. Find the node (like in Find operation) with the given key and set the reference to it from its parent to its (single) child. 6

  8. Operation Delete in BST (IIIa.) Delete a nodewith2children. 51 Delete 34 x 34 76 13 68 92 40 8 22 38 45 73 36 74 18 70 y Key 34 disappears. And it is substituted by key 36. Delete IIIa. 1. Find the node (like in Find operation) with the given key and then find the leftmost(= smallest key) node y in the rightsubtree of x. 2. Point from y to children of x, from parent of y point to the child of y instead of y, from parent of x point to y. 7

  9. Operation Delete in BST (IIIb.) is equivalent to Delete IIIa. Delete a nodewith2children. 51 Delete 34 x 34 76 13 40 68 92 y 8 22 45 73 42 74 18 70 Key 34 disappears. And it is substituted by key 22. Na jeho místo nastoupí 22. Delete IIIb. 1. Find the node (like in Find operation) with the given key and then find the rightmost(= smallest key) node y in the leftsubtree of x. 2. Point from y to children of x, from parent of y point to the child of y instead of y, from parent of x point to y. 8

  10. AVL tree -- G.M. Adelson-Velskij & E.M. Landis, 1962 AVL tree is a BSTwith additional properties which keep it acceptablybalanced. Operations Find, Insert, Delete also apply to AVL tree. 51 2 1 34 76 1 1 0 0 13 40 68 92 0 0 -1 0 -1 -1 -1 -1 8 22 45 -1 -1 -1 -1 -1 -1 AVL rule: There are two integers associated with each node: Depth of the left and depth of the right subtree of the node. Note: Depth of an empty tree is -1. The difference of the heights of the left and the right subtree may be only -1 or 0 or 1 in each node of the tree. 9

  11. AVL tree -- G.M. Adelson-Velskij & E.M. Landis, 1962 Find -- same as in a BST Insert -- first, insert as in a BST, next, travel from the inserted node upwards and update the node depths. If disbalance occurs in any node along the path then apply an appropriate rotation and stop. Delete -- first, delete as in a BST, next, travel from the deleted position upwards and update the node depths. If disbalance occurs in any node along the path then apply an appropriate rotation. Continue travelling along the path up to the root. 10

  12. Rotation R in general Disbalance detected C x+2 x Before A x+1 x B Disbalancing node Z -1 -1 A x+1 x+1 After C x x B Unaffected subtrees Z -1 -1 11

  13. RotationLin general A x x+2 C Before x x+1 B Rotation L is a mirror image of rotation R, there is no other difference between the two. Z -1 -1 C x+1 x+1 After A x x B Unaffected subtrees Z -1 -1 12

  14. Disbalance detected RotationLR in general E X+2 x Before A x x+1 C x x-1 B D Z Disbalancing node -1 -1 C x+1 x+1 After A E x x x-1 x B D Unaffected subtrees Z -1 -1 13

  15. Rotation RLin general Disbalance detected A x X+2 E x+1 x Before C x-1 x B D RotationRL is a mirror image of rotationLR, there is no other difference between the two. Z Disbalancing node -1 -1 C x+1 x+1 After A E x-1 x x x B D Unaffected subtrees Z -1 -1 14

  16. Rules for aplying rotations L, R, LR, RL in Insert operation Travel from the inserted node up to the root and update the subtree depths in each node along the path. If a node is disbalanced and you came to it along two consecutive edges * in the up and right direction perform rotation R in this node, * in the up and left direction perform rotationLin this node, * first in the in the up and left and then in the up and right direction perform rotationLRin this node, * first in the in the up and right and then in the up and left direction perform rotationRLin this node, After one rotation in the Insert operation the AVL tree is balanced. After one rotation in the Delete operation the AVL tree might still not be balanced, all nodes on the path to the root have to be checked. 15

  17. Necessity of multiple rotationsinoperation Delete. Example. The AVL tree is originally balanced. Delete the rightmost key. 1 2 4 3 Balanced. 16

  18. A4B33ALG 2011 /06 Asymptotic complexities of Find, Insert, Delete in BST and AVL BSTwith n nodes AVL treewith n nodes 17

More Related