Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.

Similar presentations


Presentation on theme: "Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree."— Presentation transcript:

1 Binary Search Trees

2 A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree and smaller than all elements in its right sub-tree. 810 184 2 6

3 Binary Search Trees Binary search tree is a data structure that efficiently supports all dictionary operations. Each node has at most two Childs that can be identified as the left and right, and a data field. Implementing a binary tree is like implementing a linked list, where each node has two next nodes Sometimes it will be helpful to have a pointer from each node to its parent

4 BST Vs. Heap

5 Motivation Binary search on ordered arrays is efficient, However insertion of an item in an ordered array is slow: O(N) Ordered arrays are best suited for static searching, where search space does not change. Binary search trees can be used for efficient dynamic searching.

6 Searching in a BST Begin in the root. Go left or right depending if the value you are looking for is than the current node. treeSearch (Node x, int k) { if (x == null or k = x.key) return x if (k < x.key) return treeSearch(x.left, k) else return treeSearcg (x.right, k)

7 Searching a BST Searching time is proportional to the height of the tree. The height is O(logn) if the tree is balanced and could be as worse as O(n) for an unbalanced tree.

8 Minimum element treeMinimum (Node x) { while (x.left != null) x = x.left return x }

9 Maximum element treeMaximum (Node x) { while (x.right != null) x = x.right return x }

10 Predecessor If x has two children then the predecessor is the largest child in x left sub tree. Go left and then right all the way down If it does not have a left child, a node's predecessor is its first left ancestor.

11 Predecessor I II III

12 Successor If x has two children then the successor is the smallest child in x right sub tree. Go right and then left all the way down If it does not have a right child, a node's successor is its first right ancestor.

13 I III II Successor

14 treeSuccessor(Node x) { if (x.right != null) return treeMinimum(x.right) else y = x.parent while (y!= null && x= y.right) x = y y = y.parent return y

15 Inserting an element Find the element location using the search operation. Replace the null value with the new node.

16 Insert 15 25 30 10 70 2 25 30 10 70 215

17 Inserting an element treeInsert(Tree t, Node z) y = null x = t.root while (x != null) { y = x if (z.key < x.key) x = x.left else x = x.right } z.setParent (y) if(y = null){ t.setRoot(z) } else if z.key < y.key y.setLeft(z) else y.setRight(z)

18 Deleting a key from a BST Deleting is more complicated, because the node that should be deleted may have children, which could be effected. Case 1: if the node is a leaf, just set the references from it’s parent to null

19 Case 1 – Delete leaf Delete 20

20 Deleting a key from a BST Case 2: if the deleted node has one child, just cut off the node Case 3: if the node has two Childs then we replace the contents of the node with it’s successor (which has at most one child) and remove the successor

21 Case 2: node with single child Delete 7

22 Case 3 – node with two children Delete 6

23 Questions 1.Draw binary search trees of height 2,3,4,5 and 6 on the set of keys {1,4,5,10,16,17,21}

24 Questions 2. Suppose have numbers between 1 and 1000 in a binary search tree and we are searching for 363. Which of the following sequences could not be a search sequence? a.2, 252, 401, 398, 330, 344, 397, 363 b. 924, 220, 911, 244, 898, 258, 362, 363 c. 925, 202, 911, 240, 912, 245, 363 d. 2, 399, 387, 219, 266, 382, 381, 278, 363 e. 935, 267, 347, 621, 299, 392, 358, 363

25 Questions 3. Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 into an initially empty binary search tree. Show the result of deleting the root.

26 Questions 4. Write a method that returns the number of elements in a binary search tree.

27 Interval trees The intent of interval trees is to maintain data dealing with intervals, where an interval [t1,t2] is defined as an event from t1 to t2 where t1<t2 Interval trees support insert, delete and search operations

28 Interval Node An interval node has the following properties: T1, T2 the start and end of the interval A max value, storing the max value of the sub tree rooted from this node T1,T2 max

29 Example 1,10 20 0-10 10 7-20 20

30 Insertion to an interval tree T1 is used as the key for insertion. So the basic insertion step is the same as in a regular binary tree. The difference is in updating the maximum field

31 Insertion to an interval tree insert [10,12] [5,10] [15,16] [13,30] [20,22] 10,12 12 16 30 5-10 10 15-16 16 30 13-30 30 20-22 22

32 Interval search Searching is done by providing an interval and looking for an overlapping interval (which may be more than one) in the tree Two intervals x,y overlap if

33 Interval Search intervalSearch(Tree t, Interval y) x  root(T) while(x != null && !x.overlap(y)) if left(x) != null && max(left(x)) >= low(y)) x  left(x) else x  right(x) return x;

34 Interval Search Go right when: –No left child –Max(left(x)) < low(y) Go left when: –There is a left child and max(left(x)) >= low(y)


Download ppt "Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree."

Similar presentations


Ads by Google