advanced data structures and algorithms cosc-600 lecture presentation-6

Download Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6

Post on 24-Dec-2015

213 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6
  • Slide 2
  • TREES Trees are special subset of graphs. A tree is a collection of nodes. It consists of a) a distinguished node called root b) zero or more non empty subtrees T1,T2. Tk, each of these nodes connected by a directed edge from r c)recursive definition root T1 T2 Tk
  • Slide 3
  • Tree is a directed graph without a cycle 1.each node of a tree has only one parent node except root 2.there is only one path from root to each node, no cycle Path Path: a sequence of nodes n 1, n 2 .. n k such that n i is the parent of n i+1 Length of path: the number of edges Depth of n i : length of the path from root to n i Height of n i : length of longest path from n i to leaf node Leaf node: which has no child also called as terminal node
  • Slide 4
  • Linked list representation of a tree
  • Slide 5
  • Implementation: using linked lists
  • Slide 6
  • Tree Search (tree traverse) 1) Depth first search(DFS) use stacks 2) Breadth first search(BFS) Use Queues
  • Slide 7
  • Binary Search Tree each son of a vertex is distinguished either as a left son or as right son no vertex has more than one left son nor more than one right son a tree in which no node can have more than two children Operations: 1.Find(Search) 2.Traversal 3.Insert 4.Delete 5.Build
  • Slide 8
  • Find Operation Time Complexity O(Height of the Binary Search Tree) That is O(N) in worst case Example, A B C D E Height of the tree = N Thus, Order of growth = O(N)
  • Slide 9
  • Find (Worst Case Example) Order of growth will be O(N), no matter how the tree is structured. A D E B C A B C D E
  • Slide 10
  • 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 BC D F G J L M E HI K O N
  • Slide 11
  • 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.
  • Slide 12
  • Insert Operation 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) 5 74 2 5 7 4 2 6
  • Slide 13
  • Delete Operation 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 5 74 2 5 74 5 74 2 5 7 2
  • Slide 14
  • 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 94 2 6 5 8 9 2 6 5
  • Slide 15
  • Build Operation 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 5 10 21 32 7 5 10 21 32 7
  • Slide 16
  • Average Case Analysis
  • Slide 17
  • 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. 1.Must be easy to maintain. 2.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.
  • Slide 18
  • Ex:
  • Slide 19
  • To maintain balance condition we require additional operations after inserting or deleting a node. Note: Height information is kept for each node. After each insert operation, update the height of all the nodes from new inserted node to root node. Note: The min no. of nodes in an AVL tree of height h is S(h). S(h) = S(h-1)+S(h-2)+1 Ex: S(0)=1; S(1)=2; S(2)=4; S(4)=12; S(5)=20 All the tree operations = O(log n) To maintain AVL tree condition after each of insert or delete operations we need to perform Rotations.
  • Slide 20
  • Insertion Operation: If you want to insert 6 into above AVL tree the newly inserted node will be leaf node.
  • Slide 21
  • The above tree is not an AVL tree as node 8 violates the condition and height is not balanced. So, we need to rotate. Note: After one insertion only nodes that are on the path from the insertion point to the root might have their balance altered. Inserted node to root : update the balancing info. : node that must be rebalanced Case 1: Insertion into the left sub tree of the left child of Case 2: Insertion into the right sub tree of left child of Case 3: Insertion into the left sub tree of right child of Case 4: Insertion into the right sub tree of right child of
  • Slide 22
  • Types of rotation: Single Rotation: Case 1 and Case 4 Double rotation: Case 2 and Case 3. Case 1:
  • Slide 23
  • K2 is alpha. So perform single rotation.
  • Slide 24
  • Case 4:
  • Slide 25
  • After single rotation (counter-clock wise)
  • Slide 26
  • Case 3 Double rotation
  • Slide 27
  • Case 2 Double rotation
  • Slide 28
  • Upon the 1 st rotation we get the following tree.
  • Slide 29
  • Slide 30
  • To understand the double rotation consider for example tree where we insert 5.But upon such action height violation occur at the node 8.
  • Slide 31
  • Slide 32
  • But this tree is also not balanced we get a violation at node 4.So single rotation does not work here and hence we have to use double rotation for the tree. After double rotation we get tree as below.
  • Slide 33
  • SPLAY TREE It is a special case of Binary Search Tree and splay tree balanced Binary Search Tree. * Relatively Simple. * Guarantee that any M Consecutive tree operations starting from an empty BST take at most O(MlogN). * It does not mean O(logN) for single operation. * This just means O(logN) amortized cost per operation. Idea: After a node is accessed it is pushed to the root by a series of AVL tree rotations. Why?: Likely to be accessed again in the future ~ Locality. Does not require the maintenance or height/balance information.
  • Slide 34
  • Slide 35
  • After 1 st rotation
  • Slide 36
  • After 2 nd rotation
  • Slide 37
  • After 3rd rotation
  • Slide 38
  • After 4 th rotation
  • Slide 39
  • Problem: Another node might be as deep as tree height. (M.N) time for sequence of M operations. Method 2: Perform a series of special rotations. Zig-Zag. Zig-Zig. Let X be a (non-root) node on the access path at which we are rotating. Rule: If the parent of X is the root of the tree, merely rotate X and the root OTHERWISE X has both a parent and parent which are (P) and (G) respectively.
  • Slide 40
  • Case 1: Zig Zag
  • Slide 41
  • Case 2: Zig Zig
  • Slide 42
  • Delete Operation 1)Accessing the node to be deleted push up the node to root 2)Delete it (root) two subtrees Tl and Tr. 3)Find largest element in Tl It will be a root with right child of Tl. 4)Tr will be the right subtree of the root.
  • Slide 43
  • B-Tree (M and L) = Reduce the number of disc access time M-ary search Tree M-way branches M-1 keys Maintain M-ary search tree is balanced
  • Slide 44
  • Properties of B-Tree 1. Data items are stored at leaves. 2. Non-leaf nodes store up to (M-1) keys Key i represents the smallest key in subtree (i + 1) 3. Root is either a leaf or has between two & M children 4. All non-leaf nodes(except root) rhave between [M/2] & M children 5. All leaves are at the same depth & have between [L/2] & L children for same L
  • Slide 45
  • eg.
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • eg. deletion
  • Slide 50
  • eg. Assume that one block = 8192bytes = 8K each key = 32bytes link = 4bytes M-ary B-Tree M-1 keys M links 32(M-1)+4M