You are on page 1of 7

9/14/2013

COMPSCI 105 Principles of Computer Science

Content
1. Binary Search Tree Definition 2. ADT Binary Search Tree 3. Operations on ADT

RESOURCES: Lecture Notes Textbook: - Section 11.3 Binary Search Trees - Exercises

Binary Search Trees

Binary Search Tree


Retrieve (Search) Insert Delete

1. 2. 3.

COMPSCI 105 S2 C

1. Binary Search Tree Definition


A Binary Search Tree is a Binary Tree, which for each node n

Binary Search Tree Example


Example: All binary search trees for the values 1,2 and 3 are

satisfies the following three properties:

ns value is greater than all values in its left subtree TL ns value is less than all values in its right subtree TR Both TL and TR are Binary Search Trees
3 COMPSCI 105 S2 C

60

20

70

10

40

30

50

COMPSCI 105 S2 C

9/14/2013

2. ADT Binary Search Tree


A Binary Search Tree is a

ADT Binary Search Tree (cond)


A data item in a Binary Search Tree has a specifically designated search key

Binary Tree which fulfills the Binary Search Tree properties In order to guarantee that properties are fulfilled the access operations must be restricted In order to find and compare elements we need a searchKey which is comparable
The nodes of this tree have a searchKey attribute to compare and identify them
5 COMPSCI 105 S2 C 6

which is used to find items in the tree


