Binary Search Trees

- Dictionary Operations
- find(key)
- insert(key, value)
- erase(key)
- Additional operations
- ascend()
- get(index) (indexed binary search tree)
- delete(index) (indexed binary search tree)

Definition Of Binary Search Tree

- A binary tree.
- Each node has a (key, value) pair.
- For every node x, all keys in the left subtree of

x are smaller than that in x. - For every node x, all keys in the right subtree

of x are greater than that in x.

Example Binary Search Tree

20

10

40

6

15

30

25

2

8

Only keys are shown.

ADT bsTree

- AbstractDataType bsTree
- Instances
- Operations
- Find(k)
- Insert(p)
- Erase(k)
- Ascend()

The Operation ascend()

Do an inorder traversal. O(n) time.

The Operation find()

Complexity is O(height) O(n), where n is number

of nodes/elements.

The Operation insert()

35

Insert a pair whose key is 35.

The Operation insert()

7

Insert a pair whose key is 7.

The Operation insert()

20

10

40

6

15

30

18

25

35

2

8

7

Insert a pair whose key is 18.

The Operation insert()

20

10

40

6

15

30

18

25

35

2

8

7

Complexity of insert() is O(height).

The Operation erase()

- Three cases
- Element is in a leaf.
- Element is in a degree 1 node (has one child).
- Element is in a degree 2 node (has two children).

Erase From A Leaf

Erase a leaf element. key 7

Erase From A Leaf (contd.)

Erase a leaf element. key 35

Remove node with one child

- If node n has one child, move ns child up to

take ns place.

Erase From A Degree 1 Node

Erase from a degree 1 node. key 40

Erase From A Degree 1 Node (contd.)

Erase from a degree 1 node. key 15

Erase From A Degree 1 Node (contd.)

Erase from a degree 1 node. key 15

Remove node with two children

- If node n has two children, let x be node in ns

right subtree with smallest key, - Remove x (x has no left child, and is easily

removed) - Replace ns key with xs key

Remove node with two children

- If node n has two children, let x be node in ns

left subtree with largest key, - Remove x (x has no left child, and is easily

removed) - Replace ns key with xs key

Erase From A Degree 2 Node

Erase from a degree 2 node. key 10

Erase From A Degree 2 Node

20

10

40

6

15

30

18

25

35

2

8

7

Replace with largest key in left subtree (or

smallest in right subtree).

Erase From A Degree 2 Node

20

10

40

6

15

30

18

25

35

2

8

7

Replace with largest key in left subtree (or

smallest in right subtree).

Erase From A Degree 2 Node

20

8

40

6

15

30

18

25

35

2

8

7

Replace with largest key in left subtree (or

smallest in right subtree).

Erase From A Degree 2 Node

20

8

40

6

15

30

18

25

35

2

8

7

Largest key must be in a leaf or degree 1 node.

Another Erase From A Degree 2 Node

Erase from a degree 2 node. key 20

Erase From A Degree 2 Node

20

10

40

6

15

30

18

25

35

2

8

7

Replace with largest in left subtree.

Erase From A Degree 2 Node

20

10

40

6

15

30

18

25

35

2

8

7

Replace with largest in left subtree.

Erase From A Degree 2 Node

18

10

40

6

15

30

18

25

35

2

8

7

Replace with largest in left subtree.

Erase From A Degree 2 Node

18

10

40

6

15

30

25

35

2

8

7

Complexity is O(height).

Exercise

- Start with an empty binary search tree.
- Insert the keys 4,12,8,16,6,18,24,2,14,3, draw

the tree following each insert. - From the tree above, delete the keys, 6,14,16,4

in order, draw the search tree following each

deletion.

(No Transcript)

Indexed Binary Search Tree

- Binary search tree.
- Each node has an additional field.
- leftSize number of nodes in its left subtree

Example Indexed Binary Search Tree

7

20

4

3

10

40

1

0

1

6

15

30

0

0

0

0

1

18

25

35

2

8

0

7

leftSize values are in red

get(index) And delete(index)

get(index) And delete(index)

- if index x.leftSize desired element is

x.element - if index lt x.leftSize desired element is

indexth element in left subtree of x - if index gt x.leftSize desired element is (index

- x.leftSize-1)th element in right subtree of x

get(7)

get(9)

get(5)

Exercise

- Start with an empty indexed binary search tree.
- Insert the keys 4,12,8,16,6,18,24,2,14,3, draw

the tree following each insert. Show leftSize

value. - Find the keys with index 3, 6, and 8
- From the tree above, delete the keys whose index

is 7,5,0 in this order. draw the search tree

following each deletion.