1 / 12

Advanced Data Structures and Algorithms

Advanced Data Structures and Algorithms. COSC-600 Lecture presentation-6. Binary Search Tree Operations. Find(Search) Traversal Insert Delete Build. Find Operation. Time Complexity – O(Height of the Binary Search Tree) That is O(N) in worst case Example,. Height of the tree = N

mona-joyner
Télécharger la présentation

Advanced Data Structures and Algorithms

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. Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6

  2. Binary Search Tree Operations • Find(Search) • Traversal • Insert • Delete • Build

  3. Find Operation • Time Complexity – O(Height of the Binary Search Tree) That is O(N) in worst case Example, Height of the tree = N Thus, Order of growth = O(N) A B C D E

  4. Find (Worst Case Example) Order of growth will be O(N), no matter how the tree is structured. A A B B C C D D E E

  5. Find Max and Find Min For Find Max and Find Min operations, the worst case will have the time complexity order of O(N). • L is the smallest value in this BST. • I is the largest value in this BST. • For sorting in ascending order use inorder (LVR) method. • For sorting in descending order use inorder (RVL) method. • It will have the order of O(N). A B C D E G F H I J K M L N O

  6. Traversal & Median Value • Inorder can be used to find the median value in the BST. • It will have the order of O(N). • We can use the balanced binary search tree in order to change the order O(N) to O(logN). • Traversal in a BST will have the order O(N). • Recursion can be used for traversal operation.

  7. Insert Operation 5 • Always follow the BST rules while inserting a new node in the tree. • Case 1) New node will always be a terminal node. • Example ( 2 is the new node) Case 2) In order to find the location to insert the node in some cases when following the BST protocols. The complexity will be in the order O(N). Example ( 6 is the new node) Time Complexity worst case – O(Height of the BST) 4 7 2 5 4 6 2 7

  8. Delete Operation 5 5 4 7 • Case 1) The deleting node has no child ≡ terminal node (leaf) => just delete it ! • Example • Case 2) The deleting node has only one child => reconnect the child to its parent node. • Example 4 7 2 5 5 4 7 2 7 2

  9. Delete Operation • Case 3) The deleting node has two children (two sub trees). • A) Find the smallest node from its right sub tree and replace the deleting node with it and delete the replaced node. • B) Find the largest node from its left sub tree and replace the deleting node with it and delete the replaced node. • Time Complexity worst case – O(Height of the BST) • Example (A) 8 8 9 4 9 5 6 6 2 2 5

  10. Build Operation 5 • For the following N elements, the BST is build based on the input from left to right. Example 5,10,21,32,7 Example 10,7,5,21,32 10 21 7 32 10 21 7 5 32

  11. Average Case Analysis • T(N)=0+1+2+3+…+(N-1) ≠ O(N) because we are looking for the worst case which = O(N2) And in average case time complexity = O(log N) , (this one can the best case too ). Average depth all nodes in a tree is O(log N). On the assumption that all insertion sequences are “equally likely ”. Some of depth of all nodes ≡ internal path length. • Time complexity of Insert/Delete Pairs in a BST is O(). • Time complexity of Insert Operation on the average is O(N log N). • Time complexity of the height on the average is O(N log N). • Theorem The expected depth of tree needed to insert N random elements in to an initially empty binary search tree is O(N log N) for N>=1.

  12. Balanced Binary Search Tree ( AVL Tree ) The difference of depth between any terminal node in a binary tree should be at most 1. • AVL Tree is a BST which satisfies the balance condition. • Must be easy to maintain. • Depth of tree is O(log N). • How to implement it ? • Reshape the AVL tree if the depth difference is more than 1. • Rotation • Single Rotation • Double Rotation • Height of empty AVL tree = -1 (Purely Mathematical assumption) • Definition of Balance Condition • For “every” node in AVL tree, height of left and right sub tree can differ by at most 1.

More Related