Public abstract class KeyedItem<KT extends Comparable<? super KT>> { private KT searchKey; Public KeyedItem(KT key) { Wildcard representing any searchKey = key; supertype of KT } // end constructor public KT getKey() { return searchKey; } // end getKey } // end KeyedItem

COMPSCI 105 S2 C

Search Key Example


A Person class with the persons ID number as the search

key.

3. Operations of ADT Binary Search Tree


Retrieve the item with a given search key from a Binary Search

public class Person extends KeyedItem<String> { private FullName name; private String phoneNumber; private Address address; public Person(String id, FullName name, String phone, Address addr) { super(id); this.name = name; phoneNumber = phone; address = addr; } // end constructor This initialises the key in the super public String toString() { Class with the id return getKey() + "#" + name; } // end toString }
7 COMPSCI 105 S2 C

Tree
retrieve(searchKey)

Insert a new item into a Binary Search Tree


insert(newItem)

Delete the item with a given search key from a Binary Search Tree
delete(searchKey)

COMPSCI 105 S2 C

9/14/2013

3.1 Retrieve algorithm


Use properties defining the Binary Search

Retrieve algorithm example


Search for 3
60

Key, i.e.
For a node n all items in its left subtree are

smaller than n
For a node n all items in its right subtree are
20 70

3<8 3<5 3>2


50 10 40

larger than n
The subtrees are again Binary Search Trees
ALGORITHM If (tree==null) return null (item not found)

30

Else compare searchKey with key of node If smaller search recursively in left subtree If larger search recursively in right subtree
9 COMPSCI 105 S2 C 10 COMPSCI 105 S2 C

Found 3

Retrieve algorithm example


Search for 6 6<8 6>5 Right child of 5 is null => Item not found, return null
11 COMPSCI 105 S2 C

Retrieve algorithm
protected KeyedItem retrieveItem(TreeNode<T> tNode, KT searchKey) { T treeItem; if (tNode == null) { treeItem = null; } else { T nodeItem = tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey()) == 0) { // item is in the root (of the current subtree) treeItem = (KeyedItem)tNode.getItem(); } else if (searchKey.compareTo(nodeItem.getKey()) < 0) { // search the left subtree treeItem = retrieveItem(tNode.getLeft(), searchKey); } else { // search the right subtree treeItem = retrieveItem(tNode.getRight(), searchKey); } // end if } // end if return treeItem; } // end retrieveItem
12 COMPSCI 105 S2 C

9/14/2013

3.2 Insert algorithm


Similar to the find algorithm Note that algorithm also works if there are
60

Insert algorithm example


Insert 7 7<8
20 70

duplicate keys!!
10

7>5
40

ALGORITHM: If (node == null) make new node with the new item. Else compare key of new item with key of node If smaller insert recursively in left subtree If larger insert recursively in right subtree

7
50

30

13

COMPSCI 105 S2 C

14

COMPSCI 105 S2 C

Right subtree empty => insert new node 7

Insert algorithm example


Insert 6 6>5 6<7 7
6
15 COMPSCI 105 S2 C

Insert algorithm
protected TreeNode insertItem(TreeNode<T> tNode, KT newItem){ TreeNode<T> newSubtree; if (tNode == null) { // position of insertion found; insert as new leaf tNode = new TreeNode<T>(newItem, null, null); return tNode; } // end if T nodeItem = tNode.getItem(); // search for the insertion position if (newItem.getKey().compareTo(nodeItem.getKey()) < 0) { // search the left subtree newSubtree = insertItem(tNode.getLeft(), newItem); tNode.setLeft(newSubtree); return tNode; } else { // search the right subtree newSubtree = insertItem(tNode.getRight(), newItem); tNode.setRight(newSubtree); return tNode; } // end if }COMPSCI 105 S2 C // end insertItem

6<8

Left subtree empty => insert new node 6

16

9/14/2013

3.3 Delete algorithm


Locate the node N containing item with the specified search key (like retrieve operation) Three cases for the node N containing the item to be deleted
1. 2.

Delete Algorithm Example - Case 1 (node is leaf)


Delete 6 6>5 6<7 7
6
18 COMPSCI 105 S2 C

N is a leaf => Delete N has only one child


6<8

2(a): N has a left child => Replace N with left child 2(b): N has a right child => Replace N with right child => Replace N with its successor in inorder, i.e. the leftmost node of the right subtree. (Note: Since the successor is the leftmost node, it does not have a left child, i.e. can be removed with case 1 or case 2(b))

3.

N has two children

6=6 => Found 6 is leaf => Delete

17

COMPSCI 105 S2 C

Delete algorithm
protected TreeNode deleteItem(TreeNode<T> tNode, KT searchKey){ TreeNode<T> newSubtree; if (tNode == null) { throw new TreeException("TreeException: Item not found"); } else { T nodeItem = tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey()) == 0) { // item is in the root of some subtree tNode = deleteNode(tNode); } // delete the item else if (searchKey.compareTo(nodeItem.getKey()) < 0) { // search the left subtree newSubtree = deleteItem(tNode.getLeft(), searchKey); tNode.setLeft(newSubtree); } else { // search the right subtree newSubtree = deleteItem(tNode.getRight(), searchKey); tNode.setRight(newSubtree); } // end if } // end if return tNode; } // end deleteItem COMPSCI 105 S2 C

Delete Algorithm - Case 2 (node has only one child)


Replace N with Ns child (node that this can be a subtree)

N has only a left child

after delete N
20 COMPSCI 105 S2 C

19

9/14/2013

Delete Algorithm Example - Case 2 (node has only one child)


Delete 7 7>5
7 6

Delete Algorithm Example - Case 3 (node has two children)


Locate another node M that is easier to remove from the tree than

node N

Copy the item that is in M to N, thus effectively deleting from the

7<8 7=7 => Found 7 has only left child 6 => Delete and replace with child

tree the item originally in N

Remove the node M from the tree NOTE: M must be chosen carefully

so that Binary Search tree properties are maintained!


WRONG SOLUTION!!!
22 COMPSCI 105 S2 C

21

COMPSCI 105 S2 C

Delete Algorithm Example - Case 3 (node has two children)


Replace N with a node M which has maintains the Binary Search Tree

Delete Algorithm Example - Case 3 (node has two children)


Delete 9 9>7 9 has two children => Find => leftmost element in Found 6 10 9 9=9 right subtree 11 is root of right subtree 11 Go left until left child is 8 10null. has no left child => 10 Replace 9 with 10 and delete node
7

property and is easy to remove

Rather than replacing the node N we just copy the key from M to N Then we delete M M is the inorder successor of N [= the smallest node greater than N = the leftmost node in the right subtree)
23 COMPSCI 105 S2 C 24

COMPSCI 105 S2 C

9/14/2013

Delete a Node algorithm


protected TreeNode deleteNode(TreeNode<T> tNode) { T replacementItem; if ( (tNode.getLeft() == null) && Case 1 (tNode.getRight() == null) ) { // is a leaf node return null; } // end if leaf else if (tNode.getLeft() == null) { // has only right child return tNode.getRight(); Case 2(a) } // end if no left child else if (tNode.getRight() == null) { // has only left child return tNode.getLeft(); Case 2(b) } // end if no right child else { // there are two children // retrieve and delete the inorder successor Case 3 replacementItem = findLeftmost(tNode.getRight()); tNode.setItem(replacementItem); tNode.setRight(deleteLeftmost(tNode.getRight())); return tNode; } // end if COMPSCI 105 S2 C 25 } // end deleteNode

Summary
The Binary Search Tree allows to use a binary search like algorithm to

search for an item with a specific value operations

The shape of a Binary Search Tree determines the efficiency of its The height of a binary search tree with n nodes can range from a minimum The closer a Binary Search Tree is to a balanced tree, the more efficient

of log2(n + 1) to a maximum of n

26

COMPSCI 105 S2 C

You might also like