Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Rem Collier Room A1.02

Similar presentations


Presentation on theme: "Binary Search Trees Rem Collier Room A1.02"— Presentation transcript:

1 Binary Search Trees Rem Collier Room A1.02
School of Computer Science and Informatics University College Dublin, Ireland

2 Binary Search Tree A Binary Search Tree is a Binary Tree that satisfies the following properties: Each internal node holds a (unique) value. A total-order relation (~) is defined on those values. Reflexive: k ~ k Antisymmetric: if k1 ~ k2 and k2 ~ k1 then k1 = k2 Transitive: if k1 ~ k2 and k2 ~ k3 then k1 ~ k3 All the values (ki) in the left sub-tree of a node with value k satisfy the relation ki ~ k. All the values (kj) in the right sub-tree of a node with value k satisfy the relation k ~ kj. Useful Feature: An in-order traversal of a binary search trees visits the values in the order specified by the total-order relation.

3 Binary Search Tree Example
Consider a Binary Search Tree for integer values, using the less than (≤) total order relation. The state of a Binary Search Tree depends on the order in which items are added. 6 9 2 4 1 8

4 Uses of BSTs Binary Search Trees are an implementation strategy for other ADTs. Support three basic operations: insert(e): Add a new node containing e, if one does not already exist find(e): Find the node containing e remove(e): Remove the node containing e Can easily be adapted to store entries and support retrieval by key. Do you know of any other ADTs that have similar operations?

5 Search To search for a value 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 value of the current node If we reach a leaf, the key is not found and we return null Example: find(4) Algorithm find(k, v) if T.isExternal(v) return null if k < v.element() return find(k, T.leftChild(v)) else if k = v.element() return v else return find(k, T.rightChild(v)) < 6 2 > 9 1 4 = 8

6 Insertion To perform operation insert(k), we search for value k
Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert(5) < 6 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5

7 Deletion To perform operation remove(k), we search for k.
Assume k is in the tree, and let let v be the node storing k If node v has one child, then we can remove it using the existing remove(v) operation. Example: remove(4) 6 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8

8 Deletion (cont.) What about the case where the k to be removed is stored at a node v that has 2 children? we find the internal node w that follows v in an inorder traversal we copy w.element() into node v we remove node w and its left child z (which must be a leaf) Example: remove(3) 1 v 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9

9 Problems Create the following BST:
insert(15), insert(25), insert(5), insert(9), remove(15) insert(37), insert(26), insert(35), insert(28), remove(26) insert(40), insert(1), insert(15), insert(26), insert(6), remove(5) insert(H), insert(A), insert(P), insert(Y), insert(O), remove(H) insert(B), insert(C), insert(F), insert(E), remove(A) insert(H), insert(A)

10 Performance Consider a binary tree containing n items that is of height h the space used is O(n) methods find(), insert() and remove() take O(h) time The height h is O(n) in the worst case and O(log n) in the best case! The best case arises when the tree is balanced!


Download ppt "Binary Search Trees Rem Collier Room A1.02"

Similar presentations


Ads by Google