binary search trees - york university · binary search trees binary search tree ... binary search...

33
1 Binary Search Trees Binary Search Trees Outline and Required Reading: Outline and Required Reading: COSC 2011, Fall 2003, Section A Instructor: N. Vlajic The Dictionary ADT (§ 8.1) Binary Search Trees (§ 9.1)

Upload: ngonhi

Post on 14-Jun-2018

249 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

1

Binary Search TreesBinary Search Trees

Outline and Required Reading:Outline and Required Reading:

COSC 2011, Fall 2003, Section AInstructor: N. Vlajic

• The Dictionary ADT (§ 8.1)• Binary Search Trees (§ 9.1)

Page 2: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

2

Dictionary ADT

Dictionary Dictionary – searchable collection of (k, e) pairs, also called “items”•••• k = key – used to facilitate search for given item•••• e = element – information of interest•••• examples: a) student records

•••• key = student’s ID number•••• element = student’s name, address, grades

b) phone book•••• key = person’s name•••• element = person’s phone number

Operations on DictionariesOperations on Dictionaries – dictionaries support following operations:(1) searching(2) inserting(3) deleting

Page 3: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

3

Dictionary ADT (cont.)

Ordered Dictionaries Ordered Dictionaries – “order” relations are defined on the keys,so the relative order of any two keys canbe determined, e.g. key(p) ≤≤≤≤ key(r)

•••• examples: a) student recordsID_number_1 > ID_number_2

b) phone bookname_1 > name_2

Unordered Dictionaries Unordered Dictionaries – no “order” relation is assumed on thekeys, and only equality testing betweenkeys can be done

•••• example: a) colour-based dictionaryitem_1 = (blue, see)item_2 = (blue, sky)item_3 = (green, grass)

In general, it is possible to have multiple items with the same key.

Page 4: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

4

Dictionary ADT: Interface

Interface Methods Interface Methods public int size();/* return the number of items in D */

public boolean isEmpty();/* return true if D is empty */

public ObjectIterator elements();/* return the elements stored in D */

public ObjectIterator keys();/* return the keys stored in D */

public Object findElement(Object k);/* if D contains item with key equal to k, then return *//* element of such item, else return NO_SUCH_KEY */

public ObjectIterator findAllElements(Object k);/* return an iterator of all elements with key equal to k */

Page 5: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

5

public void insertItem(Object k, Object e);/* insert an item with element e and key k into D */

public Object removeElement(Object k);/* remove from D item with key equal to k, and return its *//* element; if D has no such item, return NO_SUCH_KEY */

public ObjectIterator removeAllElements(Object k);/* remove from D items with key equal to k, and return an *//* iterator of their elements */

Dictionary ADT: Interface (cont.)

Page 6: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

6

Dictionary ADT: What Standard Java Offers!?

Map Interface in java.util.*[ from: http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html ]

Implementing Classes: MapTree, Hashtable, WeakHashMap, etc.

! “Map” is an object that maps keys to values.

! A map cannot contain duplicate keys; each key can map to at most one value.

! Some map implementations, like the TreeMap class, make specific guaranteesas to their order; others, like the HashMap class, do not.

The Map.Entry Interface! The Map interface contains a static nested interface called Entry.

! Map.Entry is used to access key-value pairs as individual objects outside of the Map.

! Key methods of the Map.Entry interface: • Object getKey() - Returns the key portion of this Entry. • Object getValue() - Returns the value portion of this Entry. • Object setValue(Object value) - Replaces the value portion of this Entry with the

parameter. The replaced value is returned.

Page 7: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

7

Map Interface - Method Summary boolean containsKey(Object key)

Returns true if this map contains a mapping for the specified key. boolean containsValue(Object value)

Returns true if this map maps one or more keys to the specified value. boolean equals(Object o)

Compares the specified object with this map for equality. Object get(Object key)

Returns the value to which this map maps the specified key. boolean isEmpty()

Returns true if this map contains no key-value mappings. Object put(Object key, Object value)

Associates the specified value with the specified key in this map (optional operation).

Object remove(Object key) Removes the mapping for this key from this map if present (optional operation).

int size() Returns the number of key-value mappings in this map.

Dictionary ADT: What Standard Java Offers!?

Page 8: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

8

Binary Search Trees

