Click here to load reader
Upload
emerson-ferreira
View
140
Download
4
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
Universidade Federal da ParaíbaCentro de Informática
Binary Search TreesLecture 17
1107186 – Estrutura de Dados – Turma 02
Prof. Christian Azambuja PagotCI / UFPB
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!
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.
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.
5Universidade Federal da ParaíbaCentro de Informática
Binary Search Tree Example
16
13
1815
30
5028
42
21
root
left subtree right subtree
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 !!!
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
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
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:
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:
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
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!
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.
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.
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
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).
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?