Professional Documents
Culture Documents
<
2 1 6 9 4 = 8
Log 1,048,576 = 20
>
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
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
!
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
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
3 11 1
7 12
0 5
2 6
6 7
7 14 8
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
>
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)
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
15
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
18
findMax(leftChild)
findMin(rightChild)
Binary Search Trees 20
19
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
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
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
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
! !
27