Binary Search Tree Binary Search Tree – binary tree T such that each internal node v ofT stores an item (k,e) of a dictionary D, and •••• all keys stored at nodes in left subtree of v

are less than k=key(v)•••• all keys stored at nodes in right subtree of v

are greater than or equal to k=key(v)

key(v.leftChild) < key(v) ≤≤≤≤ key(v.rightChild)

10

5 14

1 7

10

5 14

1 12

This is NOT a BST !This is a BST !

Page 9: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

9

Binary Search Trees (cont.)

CS12745[Bill T.,

M2M 3Z2]

CS12369[John L.,

M3R 4H1]

CS25376[Ann M.,M7D 9I4]

CS12599[Peter K.,M9S 7K2]

Example 1 [ database of student records as a BST ]

CS12131[Maya R.,M6Y O9J]

keyused to facilitate

ordering, searching,inserting in the BST

elementinformation of users’ interest –

irrelevant from the BSTpoint of view

Page 10: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

10

Properties ofProperties of •••• external nodes are Null_Nodes – they do not Binary Search TreesBinary Search Trees store any key or element of D !!!

(used for coding simplicity)•••• the leftmost item has the smallest key •••• the rightmost item has the largest key•••• an inorder traversal of a binary search tree

visits the keys in non-decreasing order !!!

Binary Search Trees (cont.)

44

5617

325 48

51

Schedule of Inorder Visits: 5, 17, 32, 44, 48, 51, 56

Page 11: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

11

Other Properties ofOther Properties of •••• all keys greater than a given key k are inBinary Search TreesBinary Search Trees (1) the right subtree of node p associated with k, and

(2) the right subtrees of all right-ancestors of p

•••• all keys smaller than a given key k are in(1) the left subtree of node p associated with k, and(2) the left subtrees of all left-ancestors of p

Binary Search Trees (cont.)

15 58

47ra1(p)2010 68

603317 p

ra2(p)

right subtree rooted at ra2

3 11

la1(q)

q

27

left subtree rooted at la2

Page 12: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

12

Quiz Question:

Assume the reference to an arbitrary node (v) in a BST tree is given. Propose an algorithm for finding the difference between the key stored inthis node and the smallest key in the tree. What is the worst case complexity of your algorithm?

44

5617

325 48

51

v

Page 13: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

13

Example 2 [ finding smaller/greater keys in a BST ]

Binary Search Trees (cont.)

15 58

472010 68

603317

p

3 11 55 q

In the given BST find all keys(a) greater than p=20;(b) smaller than q=55.

27

Page 14: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

14

Binary Search Trees (cont.)

Example 3 [ verifying BST ]

14 58

47227 68

6038173 15 53

27

Is the following tree a BST?

25 75

Page 15: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

15

Binary Search Trees (cont.)

Example 4 [ storing linked list / array in BST ]

Suppose the numbers in a linked list /array are: 11, 5, 8, 16, 4, 19, 17.Store them in a BST.

11 11

5

11

5

11

5 16

11

5 16

8

4

11

5 16

19

11

5 16

19

17

**

**

* **

8

8 4 8 4 8

Page 16: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

16

Binary Search Trees (cont.)

Suppose the set contains: 11, 5, 8, 16, 4, 19, 17. These numbers can be stored in a BST in either of the following ways …

11

5 16

19

17

4 8

5

4 8

11

16

19

17

Start storing from 5.

5

4

8

11

16

19

17

Start storing from 16.Assume the keys are sorted

and then accessed as inthe binary search procedure.

11

5

16

17

4 8 19

Advantages: … ???Disadvantages: … ???

Is the procedure for storing an unordered set of keys (elements) in a BST unique?!

Page 17: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

17

Example 7 [ Would “insertion in the middle” of a BST work??? ]

9

11

7

3

6 15

Suppose we have to insert key=5 in the following BST.

Binary Search Trees (cont.)

9

11

7

3

6

15

5

6 ends up in the left subtree of 5.WRONG!

9

11

7

3

6 15

5

BST property preserved everywhere.OK!

Page 18: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

18

Motivation for Motivation for – BST enable efficient search or counting the numberUse of BSTUse of BST of appearances of a particular key/element

•••• search / counting method recursively proceeds in one subtree only

•••• in the case of arrays and linked lists:(1) if we search for an element – all elements might need

