Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6."— Presentation transcript:

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

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

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

4 Linked list representation of a tree

5 Implementation: using linked lists

6 Tree Search (tree traverse) 1) Depth first search(DFS)……………… use stacks 2) Breadth first search(BFS) …………… Use Queues

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

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)

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

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

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.

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

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

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

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

16 Average Case Analysis

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.

18 Ex:

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.

20 Insertion Operation: If you want to insert 6 into above AVL tree the newly inserted node will be leaf node.

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 α

22 Types of rotation: Single Rotation: Case 1 and Case 4 Double rotation: Case 2 and Case 3. Case 1:

23 K2 is alpha. So perform single rotation.

24 Case 4:

25 After single rotation (counter-clock wise)

26 Case 3 – Double rotation

27 Case 2 – Double rotation

28 Upon the 1 st rotation we get the following tree.

29

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.

31

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.

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.

34

35 After 1 st rotation

36 After 2 nd rotation

37 After 3rd rotation

38 After 4 th rotation

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.

40 Case 1: Zig Zag

41 Case 2: Zig Zig

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.

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

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

45 eg.

46

47

48

49 eg. deletion

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 <= 8192 M=228 L value? 8192/record size(256byte) = 32records/leaf

51 10 million records 1.Each leaf has between 16 & 32 records 2.Each internal node(except root) at least 114 branches  10,000,000 / 16 = 625,000 leaves (worst case)  Worst case, depth of B-Tree?  Log 114 10,000,000


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

Similar presentations


Ads by Google