Advanced Programming Binary Search Tree 1
Binary Search Tree
2
Definitions Binary Search Tree: n Tree: hierarchical structure made - - PDF document
Advanced Programming Binary Search Tree Binary Search Tree Introduction Binary Search Tree, o BST implement dictionaries or ordered queues elements stored are ordered according to a key Efficient (log n) operations S EARCH , M INIMUM , M
Advanced Programming Binary Search Tree 1
2
Advanced Programming Binary Search Tree 2
3
n Tree: hierarchical structure made of nodes
n Binary: each node has at most 2 sons (left
n Search: nodes have a key field and are
4
n For all nodes y in left subtree of x,
n For all nodes y in right subtree of x,
Advanced Programming Binary Search Tree 3
5
x
6
5 3 7 2 5 8
Advanced Programming Binary Search Tree 4
7
5 3 7 2 5 8
8
9 3 7 2 5 8
Advanced Programming Binary Search Tree 5
9
10
Advanced Programming Binary Search Tree 6
11
5 3 7 2 5 8
12
6 3 7 2 5 8
Advanced Programming Binary Search Tree 7
13
14
constant
Advanced Programming Binary Search Tree 8
15
n Preorder: node, subtree left, subtree right n Inorder: subtree left, node, subtree right n Postorder: subtree left, subtree right, node
16
Advanced Programming Binary Search Tree 9
17
18
Advanced Programming Binary Search Tree 10
19
20
15 6 18 17 20 3 7 2 4 13 9
Advanced Programming Binary Search Tree 11
21
15 6 18 17 20 3 7 2 4 13 9 1 2 3 4 5 6 7 8 9 10 11
22
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
15 6 18 17 20 3 7 2 4 13 9 1 2 3 4 5 6 7 8 9 10 11
24
Advanced Programming Binary Search Tree 13
25
26
15 6 18 17 20 3 7 2 4 13 9 Tree-Search(13)
Advanced Programming Binary Search Tree 14
27
28
15 6 18 17 20 3 7 2 4 13 9 Tree-Search-iterative(13)
Advanced Programming Binary Search Tree 15
29
15 6 18 17 20 3 7 2 4 13 9 15 6 18 17 20 3 7 2 4 13 9 15 6 18 17 20 3 7 2 4 13 9 15 6 18 17 20 3 7 2 4 13 9
30
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
32
15 6 18 17 20 3 7 2 4 13 9
Advanced Programming Binary Search Tree 17
33
15 6 18 17 20 3 7 2 4 13 9
34
15 6 18 17 20 3 7 2 4 13 9
Advanced Programming Binary Search Tree 18
35
15 6 18 17 20 3 7 2 4 13 9
36
15 6 18 17 20 3 7 2 4 13 9
Advanced Programming Binary Search Tree 19
37
15 6 18 17 20 3 7 2 4 13 9
38
Advanced Programming Binary Search Tree 20
39
40
n Create node z with left[z]=right[z]=NIL n Search key[z] n Set pointers
Advanced Programming Binary Search Tree 21
41
y z x=NIL
42
y z x=NIL
Advanced Programming Binary Search Tree 22
43
12 5 18 15 20 2 9 17 13
z
44
12 5 18 15 20 2 9 17
13 z y x 13
Advanced Programming Binary Search Tree 23
45
12 5 18 15 20 2 9 17
z y 13
46
Advanced Programming Binary Search Tree 24
47
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
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
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
y: to be canceled x: unique son of y 12 y x
Advanced Programming Binary Search Tree 26
51
father(y) Update father(x) Y is root? x becomes root 12 y x
52
If needed, copy info of successor in node to be canceled
Advanced Programming Binary Search Tree 27
53
54
Advanced Programming Binary Search Tree 28
55
n Define an insert sequence that mantains
n Define an insert sequence that mantains
56
6 3 8 1 5 7 9 2 4 Insert(): 6, 3, 1, 0, 2, 5, 4, 8, 7, 9
Advanced Programming Binary Search Tree 29
57
9 Insert() : 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 8 7 6 5 4 3 2 1
58
Advanced Programming Binary Search Tree 30
59
60
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
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
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
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);