Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.

Similar presentations


Presentation on theme: "Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1."— Presentation transcript:

1 Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

2 Outline  2.6 Dealing with Non-Unique Keys  2.7 Queries for the Keys in an Interval  2.8 Building Optimal Search Trees  2.9 Converting Trees into Lists  2.10 Removing a Tree 2

3  Complete Binary Tree : All level except leaves are completely filled. Max # of nodes @ level i=2^i Height (h) with n nodes = log2 n  Perfect Binary Tree: All levels completely full Max # of nodes in tree with height h is = 2^0 + 2^2 +…. +2^h =2^(h+1) -1 = 2^(# of levels)-1 Height (h) with n nodes = log2 (n+1) -1 ( n = 2^(h+1) -1  2^(h+1) = (n+1)  h = log2 (n+1) -1 ) Time complexity = O(h)  O(log2 n) 3 Terminology:

4  Balanced binary tree : Difference between height of left and right subtree for every node is not more than 1.  Height of a node : # of edges in longest path from the node to a leaf node>  Depth of a node : # of edges in path from root to the node 4 Terminology:

5 Traversal Orders  The three traversal orders are: A. Breadth-first B. Depth-first: 1- Preorder: Process the value in the current node first, then its left and right subtrees. 5

6 Traversal Orders 2- Inorder (symmetric): process the left subtree first, then the value in the current node, then its right subtree. 6

7 Traversal Orders 3- Postorder: process the left and right subtrees first, then the value in the current node. 7

8 Linked data structure  Linked list  Chains  Doubly linked 8

9 2.6 Dealing with Non-Unique Keys  Store many objects with the same key value.  Keep all elements of the same key in a linked list  The correct reaction is as follows: 1- Find : produces all elements of that list O(h + k) k is the number of elements that find returns. 2- Insert: inserts at the beginning of the list O(h). 9

10 2.6 Dealing with Non-Unique Keys 3- Delete: deletes all items of that key in time O(h). 1- Delete the references to the objects :  Additional node between the leaf and the linked list. ( contains pointers to the beginning and to the end of the list)  Transfer the entire list with O(1) operations to the free list. 2- Delete the objects  walking along this list, but not in O(1). (time independent of the # of objects) 10

11 2.7 Queries for the Keys in an Interval  We store all those branches on a stack.  We have two keys [a,b[ left and right.  Use left as a Binary search tree key.  Largest end point of subtree in right. 11

12 Insert Interval [a,b[ 12  Insert into BST, using a as the key.  Update max in each node on search path.

13 Search Interval [a,b[  If interval in node intersects query interval, return it. interval [a, b[ (lo,hi).  Else if left subtree is null, go right  Else if max endpoint in left subtree is less than a, go right  Else go left. 13

14 Search Interval [a,b[ 14

15 2.7 Queries for the Keys in an Interval We Have tow Methods: 1- Method One Change Tree Structure:  Need to organize the leaves into a doubly linked list  Moving between nodes cost O(1).  Need to change the insertion and deletion functions. (cost only O(1) additional time)  Query takes O(k) additional time if it lists, a total of k keys in the interval. 15

16 2.7 Queries for the Keys in an Interval 16 2- Method Two Change Query Function:  Down the tree with the query interval instead of the query key.  Search for any one interval they intersects query, If interval in node intersects query interval, return it. interval [a, b[:  Else if left subtree is null, go right  Else if max endpoint in left subtree is less than a, go right  Else go left.  We have i + 1 leaves, when i interior nodes between paths,  total number of nodes visited is at most twice the # of nodes visited in a normal find operation plus O(k) for k leaves.

17 17

18 2.8 Building Optimal Search Trees We can construct an optimal search tree from a given set of (key, object) pairs. The search tree will be as static data structure: there are no inserts and deletes, In this case :  No problem for rebalancing the tree,  But if we build it, knowing the data in advance. The primary issue is the height  Because a search tree of height h has at most 2^h leaves, an optimal search tree for a set of n items has height (log n), where the log, as always, is taken to base 2. 18

19 How to construct search tree with optimal height? Bottom-up. Top-down. Assuming that the (key, object) pairs are given in sorted list (increased order). 19

20 Bottom-Up method The construction of Bottom_Up is easy.  One views the initial list as a list of one element trees.  Then, one goes repeatedly through the list, joining two consecutive trees. Until there is only one tree left. Assume here that:  the list items are themselves of type tree_node_t.  Left entry pointing to the object.  The key containing the object key.  The right entry pointing to the next item. Or NULL. Create a list, where all the nodes of the previous list are attached as leaves. Then maintain a list of trees, where the key value is the smallest key value in the tree below it. 20

21 O(n) 21

22 22

23 The Bottom_Up method is O(n) The disadvantage of this method is that:  the tree is unbalanced although it has optimal height. The first half of the algorithm, duplicating the list and converting all the original list nodes to leaves, takes obviously O(n) ; it is just one loop over the length of the list. The second half has a more complicated structure, but in each execution of the body of the innermost loop,  One of the n interior nodes created in the first half is removed from the current list and put into a finished subtree,  So the innermost part of the two nested loops is executed only n times. 23

24 Top-down method The top-down construction is easiest to describe recursively:  divide the data set in the middle,  create optimal trees for the lower and the upper halves,  And join them together. This division is very balanced; in each subtree the number of items left and right differs by at most one, and it also results in a tree of optimal height. But if we implement it this way, and the data is given as list, it takes ( n log n ) time,  because we get an overhead of ( n ) in each recursion step to find the middle of the list.  But there is a nice implementation with O(n) complexity using a stack. 24

25 How does it works First construct the tree “in the abstract,” without filling in any key values or pointers to objects. Then, find the middle of the list; by keep tracking the number of elements that should go into the left and right subtrees. Building abstract tree using a stack: 1. We initially put the root on the stack, labeled with the required tree size; 2. Then we continue, until the stack is empty, to take nodes from the stack, attach them to two newly created nodes labeled with half the size, 3. And put the new nodes again on the stack. If the size reaches one, we have a leaf, so node should not be put back on the stack but should be filled with the next key and object from the list. The problem is to fill in the keys of the interior nodes, which become available only when the leaf is reached.  Each item on the stack needs two pointers,  One to the node that still needs to be expanded and one to the node higher up in the tree, where the smallest key of leaves below that node should be inserted as comparison key.  Also, each stack item contains a number, the number of leaves that should be created below that node. 25

26 The Top_Down method is O(n) To analyze this algorithm:  In each step on the stack, we create either two new nodes, and there are only n − 1 new nodes created in total,or we attach a list item as leaf, and there are only n list items. So the total complexity is O(n). 26

27 27

28 2.9 Converting Trees into Lists  Using a trivial depth-first search (Stack).  Converts tree of n leaves into a list of n elements.  Converting time complexity is O(n)  Using array as a preferred method If height of tree is not too large.  Array size needs to be at least as large as the height of the tree 28

29 2.10 Removing a Tree  No longer need it  To avoid a memory leak  Time to Remove a tree structure related to the size.  Rotations in the root till the left-lower neighbor is a leaf;.  Returns that leaf.  Moves the root down to the right,  Returns the previous root. 29

30 Thanks 30


Download ppt "Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1."

Similar presentations


Ads by Google