Upload
others
View
14
Download
0
Embed Size (px)
Citation preview
Advanced Programming Binary Search Tree
1
Binary Search Tree
2
Introduction
Binary Search Tree, o BST implement dictionaries or ordered queues – elements stored are ordered according to a key Efficient (log n) operations SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, DELETE.
Advanced Programming Binary Search Tree
2
3
Definitions
Binary Search Tree: n Tree: hierarchical structure made of nodes
with father-son relations. One node has no father (root) and is unique. The path from the root to any node is unique
n Binary: each node has at most 2 sons (left e right) and only one father (p) – except the root that has no father
n Search: nodes have a key field and are ordered according to it.
4
Ordering relations (I)
For each node x : n For all nodes y in left subtree of x,
key[y]≤key[x] n For all nodes y in right subtree of x,
key[y]≥key[x]
Advanced Programming Binary Search Tree
3
5
Ordering relations (II)
x
≤ x ≥ x
6
Ex I
5
3 7
2 5 8
This tree is a BST
Advanced Programming Binary Search Tree
4
7
Ex
5
3
7
2
5
8
BST
8
Ex
9
3
7
2
5
8
NO BST
Advanced Programming Binary Search Tree
5
9
Balanced BST
A BST is balanced if, for each node, the height of its left subtree = the height of its right subtree +- 1
10
Height of a tree
The height of a tree is the number of levels below the root node.
h = 0: only root node h = 1: root node and at least a child h = 2: root, children and grandchild …
In a balanced tree: h = ⎣ log2(n) ⎦
Advanced Programming Binary Search Tree
6
11
Ex
5
3 7
2 5 8
Balanced BST
12
Ex II
6
3
7
2
5
8
Not Balanced BST
Advanced Programming Binary Search Tree
7
13
Complexity
Operations have complexity proportional to the depth h of the tree For a balanced tree (n nodes) complexity = Θ(log n) For a completely unbalanced tree, the worst case, complexity = O(n). Average case Θ(log n)
14
Log vs. Log2
Complexity proportional to depth of tree (h) Since h ~ log2(n) then complexity should be Θ( log2(n) ) But, remember that: So Θ( log2(n) ) ⟺ Θ( log(n) )
logb(x) logb(a)
loga(x) =
constant
Advanced Programming Binary Search Tree
8
15
Traversals
Going through all nodes can be done in 3 ways n Preorder: node, subtree left, subtree right n Inorder: subtree left, node, subtree right n Postorder: subtree left, subtree right, node
16
Preorder
Preorder-Tree-Walk(x) 1 if x ≠ NIL 2 then print key[x] 3 Preorder-Tree-Walk(left[x]) 4 Preorder-Tree-Walk(right[x])
Advanced Programming Binary Search Tree
9
17
Inorder
Inorder-Tree-Walk(x) 1 if x ≠ NIL 2 then Inorder-Tree-Walk(left[x]) 3 print key[x] 4 Inorder-Tree-Walk(right[x])
18
Postorder
Postorder-Tree-Walk(x) 1 if x ≠ NIL 2 then Postorder-Tree-Walk(left[x]) 3 Postorder-Tree-Walk(right[x]) 4 print key[x]
Advanced Programming Binary Search Tree
10
19
Notes
Inorder trasversal (see Inorder-Tree-Walk(root[T])) returns the elements in ascending order of key All traversal are Θ(n) (all nodes are passed once)
20
Ex. Trasversals?
15
6 18
17 20 3 7
2 4 13
9
Advanced Programming Binary Search Tree
11
21
Inorder
15
6 18
17 20 3 7
2 4 13
9
1
2
3
4
5
6
7
8
9
10
11
22
Preorder
15
6 18
17 20 3 7
2 4 13
9
1
2
3
4 5
6
7
8
9
10 11
Advanced Programming Binary Search Tree
12
23
Postorder
15
6 18
17 20 3 7
2 4 13
9
1 2
3
4
5
6
7
8 9
10
11
24
Search operations
Search, Minimum/Maximum, Predecessor/Successor. Are all O(h)
Advanced Programming Binary Search Tree
13
25
Tree-Search
Tree-Search(x, k) 1 if x = NIL or k = key[x] 2 then return x 3 if k < key[x] 4 then return Tree-Search(left[x], k) 5 else return Tree-Search(right[x], k)
26
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Search(13)
Advanced Programming Binary Search Tree
14
27
Tree-Search (iterative)
Tree-Search-iterative(x, k) 1 while x ≠ NIL and k ≠ key[x] 2 do if k < key[x] 3 then x ← left[x] 4 else x ← right[x] 5 return x
28
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Search-iterative(13)
Advanced Programming Binary Search Tree
15
29
Min / Max (iterative)
Tree-Minimum(x) 1 while left[x] ≠ NIL 2 do x ← left[x] 3 return x Tree-Maximum(x) 1 while right[x] ≠ NIL 2 do x ← right[x] 3 return x
15
6 18
17 203 7
2 4 13
9
15
6 18
17 203 7
2 4 13
9
15
6 18
17 203 7
2 4 13
9
15
6 18
17 203 7
2 4 13
9
30
Successor
Given a node, find the closest - 2 cases:
x
p[x] x
p[x] Min of right subtree
First ancestor, of which the node is left heir
Advanced Programming Binary Search Tree
16
31
Successor
Tree-Successor(x) 1 if right[x] ≠ NIL 2 then return Tree-Minimum(right[x]) 3 y ← p[x] 4 while y ≠ NIL and x = right[y] 5 do x ← y 6 y ← p[y] 7 return y
32
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Successor(7)
Advanced Programming Binary Search Tree
17
33
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Successor(7)
34
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Successor(4)
Advanced Programming Binary Search Tree
18
35
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Successor(4)
36
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Successor(17)
Advanced Programming Binary Search Tree
19
37
Ex
15
6 18
17 20 3 7
2 4 13
9 Tree-Successor(4)
38
Predecessor
Tree-Predecessor(x) 1 if left[x] ≠ NIL 2 then return Tree-Maximum(left[x]) 3 y ← p[x] 4 while y ≠ NIL and x = left[y] 5 do x ← y 6 y ← p[y] 7 return y
Advanced Programming Binary Search Tree
20
39
Insert Delete
Must maintain the BST properties.
40
Insert
To insert node z with key v: n Create node z with left[z]=right[z]=NIL n Search key[z] n Set pointers
The new node is always a leaf
Advanced Programming Binary Search Tree
21
41
Tree-Insert (I)
Tree-Insert(T, z) 1 y ← NIL 2 x ← root[T] 3 while x ≠ NIL 4 do y ← x 5 if key[z]<key[x] 6 then x ← left[x] 7 else x ← right[x]
search key[z]
y
z
x=NIL
42
Tree-Insert (II)
8 p[z] ← y 9 if y = NIL 10 then root[T] ← z 11 else if key[z] < key[y] 12 then left[y] ← z 13 else right[y] ← z
y
z
x=NIL
insert z as son of y
Advanced Programming Binary Search Tree
22
43
Ex
12
5 18
15 20 2 9
17
13 Tree-Insert(13) z
44
Ex
12
5 18
15 20 2 9
17
Tree-Insert(13)
13
z
y
x
13
Advanced Programming Binary Search Tree
23
45
Ex
12
5 18
15 20 2 9
17
Tree-Insert(13) z
y
13
46
Delete
Three cases, zero, 1, 2 sons
Advanced Programming Binary Search Tree
24
47
Delete: 0 sons
12 15
5 16
3 12 20
10 13
6
7
18 23
12 15
5 16
3 12 20
10 18 23 z
6
7 Just cancel z
48
Delete: 1 son
12 15
5 16
3 12 20
10 13 18 23
12 15
5
3 12 20
10 18 23
z
6
7
6
7 Son(z) becomes new son of father(z)
13
Advanced Programming Binary Search Tree
25
49
Delete: 2 sons
12 15
5 16
3 12 20
10 13 18 23
12 15
5 16
3 12 20
10 18 23
z
Copy succ (z) in position of z
z
6
7
6
7
13
50
Tree-Delete (I)
Tree-Delete(T, z) 1 if left[z]=NIL or right[z]=NIL 2 then y ← z 3 else y ← Tree-Successor(z) 4 if left[y] ≠ NIL 5 then x ← left[y] 6 else x ← right[y]
y: to be canceled
x: unique son of y
12 y
x
Advanced Programming Binary Search Tree
26
51
Tree-Delete (II)
7 if x ≠ NIL 8 then p[x] ← p[y] 9 if p[y] = NIL 10 then root[T] = x 11 else if y = left[p[y]] 12 then left[p[y]] ← x 13 else right[p[y]] ← x
otherwise, link x to father(y)
Update father(x)
Y is root? x becomes root
12 y
x
52
Tree-Delete (III)
14 if y ≠ z 15 then key[z] ← key[y] 16 fields[z] ← fields[y] 17 return y
If needed, copy info of successor in node to
be canceled
Advanced Programming Binary Search Tree
27
53
Complexity
For insert and cancel: O(h)
If tree is balanced: O(log(n))
54
Balancing
The insert/cancel proposed do not guarantee to maintain the tree balanced. There are variants of BST that maintain the tree balanced when inserting and deleting nodes:
Red-Black tree AA tree
Advanced Programming Binary Search Tree
28
55
Ex
Build a BST with numbers 0 to 9 n Define an insert sequence that mantains
the tree balanced n Define an insert sequence that mantains
the tree unbalanced
56
Solution (I)
6
3 8
1 5 7 9
0 2 4 Insert(): 6, 3, 1, 0, 2,
5, 4, 8, 7, 9
Advanced Programming Binary Search Tree
29
57
Solution (II)
9
Insert() : 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
8 7
6 5
4 3
2 1
0
58
C: node
typedef struct btnode *P_NODE; typedef struct btnode{
int key;
P_NODE left,
right;
}NODE;
Advanced Programming Binary Search Tree
30
59
inorder
void inorder( P_NODE head) {
if( head == NULL)
return;
inorder( head->left);
printf( "%d ", head->key);
inorder( head->right);
}
60
search
P_NODE search( int key, P_NODE head) {
if((head == NULL)!!(head->key == key)) return( head);
else
if( head->key < key) return head->right;
else return head->left;
}
Advanced Programming Binary Search Tree
31
61
insert (I) int insert( int key, P_NODE *phead) { if( *phead == NULL) { if((*phead=(P_NODE)malloc (sizeof(NODE)))== NULL)
return( 0); else { (*phead)->key = key; (*phead)->left = NULL; (*phead)->right = NULL; return( 1); } }
62
insert (II) else if( (*phead)->key == key) { printf("Element already present\n"); return( 0);
} else if( (*phead)->key < key) return( insert( key, &((*phead)->right)));
else return( insert( key, &((*phead)->left)));
}
Advanced Programming Binary Search Tree
32
63
Main P_NODE head=NULL; … if( !insert( key, &head))
printf( ”Insert error!\n"); … if( search( key, head))
printf( ”Found\n"); else
printf( ”Not found\n"); … inorder( head);