to be examined(2) if we count the appearance of an element – all elements

need to be examined

Binary Search Trees (cont.)

44

5617

Example 5 [ search for number 27 in a BST ]

27 < 44,look left of 44 44

1727 > 17,

look right of 17

44

17

null node reached,27 not in T !

56 56

Page 19: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

19

Binary Search Trees – Implementation

Item Class Item Class – contains key-element pairs – instances of this class get stored in the positions (nodes) of the underlying BST

public class Item {

private Object key, elem;

protected Item (Object k, Object e) {

key = k;

elem = e; }

public Object key() { return key; }public Object element() { return elem; }public void setKey(Object k) { key = k; }public void setElement(Object e) { elem = k; }

}

Page 20: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

20

Binary Search Trees – Implementation (cont.)

BinarySearchTreeBinarySearchTree Class Class

public class BinarySearchTree implements Dictionary {

Comparator C; / * contains set of rules for comparing keys */

BinaryTree T; / * underlying binary tree */

public BinarySearchTree(Comparator c) {C = c;T = new BinaryTree(); }

…public Position findPosition(Object key, Position p) { … }public void insertItem(Object k, Object e) { … }public Object removeElement(Object k) { … }

}

Page 21: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

21

BST – Implementation: Search

public Position findPosition(Object key, Position pos) {

if T.isExternal(pos) { return pos; }; / * terminate */

else {

Object curKey = key(pos);

if (C.isLessThan(key, curKey)) {

return findPosition(key, T.leftChild(pos)); };

else if (C.isGreaterThan(key, curKey)) {

return findPosition(key, T.rightChild(pos)); };else return pos; };

}

findPositionfindPosition(k,p) Method (k,p) Method – belongs to BinarySearchTree class !•••• returns a node with a specific key, or

the last external node found

44

5617

325

key=27

key<44

key>17

key<32

Page 22: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

22

Performance of Performance of findPosition findPosition – method starts at the root and goes for Tree of for Tree of Hight Hight hh down 1 level at the time

•••• O(1) time spent per node ⇒⇒⇒⇒h+1=O(h) is the worst-case RT

log2(n+1)-1 ≤≤≤≤ h ≤≤≤≤ (n-1)/2 ⇒⇒⇒⇒ O(log(n)) ≤≤≤≤ worst case RT ≤≤≤≤ O(n)

General Performance of General Performance of findPositionfindPosition

h = max numberof ancestors

BST – Implementation: Search (cont.)

full binary tree unbalanced tree when n is known, but the “shape” of the tree is unknown

Page 23: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

23

BST – Implementation: Search (cont.)

Example 6 [ determining complexity of Binary Search using BST ]

1 3 10 19 23 45 5327 61 72 99

middle

27

10

23

61

1 45 99

193 7253

balanced BST ⇒⇒⇒⇒ h ≈≈≈≈ log2(n+1)-1

Thus, O(h) = O(log2(n)) is the worst case RT of the BS algorithm on a sorted array.

BS walk through an array = = findPosition in corresponding balanced BST

Page 24: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

24

