next up previous contents
Next: B-Trees: Indexing to Relational Up: Trees: ADT Previous: Binary Trees: ADT   Contents

Binary Search Trees: ADT [63]




\begin{picture}(190,150)(45,685)
\thicklines\put(160,820){\circle{30}}
\put(100,...
...t( 56,765){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\tiny$<1$}}}
\end{picture}

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


\begin{picture}(190,195)(45,640)
\thicklines\put(160,820){\circle{30}}
\put(100,...
...ut(180,650){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 4}}}
\end{picture}

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


\begin{picture}(190,190)(45,645)
\thicklines\put(160,820){\circle{30}}
\put(100,...
...ut(175,655){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 4}}}
\end{picture}

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




\begin{picture}(350,190)(85,645)
\thicklines\put(160,820){\circle{30}}
\put(100,...
...ut(415,695){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 4}}}
\end{picture}

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)




\begin{picture}(535,154)(60,685)
\thicklines\put(340,820){\circle{30}}
\put(280,...
...\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm on right side}}}
\end{picture}

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: $\Omega(\hspace{2cm})$

Find: $\Omega(\hspace{2cm})$

Insert: $\Omega(\hspace{2cm})$

Delete: $\Omega(\hspace{2cm})$


next up previous contents
Next: B-Trees: Indexing to Relational Up: Trees: ADT Previous: Binary Trees: ADT   Contents
Ted Billard 2001-10-25