Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every...

34
Binary Search Trees

Transcript of Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every...

Binary Search Trees

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 and smaller than all elements in its right sub-tree.

8

10

184

2 6

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

BST Vs. Heap

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.

Searching in a BST

• Begin in the root. Go left or right depending if the value you are looking for is < or > than the current node.

• treeSearch (Node x, int k) {if (x == null or k = x.key)

return xif (k < x.key)

return treeSearch(x.left, k)else return treeSearcg (x.right, k)

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.

Minimum element

treeMinimum (Node x) {

while (x.left != null)

x = x.left

return x

}

Maximum element

treeMaximum (Node x) {

while (x.right != null)

x = x.right

return x

}

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.

PredecessorI

IIIII

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.

I

IIIII

Successor

Successor

treeSuccessor(Node x) {if (x.right != null)

return treeMinimum(x.right)else

y = x.parentwhile (y!= null && x= y.right)

x = yy = y.parent

return y

Inserting an element

• Find the element location using the search operation. Replace the null value with the new node.

Insert 15

25

3010

702

25

3010

702 15

Inserting an element

treeInsert(Tree t, Node z)y = nullx = t.rootwhile (x != null) {

y = xif (z.key < x.key)

x = x.leftelse

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)

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

Case 1 – Delete leaf

• Delete 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

Case 2: node with single child

• Delete 7

Case 3 – node with two children

• Delete 6

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}

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, 363b. 924, 220, 911, 244, 898, 258, 362, 363c. 925, 202, 911, 240, 912, 245, 363d. 2, 399, 387, 219, 266, 382, 381, 278, 363e. 935, 267, 347, 621, 299, 392, 358, 363

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.

Questions

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

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

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

Example

• 1,10

20

0-10

10

7-20

20

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

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

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

( ( ) lo ( )) ( ( ) ( ))high x w y and low x high y

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))

xleft(x)

else

xright(x)

return x;

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)