Binary Tree sorted on values.
Find: at each node, smaller values are to the left; larger to the right.
Binary Search Trees: search [64]
% gcc -o search search.c > tcc search.c % search > h h(elp; q(uit; i(nsert x; n(on-recursive insert x; d(elete x; f(ind x; F(ind non-recursive x; M(ax; m(in; P(reorder; p(ostorder; I(norder
Data File with Test Cases: search.dat
i 2 i 8 i 1 i 0 i 5 i 3 I i 4 I f 8 f 9 F 8 F 9 M m d 0 I d 5 I d 2 I n 13 I
Binary Search Trees: Insert [65]
Insert 4:
1. find where 4 should be (on 3's right)
2. create new node and return the pointer
3. attach the pointer as 3's right
depth pointer left right element
2 145760 0 0 0
1 145736 145760 0 1
0 145688 145736 145712 2
3 145808 0 145832 3
4 145832 0 0 4
2 145784 145808 0 5
1 145712 145784 0 8
Binary Search Trees: insert [66]
typedef struct tree_node *tree_ptr;
struct tree_node {
element_type element;
tree_ptr left;
tree_ptr right;
};
typedef tree_ptr SEARCH_TREE;
tree_ptr insert(element_type x, SEARCH_TREE T) {
if (T == NULL) {
T = (SEARCH_TREE) malloc(sizeof(struct tree_node));
if (T == NULL) printf("Out of space!\n");
else {
T->element = x;
T->left = T->right = NULL;
}
}
else
if (x < T->element)
T->left = insert(x,T->left);
else
if (x > T->element)
T->right = insert(x,T->right);
/* else x is in the tree already. Do nothing. */
return(T);
}
Binary Search Trees: find, min, max [67]
tree_ptr find(element_type x, SEARCH_TREE T) {
if (T == NULL) return(NULL);
if (x < T->element) /* go left or right */
return(find(x,T->left));
else
if (x > T->element)
return(find(x,T->right));
else
return(T);
}
tree_ptr find_min(SEARCH_TREE T) { /* always go left */
if (T == NULL)
return(NULL);
else
if (T->left == NULL)
return(T);
else
return(find_min(T->left));
}
tree_ptr find_max(SEARCH_TREE T) { /* always go right */
if (T != NULL)
while (T->right != NULL)
T = T->right;
return(T);
}
Search Trees: Delete with No Children [68]
Delete 0:
1. find 0
2. free node
3. return NULL pointer back to parent (1)
4. attach pointer as parent's left pointer
depth pointer left right element
1 145736 0 0 1
0 145688 145736 145712 2
3 145808 0 145832 3
4 145832 0 0 4
2 145784 145808 0 5
1 145712 145784 0 8
Search Trees: Delete with 1 Child [69]
Delete 5:
1. find 5
2. free node
3. return pointer to child (3) back to parent (8)
4. attach pointer as parent's (8) left pointer
depth pointer left right element
1 145736 0 0 1
0 145688 145736 145712 2
2 145808 0 145832 3
3 145832 0 0 4
1 145712 145808 0 8
Search Trees: Delete with 2 Children [70]
Delete 2:
1. find 2
2. find min on right side of 2
3. move min (3) to 2's spot
4. do standard delete on old copy of min (3)
depth pointer left right element
1 145736 0 0 1
0 145688 145736 145712 3
2 145832 0 0 4
1 145712 145832 0 8
Binary Search Trees: delete [71]
tree_ptr delete(element_type x, SEARCH_TREE T) {
tree_ptr tmp_cell;
if (T == NULL) printf("Element not found\n");
else if (x < T->element) /* go left */
T->left = delete(x,T->left);
else if (x > T->element) /* go right */
T->right = delete(x,T->right);
else /* found element to be deleted */
if (T->left && T->right) { /* two children */
/* replace with smallest in right subtree */
tmp_cell = find_min(T->right);
T->element = tmp_cell->element;
T->right = delete(T->element,T->right);
}
else { /* one or no children */
tmp_cell = T;
if (T->left == NULL) /* only a right child or no children */
T = T->right;
else
if (T->right == NULL) /* only a left child */
T = T->left;
free(tmp_cell);
}
return(T);
}
Binary Search Trees: Analysis [72]
WORST CASE - Tree is very unbalanced: linked list of n nodes
Depth of Tree: O( )
Find: O( )
Insert: O( )
Delete: O( )
BEST CASE - Tree is perfectly balanced:
How many nodes at depth 0:
How many nodes at depth 1:
How many nodes at depth 2:
How many nodes at depth 3:
Depth of Tree:
Find:
Insert:
Delete: