binary search tree 황승원 fall 2011 cse, postech 2 2 search trees search trees are ideal for...

37
Binary Search Tree 황황황 Fall 2011 CSE, POSTECH

Upload: thomas-summers

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Binary Search Tree

황승원Fall 2011

CSE, POSTECH

2 2

Search Trees

Search trees are ideal for implementing dictionaries– Similar or better performance than skip lists and hashing– Particularly ideal for accessing data sequentially or by

rank

Types of search trees– Binary search trees– AVL trees– Red-black trees– B-trees

3 3

Binary Search Tree

Definition A binary tree that may be empty. A nonempty

binary search tree satisfies the following properties

1. Each node has a (key, value) pair and no two nodes have the same key (i.e., all keys are distinct).

2. For every node x, all keys in the left subtree of x are smaller than that in x.

3. For every node x, all keys in the right subtree of x are larger than that in x.

4. The left and right subtrees of the root are also binary search trees

4

Binary Search Trees

Dictionary Operations: get(key) put(key, value) remove(key)

Additional operations: ascend()

Elements in the ascending order get(rank) (indexed binary search tree)

Get the element in the rankth order remove(rank) (indexed binary search tree)

Remove the element in the rankth order

5

Complexity Of Dictionary Operationsget(), put() and remove()

Data Structure Worst Case Expected

Hash Table O(n) O(1)

Binary SearchTree

O(n) O(log n)

BalancedBinary SearchTree

O(log n) O(log n)

n is number of elements in dictionary

6

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.

7

Example Binary Search Tree

20

10

6

2 8

15

40

30

25

Only keys are shown.

8

The Operation ascend()

20

10

6

2 8

15

40

30

25

Do an inorder traversal. O(n) time.

9

The Operation get()

20

10

6

2 8

15

40

30

25

Complexity is O(height) = O(n), where n is number of nodes/elements.

10

The Operation put()

20

10

6

2 8

15

40

30

25

Put a pair whose key is 35.

35

11

The Operation put()

Put a pair whose key is 7.

20

10

6

2 8

15

40

30

25 35

7

12

The Operation put()

20

10

6

2 8

15

40

30

25

Put a pair whose key is 18.

35

7

18

13

The Operation put()

20

10

6

2 8

15

40

30

25

Complexity of put() is O(height).

35

7

18

14

The Operation remove()

Three cases:

Element is in a leaf.

Element is in a degree 1 node.

Element is in a degree 2 node.

15

Remove From A Leaf

Remove a leaf element. key = 7

20

10

6

2 8

15

40

30

25 35

7

18

16

Remove From A Leaf (contd.)

Remove a leaf element. key = 35

20

10

6

2 8

15

40

30

25 35

7

18

17

Remove From A Degree 1 Node

Remove from a degree 1 node. key = 40

20

10

6

2 8

15

40

30

25 35

7

18

18

Remove From A Degree 1 Node (contd.)

Remove from a degree 1 node. key = 15

20

10

6

2 8

15

40

30

25 35

7

18

19

Remove From A Degree 2 Node

Remove from a degree 2 node. key = 10

20

10

6

2 8

15

40

30

25 35

7

18

20

Remove From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

21

Remove From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

22

Remove From A Degree 2 Node

20

8

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

23

Remove From A Degree 2 Node

20

8

6

2 8

15

40

30

25

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

35

7

18

24

Another Remove From A Degree 2 Node

Remove from a degree 2 node. key = 20

20

10

6

2 8

15

40

30

25 35

7

18

25

Remove From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

26

Remove From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

27

Remove From A Degree 2 Node

18

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

28

Remove From A Degree 2 Node

18

10

6

2 8

15

40

30

25

Complexity is O(height).

35

7

29 29

Binary Search Trees with Duplicates

We can remove the requirement that all elements in a binary search tree need distinct keys

How?– Replace “smaller” in property 2 by “smaller or equal to”– Replace “larger” in property 3 by “larger or equal to”

Then binary search trees can have duplicate keys

30

Indexed Binary Search Tree

Binary search tree. Each node has an additional field.

leftSize = number of nodes in its left subtree

31

Example Indexed Binary Search Tree

20

10

6

2 8

15

40

30

25 35

7

18

0

0 1

1

4

0

0

7

0 0

1

3

leftSize values are in red

32

leftSize And Rank

Rank of an element is its position in inorder (inorder = ascending key order).

[2,6,7,8,10,15,18,20,25,30,35,40]

rank(2) = 0

rank(15) = 5

rank(20) = 7

leftSize(x) = rank(x) with respect to elements in subtree rooted at x

33

leftSize And Rank

20

10

6

2 8

15

40

30

25 35

7

18

0

0 1

1

4

0

0

7

0 0

1

3

sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

34

get(index) And remove(index)7

20

10

6

2 8

15

40

30

25 35

7

18

0

0 1

1

4

0

0

0 0

1

3

sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

35

get(index) And remove(index)

if index = x.leftSize desired element is x.element if index < x.leftSize desired element is index’th

element in left subtree of x if index > x.leftSize desired element has

index=(index - x.leftSize-1) in the right subtree of x

36 36

Challenge

O(height)=O(n) In which case? How to avoid this?

37 37

Coming Up Next

READING: Ch 15 NEXT: Balanced Binary Tree (Ch 16.1~2)