You are on page 1of 7

Fast Operations Binary Search Trees

<
2 1 6 9 4 = 8

What if we could do search, insert, and remove in O(log n)?


!

Log 1,048,576 = 20

>

Binary Search Trees

Binary Search Trees

Binary Search Tree (9.1)


A binary search tree is a binary tree storing keys (or key-element pairs) satisfying the following property:
!

Binary Search Tree (9.1)


Property - given a node with a value X, all the values of nodes in the left subtree are smaller than X and all the values of the nodes in the right subtree are larger than X
X

Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have v key(u) ! key(v) ! key(w) 6 u w
2 1 4 8 9

X X
3

X X

Values less than X

Values greater than X


Binary Search Trees 4

Binary Search Trees

Binary Search Tree (9.1)


External nodes do not store items? (Book says this) ! Not a good way to look at the tree
" Incredible waste of space

BST Operations
makeFromEmpty - initialize a new tree isEmpty - return true if empty, false if not search - return pointer to node in which key is found, otherwise return NULL findMin - return smallest node value findMax - return largest node value
Binary Search Trees 6

External nodes store items and children point to NULL An inorder traversal of a binary search trees visits the keys in increasing order
!

Binary Search Trees

BST Operations
insert - insert a new node into the tree maintaining BST property. All inserts are done at a leaf remove - remove a node from the tree maintaining BST property. display - print a tree in an order traversal

Array Implementation of a BST


A BST can be implemented with an array Given a node i
!

parent(i) = (i - 1)/2
" If i = 0, then no parent since root

leftChild(i) = 2i+1
" If 2i+1 <= N, otherwise no child

rightChild(i) = 2i+2
" If 2i+2 <= N, otherwise no child
Binary Search Trees 8

Binary Search Trees

Array Implementation of a BST


0 6 1 2 3 4 4 6 3 1 4 7 11 12
9

Array Implementation of a BST


In class exercise - show the array for the following tree
5 6 7 8
Binary Search Trees 10

3 11 1

7 12

0 5

2 6

6 7

7 14 8

Binary Search Trees

Linked Implementation of a BST


Array disadvantages
! !

Search (9.1.1)
To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return null Example: find(4)
<
2 1 6 9 4 = 8

Wasted space Not enough space Similar to linked list size can grow and shrink easily during runtime

Linked implementation
!

class Node { friend class Tree; private: itemtype item Node* left Node* right Node* parent }; class Tree { private: Node* root // internal functions public: // functions for // interface };
11

>

Binary Search Trees

Binary Search Trees

12

Search
Recursive implementation of search Node* search ( Node* nodePtr, itemtype key ) if (nodePtr == NULL) return NULL else if ( nodePtr->item == key ) return nodePtr else if ( nodePtr->item > key ) return search(nodePtr->left, key) else return search(nodePtr->right, key)
Binary Search Trees 13

Inorder Traversal
Recursive implementation of inorder traversal void inorder(node* nodePtr) if ( nodePtr != NULL ) inorder (nodePtr->left) print node inorder (nodePtr->right)

Binary Search Trees

14

Insertion (9.1.2)
To perform operation insertItem(k, o), we search for the position k would be in if it were in the tree All insertions create a new leaf node Example: insert 5
<
2 1 6 9 4

Insertion
> >
8 1 6 2 4 5 6 2 1 4 5 8 9 1 3 2 4 5 7 8 7 6 8

Insert 7
9

Insert 3
9

Binary Search Trees

15

Binary Search Trees

16

Insertion
In class exercise - create a BST by inserted the following integers in the given order
!

Deletion
Traverse tree and search for node to remove
!

68351074
3 1 0
Binary Search Trees

6 8 5 4
17

Five possible situations


" Item not found " Removing a leaf " Removing a node with one child - right only " Removing a node with one child - left only " Removing a node with two children

Binary Search Trees

18

Deletion - Removing a leaf


6 3 1 0 4 5 7 8 Remove 4 1 0 4 3 5 7 6 8

Deletion - Removing a node with children


Otherwise the node has children - find replacement node
!

If the left child exists


" Replace node information with the largest value smaller than the value to remove
!

findMax(leftChild)

Else there is a right child


" Replace node information with the smallest value larger than value to remove
!

findMin(rightChild)
Binary Search Trees 20

Binary Search Trees

19

Deletion - Removing a node with children (continued)


Splice out replacement node (call remove recursively) Just copy in info of replacement node over the value to remove (overload = if necessary)
!

Deletion
1 6 3 1 0 4 5 7 8 0 Remove 8

6 3 5 4 6 3 1 0 4
22

7 7

Note - this is NOT the best solution if you have a large data structure. The overhead of the copy is too great and you should move the node instead.

7 5 7

Delete replacement node if leaf


Binary Search Trees 21 Binary Search Trees

Deletion
1 6 3 1 0 4 0
Binary Search Trees

6 3 5 4 6 3 1 4
23

9 9

Deletion
6 3 8 5 4 3 5 8 4 4 9 0
Binary Search Trees

5 3 1 Remove 6 9 0 4 5 3 1 4
24

8 5 9

0 8 Remove 8 9

1 9 0 9 0

8 4 9

Deletion
6 3 1 0 8

In class exercise 1 0 0

3 8 9

Analysis of BST Operations


h =N h = log N

Remove 6

In class exercise
Worst Case empty search O(1) O(N) O(N) O(N) O(N) O(N) O(N) O(N2)
Binary Search Trees

Average Case O(1) O(log N) O(log N) O(log N) O(log N) O(log N) O(N) O(N log N)
26

3 9 1 0 8 9

findMin findMax insert remove display makeFromEmpty


25

Binary Search Trees

Analysis of BST Operations


Given a random ordering of insertions and deletions, the height of the tree will be quite close to log n We will learn later how to ensure the average case running times are also the worst case running times

Treesort
Uses a BST to sort records efficiently
!

Use makeFromEmpty
" Read in elements and insert in that order into a BST

Traverse inorder to read out nodes in ascending order Average case - O(N log N) Worst case - O(N2)
Binary Search Trees 28

Runtime
! !

Binary Search Trees

27

You might also like