17

Click here to load reader

Data Structures - 14. Binary Search Trees

Embed Size (px)

DESCRIPTION

Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.

Citation preview

Page 1: Data Structures - 14. Binary Search Trees

Universidade Federal da ParaíbaCentro de Informática

Binary Search TreesLecture 17

1107186 – Estrutura de Dados – Turma 02

Prof. Christian Azambuja PagotCI / UFPB

Page 2: Data Structures - 14. Binary Search Trees

2Universidade Federal da ParaíbaCentro de Informática

Operation UA SA USLL SSLL UDLL SDLLSearch(D, k)

Insert(D, x)

Delete(D, x)

Successor(D, x)

Predecessor(D, x)

Minimum(D)

Maximum(D)

O(n)

O(1)

O(1)

O(n)

O(n)

O(n)

O(n)

O( log n)O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

O(n)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(n)

O(1)

O(1)

O(n)

O(1)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

O(1)

● We have already seen in class:

Back to the Dictionaries...

UA: Unsorted array SA: sorted array.USSL: Unsorted Single Linked List SSSL: Sorted Single Linked ListUDSL: Unsorted Doubly Linked List SDSL: Sorted Doubly Linked List

None of these implementations present good running times

simultaneously for insertion and search!

Page 3: Data Structures - 14. Binary Search Trees

3Universidade Federal da ParaíbaCentro de Informática

Binary Search Trees

● A BST is a structure that, under certain circumstances, presents 'good' running times simultaneously for the insert and search operations.

● Despite the good performance for insertion and search, BSTs also support other operations: minimum and maximum keys, nearest keys, delete, etc.

Page 4: Data Structures - 14. Binary Search Trees

4Universidade Federal da ParaíbaCentro de Informática

Binary Search Tree Invariant

● Given a node n, with a certain key k, in a non-empty BST, the following invariant must hold:– The maximum key of the left subtree of n must be

less or equal to k, and the minimum key of the right subtree of n must be greater or equal to k.

Page 5: Data Structures - 14. Binary Search Trees

5Universidade Federal da ParaíbaCentro de Informática

Binary Search Tree Example

16

13

1815

30

5028

42

21

root

left subtree right subtree

Page 6: Data Structures - 14. Binary Search Trees

6Universidade Federal da ParaíbaCentro de Informática

How to print keys in sorted order?

16

13

1815

30

5028

42

21

Just use in-order tree traversal !!!

Page 7: Data Structures - 14. Binary Search Trees

7Universidade Federal da ParaíbaCentro de Informática

struct Node* Find(struct Node* node, int key){

if ((node == NULL) || (key == node­>key))return node;

elseif (key <= node­>key)

Find(node­>left, key);else

Find(node­>right, key);}

C code excerpt:

Operations on BSTs: Find(node,key)

16

13

1815

30

5028

42

21

Page 8: Data Structures - 14. Binary Search Trees

8Universidade Federal da ParaíbaCentro de Informática

struct Node* FirstKey(struct Node* node){

if ((node == NULL) || (node­>left == NULL))return node;

return FirstKey(node­>left);}

C code excerpt:

Operations on BSTs: FirstKey(node)

16

13

1815

30

5028

42

21

Page 9: Data Structures - 14. Binary Search Trees

9Universidade Federal da ParaíbaCentro de Informática

Operations on BSTs: LastKey(node)

16

13

1815

30

5028

42

21struct Node* LastKey(struct Node* node){

if ((node == NULL) || (node­>right == NULL))return node;

return LastKey(node­>right);}

C code excerpt:

Page 10: Data Structures - 14. Binary Search Trees

10Universidade Federal da ParaíbaCentro de Informática

Operations on BSTs: Insert(node,key)

16

13

1815

30

5028

42

21void Insert(struct Node** node, int key){

if ((*node) == NULL){

(*node) = (struct Node*) malloc(...);(*node)­>key = key;(*node)­>left = NULL;(*node)­>right = NULL;

}else{

if (key < (*node)­>key)Insert(&((*node)­>left), key);

elseInsert(&((*node)­>right), key);

}}

C code excerpt:

Page 11: Data Structures - 14. Binary Search Trees

11Universidade Federal da ParaíbaCentro de Informática

Operations on BSTs: Delete(node,key)

● There are three possibilities:– 1) The node to be deleted has no

children.● Delete the node and set the parent

pointer to NULL.

– 2) The node to be deleted has one child.

● Delete the node and set the parent pointer to its child.

– 3) The node to be deleted has two children.

● A little more tricky....

16

18

16

null

16

18

16

20

20

Page 12: Data Structures - 14. Binary Search Trees

12Universidade Federal da ParaíbaCentro de Informática

Operations on BSTs: Delete(node,key)

16

13

1815

30

5028

42

21

● Deleting a node with two children– 1) Find the node to be deleted.

– 2) Find its successor.● It will be the minimum of its right

subtree.

– 3) Remove its successor.● Do not delete, just unlink it!

Page 13: Data Structures - 14. Binary Search Trees

13Universidade Federal da ParaíbaCentro de Informática

Operations on BSTs: Delete(node,key)

16

13

1815

30

50

42

21

28

● Deleting a node with two children– 1) Find the node to be deleted.

– 2) Find its successor.● It will be the minimum of its right

subtree.

– 3) Remove its successor.● Do not delete, just unlink it!

– 4) Replace the node to be deleted with its successor.

Page 14: Data Structures - 14. Binary Search Trees

14Universidade Federal da ParaíbaCentro de Informática

Operations on BSTs: Delete(node,key)

16

13

1815

30

50

42

28

● Deleting a node with two children– 1) Find the node to be deleted.

– 2) Find its successor.● It will be the minimum of its right

subtree.

– 3) Remove its successor.● Do not delete, just unlink it!

– 4) Replace the node to be deleted with its successor.

Page 15: Data Structures - 14. Binary Search Trees

15Universidade Federal da ParaíbaCentro de Informática

BST Running Times

● The performance of several operations on a BST will depend on its balance.– In a perfectly height-balanced BST “the left and

right subtrees of any node present the same height.” *

● “This is possible only when the tree contains exactly 2(height+1) - 1 nodes!” * (a complete tree!)

– In a height-balanced BST, the difference of the heights of the left and right subtrees of any node is 0 or 1.

http://webdocs.cs.ualberta.ca/~holte/T26/balanced-trees.html

Page 16: Data Structures - 14. Binary Search Trees

16Universidade Federal da ParaíbaCentro de Informática

BST Running Times

● In a perfectly balanced BST, depth ≤ log2n

(where n is the # of nodes).– Thus, the running time to insert, delete, search will

be proportional to log2n. ( O(log

2n) ).

● In the worst case, the BST becomes a linked list.– In this case, all operations will take O(n).

Page 17: Data Structures - 14. Binary Search Trees

17Universidade Federal da ParaíbaCentro de Informática

To think about...

● Why would someone convert a binary search tree into a sorted array? – How it could be done?

● Why would someone convert a sorted array into a balanced binary search tree? – How it could be done?