Binary Search Trees - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Binary Search Trees

Description:

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. – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 40
Provided by: Preferr1366
Learn more at: http://orca.st.usm.edu
Category:
Tags: binary | search | tree | trees

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Binary Search Trees


1
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)

2
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.

3
Example Binary Search Tree
20
10
40
6
15
30
25
2
8
Only keys are shown.
4
ADT bsTree
  • AbstractDataType bsTree
  • Instances
  • Operations
  • Find(k)
  • Insert(p)
  • Erase(k)
  • Ascend()

5
The Operation ascend()
Do an inorder traversal. O(n) time.
6
The Operation find()
Complexity is O(height) O(n), where n is number
of nodes/elements.
7
The Operation insert()
35
Insert a pair whose key is 35.
8
The Operation insert()
7
Insert a pair whose key is 7.
9
The Operation insert()
20
10
40
6
15
30
18
25
35
2
8
7
Insert a pair whose key is 18.
10
The Operation insert()
20
10
40
6
15
30
18
25
35
2
8
7
Complexity of insert() is O(height).
11
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).

12
Erase From A Leaf
Erase a leaf element. key 7
13
Erase From A Leaf (contd.)
Erase a leaf element. key 35
14
Remove node with one child
  • If node n has one child, move ns child up to
    take ns place.

15
Erase From A Degree 1 Node
Erase from a degree 1 node. key 40
16
Erase From A Degree 1 Node (contd.)
Erase from a degree 1 node. key 15
17
Erase From A Degree 1 Node (contd.)
Erase from a degree 1 node. key 15
18
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

19
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

20
Erase From A Degree 2 Node
Erase from a degree 2 node. key 10
21
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).
22
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).
23
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).
24
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.
25
Another Erase From A Degree 2 Node
Erase from a degree 2 node. key 20
26
Erase From A Degree 2 Node
20
10
40
6
15
30
18
25
35
2
8
7
Replace with largest in left subtree.
27
Erase From A Degree 2 Node
20
10
40
6
15
30
18
25
35
2
8
7
Replace with largest in left subtree.
28
Erase From A Degree 2 Node
18
10
40
6
15
30
18
25
35
2
8
7
Replace with largest in left subtree.
29
Erase From A Degree 2 Node
18
10
40
6
15
30
25
35
2
8
7
Complexity is O(height).
30
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.

31
(No Transcript)
32
Indexed Binary Search Tree
  • Binary search tree.
  • Each node has an additional field.
  • leftSize number of nodes in its left subtree

33
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
34
get(index) And delete(index)
35
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

36
get(7)
37
get(9)
38
get(5)
39
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.
About PowerShow.com