(1) call method treeSearch(k,T.root())(i.e. findPosition(k,T.root()) and letw be the position returned by thismethod

(2) expandExternal(w), such that theinternal node now contains (k,e)

Node Insertion in BSTNode Insertion in BST •••• procedure for inserting item (k,e):with with Unique KeysUnique Keys

w kw

BST – Implementation: Insertion

Dictionary with Unique Keys Dictionary with Unique Keys – no two elements can have the same key

Dictionary with Duplicate Keys Dictionary with Duplicate Keys – two elements can have the same key

Page 25: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

25

(1) call method treeSearch(k,T.root())and let w be an internal nodereturned

(2) recursively apply treeSearch(k,w) on right child of w until an externalnode is reached

(3) expandExternal on the last external node as in the case of insertion with unique keys

Node Insertion in BSTNode Insertion in BST •••• procedure for inserting item (k,e):with with Duplicate KeysDuplicate Keys

5

2

24

7

key=2

1st internalpositionreturned

2nd internalpositionreturned

worst case performance ∈∈∈∈ O(h)

Performance of BST InsertionPerformance of BST Insertion(2) assumes key(v.leftChild) < key(v) ≤≤≤≤ key(v.rightChild), hence:

BST – Implementation: Insertion (cont.)

insert here

Page 26: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

26

BST – Implementation: Insertion (cont.)

2

2

22

key=2

Otherwise, if we allow key(v.leftChild) ≤≤≤≤ key(v) ≤≤≤≤ key(v.rightChild), then inthe following special case:

22

2

worst case RT of insert = O[ ] ???

Page 27: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

27

BST – Implementation: Insertion (cont.)

public void insertItem(Object k, Object e) throws InvalidKeyException {

checkKey(k); /* check whether the given key is valid */

Position insPos = T.root();

do {insPos = findPosition(k,insPos);

if (T.isExternal(insPos)) break; /* terminate do-while loop */

else insPos = T.rightChild(insPos);

} while (true)T.expandExternal(insPos);Item newItem = new Item(k, e); /* (k,e) pair - object*/

T.replaceElement(insPos, newItem);}

Page 28: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

28

BST – Implementation: Deletion

Node Deletion in BST Node Deletion in BST – more complex operation than insertion, since we must adequately “fill” any “holes” that mayappear – the BST property must be preserved! Two special cases should be considered when inserting item w=(k,e):Case A one child of w (z) is an external nodeCase B both children of w are internal nodes

Node Deletion: Case A Node Deletion: Case A – simply remove w and z from T by means of operation removeAboveExternal(z); replacew with the sibling of z

yy

y z

w

Page 29: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

29

BST – Implementation: Deletion (cont.)

Node Deletion: Case B Node Deletion: Case B – (1) find node (y) with smallest greater keythan w‘s key; y is, in fact, the left-most internal node in the right subtree of w

(2) copy the content of y into w(3) remove y and its leaf-node x by means

removeAboveExternal(x)

y

yx

yx

w y

Performance of BST Deletion Performance of BST Deletion – in both cases, worst case RT∈∈∈∈ O(h)

NOTE: x (i.e. y) immediately follows w in the inorder traversal of T –the BST property preserved!

Page 30: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

30

BST – Implementation: Deletion (cont.)

public Object removeElement(Object k) throws InvalidKeyException {

Object toReturn;

checkKey(k); /* check whether the given key is valid */

Position remPos = findPosition(k, T.root());

if(T.isExternal(remPos)) { return NO_SUCH_KEY; } /* unsucc. search */

else {toReturn = remPos.element();

if (T.isExternal(T.leftChild(remPos)))remPos=T.leftChild(remPos);

else if (T.isExternal(T.rightChild(remPos)))remPos=T.rightChild(remPos);

/* final else – boh children of remPos internal */

remPos

Page 31: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

31

BST – Implementation: Deletion (cont.)

else { Position swapPos = remPos; remPos = T.rightChild(swapPos));do {

remPos = T.leftChild(remPos);} while (T.isInternal(remPos));swap(swapPos, T.parent(remPos));t.removeAboveExternal(remPos)return toReturn;

} } }

swapPos

p

remPos

p

remPos

swapPos

2

1

2

1

Page 32: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

32

Method

size, isEmptyfindElement, insertElement, removeElement

findAllElements, removeAllElements

Time

O(1)O(h)

O(h+s)

BST: Performance

Linked Structure ImplementationLinked Structure Implementation

– space complexity: O(n)– running times:

In general, BST is expected to provide better performance than any linear(sequential) dictionary. However, in the worst case (e.g. highly unbalanced tree) BST performanceis still linear, since h=(n-1)/2 !!!

S – size of iterators returned by these methods

Page 33: Binary Search Trees - York University · Binary Search Trees Binary Search Tree ... Binary Search Trees (1) the right subtree of node p associated with k, and (2) the right subtrees

33

Binary Search Trees: Questions

Q.1 In Case 2 of BST deletion, could node w be simply replaced with oneof its children? Why are we looking for the left-most element in the rightsub-tree? Is there any other candidate element to replace w? Explain!

Q.2 Write a utility function for finding the maximum element in a (sub)tree?

Q.3 The following items are to be placed in a binary search tree: 27, 8, 35, 42, 5, 19, 40, 9, 12, 16, 27, 2.

a) Construct a binary search tree (in diagram form) for the above items inthe order given.b) How many comparisons would you require to locate the number 40 inyour search tree?