Loading...

PPT – Binary Search Tree PowerPoint presentation | free to download - id: 7b68cc-OGRmN

The Adobe Flash plugin is needed to view this content

Binary Search Tree

Outline

- Concept of Binary Search Tree (BST)
- BST operations
- Find
- Insert
- Remove
- Running time analysis of BST operations

Binary Search Tree Properties

- Elements have keys (no duplicates allowed).
- For every node X in the tree, the values of all

the keys in the left subtree are smaller than the

key in X and the values of all the keys in the

right subtree are larger than the key in X. - The keys must be comparable.

X

ltX

gtX

Binary Search Tree Examples

7

9

2

1

5

3

Binary Search Tree Examples

3

3

1

1

2

2

1

3

3

2

1

2

2

1

3

Basic Operations

- FindMin, FindMax, Find
- Insert
- Remove

FindMin

- Find node with the smallest value
- Algorithm
- Keep going left until you reach a dead end!
- Code
- BinaryNodeltTypegt findMin(BinaryNodeltTypegt t)
- if (t ! null)
- while (t.left ! null)
- t t.left
- return t

FindMax

- Find node with the largest value
- Algorithm
- Keep going right until you reach a dead end!
- Code
- BinaryNodeltTypegt findMax(BinaryNodeltTypegt t)
- if (t ! null)
- while (t.right ! null)
- t t.right
- return t

Find

- You are given an element to find in a BST. If it

exists, return the node. If not, return null. - Algorithm?
- Code?

7

9

2

1

5

3

Find Implementation

- BinaryNodeltTypegt find(Type x, BinaryNodeltTgt t)
- while(t!null)
- if(x.compareTo(t.element)lt0)
- t t.left
- else if(x.compareTo(t.element)gt0)
- t t.right
- else
- return t // Match
- return null // Not found

Insertion Principle

- When inserting a new element into a binary search

tree, it will always become a leaf node.

Insertion Algorithm

- To insert X into a binary search tree
- Start from the root
- If the value of X lt the value of the rootX

should be inserted in the left sub-tree. - If the value of X gt the value of the root X

should be inserted in the right sub-tree. - Remember that a sub-tree is also a tree.
- We can implement this recursively!

Insertion Implementation

- BinaryNodeltTypegt insert(Type x, BinaryNodeltTypegt

t) - if (t null)
- t new BinaryNodeltTypegt(x)
- else if(x.compareTo(t.element)lt0)
- t.left insert (x, t.left)
- else if(x.compareTo(t.element)gt0)
- t.right insert (x, t.right)
- else
- throw new DuplicateItemException(x)
- return t

Removing An Element

4

6

5

Removing An Element Algorithm

- If the node is a leaf, simply delete it.
- If the node has one child, adjust parents child

reference to bypass the node. - If the node has two children
- Replace the nodes element with the smallest

element in the right subtree and then remove that

node, or - Replace the nodes element with the largest

element in the left subtree and then remove that

node - Introduces new sub-problems
- removeMin
- Alternatively, removeMax

Removing Leaf

8

12

4

6

1

5

3

Removing Node With 1 Child

8

12

4

6

1

5

3

Removing Node With 1 Child

8

12

4

6

1

5

3

removeMin

- BinaryNodeltTypegt removeMin(BinaryNodeltTypegt t)
- if (t null)
- throw new ItemNotFoundException()
- else if (t.left ! null)
- t.left removeMin(t.left)
- return t
- else
- return t.right

Removing Node With 2 Children

7

9

2

1

5

3

4

Removing Node With 2 Children

7

2

9

3

1

5

3

4

Removing Node With 2 Children

7

2

9

2

3

1

5

3

4

Removing Root

9

Remove

- BinaryNodeltTypegt remove(Type x, BinaryNodeltTypegt

t) - if (t null)
- throw new ItemNotFoundException()
- if (x.compareTo(t.element)lt0)
- t.left remove(x, t.left)
- else if(x.compareTo(t.element)gt0)
- t.right remove(x, t.right)
- else if (t.left!null t.right ! null)
- t.element findMin(t.right).element
- t.right removeMin(t.right)
- else
- if(t.left!null) tt.left
- else tt.right
- return t

Find k-th element

X

X

X

SL

SR

SL

SR

SL

SR

k lt SL 1

k SL 1

k gt SL 1

Find k-th element

- BinaryNodeltTypegt findKth(int k, BinaryNodeltTypegt

t) - if (t null) throw exception
- int leftSize (t.left ! null) ?
- t.left.size 0
- if (k lt leftSize )
- return findKth (k, t.left)
- else if (k leftSize 1)
- return t
- else
- return findKth ( k - leftSize - 1,

t.right)

Analysis

- Running time for
- Insert?
- Find min?
- Remove?
- Find?
- Average case O(log n)
- Worst case O(n)

Summary

- Binary Search Tree maintains the order of the

tree. - Each node should be comparable
- All operations take O(log n) - average case, when

the tree is equally balanced. - All operations will take O(n) - worst case, when

the height of the tree equals the number of nodes.