33
Binary Search Trees

Binary Search Trees

Embed Size (px)

DESCRIPTION

Binary Search Trees. Speed of List Search. O(n) when implemented as a linked-list O(lg n) possible if implemented as an array. Binary Search Tree. 54. 21. 72. 5. 30. 60. 84. 10. 25. 35. 79. 86. 44. 93. Binary Search Tree. - PowerPoint PPT Presentation

Citation preview

Page 1: Binary Search Trees

Binary Search Trees

Page 2: Binary Search Trees

Speed of List Search

• O(n) when implemented as a linked-list

• O(lg n) possible if implemented as an array

Page 3: Binary Search Trees

Binary Search Tree

54

30

93

25 7910

5 60

86

44

72

35

84

21

Page 4: Binary Search Trees

Binary Search Tree

Tree T is a binary search tree made up of n elements: x0 x1 x2 x3 … xn-1

 Functions:createEmptyTree() returns a newly created empty binary treedelete(T, p) removes the node pointed to by p from the tree Tinsert(T, p) returns T with the node pointed to by p added in

the proper location search(T, key) returns a pointer to the node in T that has a key that matches key returns null if the node is not found traverse(T) prints the contents of T in orderisEmptyTree(T) returns true if T is empty and false if it is not

Page 5: Binary Search Trees

Homework 4

• Describe how to implement a binary search tree using a set of nodes (this tree will have no number of element limit).

• Do the six BST functions.• Can you determine how to implement a

binary search tree using an array (assume it will never have more than 100 elements)?

• Consider the six BST functions.

Page 6: Binary Search Trees

Binary Search Tree

54

30

93

25 7910

5 60

86

44

72

35

84

21

Page 7: Binary Search Trees

Binary Search Tree – Linked List

null

Page 8: Binary Search Trees

createEmptyTree()

t

null

Page 9: Binary Search Trees

isEmptyTree(t)

return t == null

Page 10: Binary Search Trees

search(t, key)

search(t, key)

if t == null return null

else if key == t.key

return t

else if key < t.key

return search(t.left, key)

else

return search(t.right, key)

Page 11: Binary Search Trees

search(t, key)

null

t52

35

20 48

40

45

10

15

30

80

65

58

60

70

75

Page 12: Binary Search Trees

insert(t, p)

insert(t, p)

if t == null

t = p

else

insert2(t, p)

return t

Page 13: Binary Search Trees

insert2(t, p)

insert2(t, p) if p.key < t.key if t.left == null

t.left = p else

insert2(t.left, p) else if t.right == null

t.right = p else

insert2(t.right, p)

Page 14: Binary Search Trees

traverse(t)

traverse(t)

if t != null

traverse(t.left)

print t.data

traverse(t.right)

Page 15: Binary Search Trees

delete(t, p) -- leaf

t

52

35

null

p

null

Page 16: Binary Search Trees

delete(t, p) -- leaf

t

52

35

null

p

null

t.left = nullnull

Page 17: Binary Search Trees

delete(t, p) – single parent

t

52

35

20 null

p

Page 18: Binary Search Trees

delete(t, p) – single parent

t

52

35

20 null

p

t.left = p.left

Page 19: Binary Search Trees

delete(t, p)t

52

35

20

p

41

46

Page 20: Binary Search Trees

delete(t, p)t

52

35

20

p

41

46

successor

Page 21: Binary Search Trees

successor(t, p)

null

t50

36

20 48

40

45

10

15

30

80

65

58

60

70

7539

Page 22: Binary Search Trees

delete(t, p)t

52

35

20

p

41

46

successor

Page 23: Binary Search Trees

delete(t, p)t

52

35

20

p

41

46

successor

delete(p, successor)

Page 24: Binary Search Trees

delete(t, p)t

52

35

20

p

41

46

successor

t.left = successor

Page 25: Binary Search Trees

delete(t, p)t

52

35

20

p

41

46

successor

successor.left = p.leftsuccessor.right = p.right

Page 26: Binary Search Trees

delete(t, p)

null

t50

36

20 48

40

45

10

15

30

80

65

58

60

70

7539

Page 27: Binary Search Trees

Binary Search Tree -- Array

10 112 3 4 5 6 7 8 9 10

2 * i + 1 is the left child2 * i + 2 is the right child

Page 28: Binary Search Trees

Binary Search Tree -- Array

10 112 3 4 5 6 7 8 9 10

2 * i + 1 is the left child2 * i + 2 is the right child

Page 29: Binary Search Trees

Binary Search Tree -- Array

10 112 3 4 5 6 7 8 9 10

2 * i + 1 is the left child2 * i + 2 is the right child

Page 30: Binary Search Trees

Binary Search Tree -- Array

• Advantages– fast– can access the parent from a child

• Disadvantages– fixed size

Page 31: Binary Search Trees

Speed of Binary Search Trees

In the worst case a BST is just a linked list.t

52

3520

48

8065

null

O(n)

How can we guaranteeO(lg n)

Page 32: Binary Search Trees

O(lg n) Search Tree

Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the worst case.  

Functions:createEmptyTree() returns a newly created empty binary treedelete(T, p) removes the node pointed to by p from the tree Tinsert(T, p) returns T with the node pointed to by p added in the proper location search(T, key) returns a pointer to the node in T that has a key that matches key returns null if the node is not found traverse(T) prints the contents of T in orderisEmptyTree(T) returns true if T is empty and false if it is not

Page 33: Binary Search Trees

Homework 5

• Describe how to implement a search tree that has a worst time search, insert, and delete time of no more than O(lg n). This tree should have no number of element limit.

• Do the six Search Tree functions.• Discuss how you would implement this if there

was a maximum to the number of elements.