Presentation is loading. Please wait.

Presentation is loading. Please wait.

Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.

Similar presentations


Presentation on theme: "Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered."— Presentation transcript:

1 Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered on unique path depends on the shape of the tree and the position of the node in the tree

2 O(h) where h is the height of the tree The height varies – A “worst-shape” BST – A “best-shape” BST – An “average-shape” BST Worst-case running time of each operation?

3 Different Shapes of Tree

4 Balanced BST Can Do better BSTs are limited because of their bad worst- case performance O(n). A BST with this worst- case structure is no more efficient than a regular linked list Balanced search trees are trees whose heights in the worst case is O(lg n)

5 Balanced BST Can Do better If the tree is perfectly balanced, we can store more than 16,000 elements and require at most 14 nodes to be checked to locate an element. How many nodes (in the worst case) would we have to check in a linked list? h-1

6 What Does it Mean to Balance a BST? Tentative Rule – Require that the left and right subtrees of the root node have the same height We can do better

7 What Does it Mean to Balance a BST? (cont’d) Another Tentative Rule – Require that every node have left and right subtrees of the same height Too restrictive  only perfectly balanced trees of 2 k – 1 nodes would satisfy this criterion

8 What Does it Mean to Balance BST? (cont’d) The Rule – Require that, for every node, the height of the left and right subtrees can differ by at most one

9 Balancing a BST There are a number of techniques to properly balance a binary tree – Approach 1: take all the elements, place them in an array, sort them, and then reconstruct the tree (global) (example, and algorithm) – Approach 2: constantly restructuring the tree when new elements arrive or elements are deleted and lead to an unbalanced tree (i.e., self- balancing trees)

10 Approach 1: Reorder Data and Build BST When all data arrive, store all data in an array, sort the array. What is the root of the tree? – the middle element of the array Designate for the root of the BST the middle element of the array (i.e., the middle element of the array is the first element inserted into the BST) Continue inserting recursively on the left and right subarrays until all elements in the array have been inserted into the BST (construct the tree)

11 Approach 1: Reorder Data and Build BST (cont’d) This approach has one serious drawback – All data must be put in an array before the BST can be created – Unsuitable or very inefficient when the BST has to be used while the data to be included in the BST are still coming

12 Approach 2: Self balancing with AVL Tree AVL trees offers another way to balance tree AVL trees maintain balance of BSTs while they are being created via insertions of data Tree rebalancing can be performed locally if only a portion of the tree is affected after insertion or deletion.

13 AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most by one – Each node contains a value (-1, 1, 0) indicating which subtree is "heavier”

14 Balance Factor – Each node is marked with a balance factor indicating which subtree is "heavier” – Balance Factor: height (right subtree) minus height (left subtree) – A balanced tree (-1, 1, 0)

15

16 AVL Implementation issues: – Insert and Delete are modified. They restructure the tree to make it balanced (if necessary)

17 Fixing Imbalances  An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1  There are two cases of imbalances: or CASE 1CASE 2 Imbalance caused by inserting node in left subtree of left child or right subtree of right child (i.e., insertion occurs on the “outside”) Imbalance caused by inserting node in right subtree of left child or left subtree of right child (i.e., insertion occurs on the “inside”)

18 Fixing Imbalances (cont’d)  Fixing an imbalance is done by rotating the tree  There are two types of rotation: single rotation  for CASE 1 imbalances double rotation  for CASE 2 imbalances  consists of two single rotations  The rotations must always preserve the BST property

19 Fixing Imbalances: Case node with imbalance A B C D ABCD This is a single right rotation. A single left rotation is symmetric. right rotate node 4 about 6

20 Fixing Imbalances: Case 1 (cont’d) left rotate node Q about P This is a single left rotation

21 Fixing Imbalances: Case node with imbalance A B C D ABCD STEP 1: left rotate node 4 about A B C D STEP2: right rotate node 4 about 6 node with imbalance

22 Fixing Imbalances: Case 2 (cont’d) STEP 1: Right rotate R about Q STEP 2: Left rotate R about P Note that P can be part of a larger AVL tree; it can be a child of some other node in the tree


Download ppt "Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered."

Similar presentations


Ads by Google