Docsity

Binary Search Trees - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Binary Search Trees - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Binary Search Trees

• Dictionary Operations:

 get(key)

 put(key, value)

 remove(key)

• Additional operations:

 ascend()

 get(index) (indexed binary search tree)

 remove(index) (indexed binary search tree)

Complexity Of Dictionary Operations

get(), put() and remove()

Data Structure Worst Case Expected

Hash Table O(n) O(1)

Binary Search

Tree

O(n) O(log n)

Balanced

Binary Search

Tree

O(log n) O(log n)

n is number of elements in dictionary

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

Only keys are shown.

The Operation get()

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

number of nodes/elements.

The Operation put()

Put a pair whose key is 35.

The Operation put()

Put a pair whose key is 18.

The Operation put()

Complexity of put() is O(height).

Remove From A Leaf

Remove a leaf element. key = 7

Remove From A Leaf (contd.)

Remove a leaf element. key = 35

Remove From A Degree 1 Node (contd.)

Remove from a degree 1 node. key = 15

Remove From A Degree 2 Node

Remove from a degree 2 node. key = 10

Remove From A Degree 2 Node

Replace with largest key in left subtree (or

smallest in right subtree).

Remove From A Degree 2 Node

Replace with largest key in left subtree (or

smallest in right subtree).