C Programming Language (CS2402): HW8

Instructor: J.-S. R. Jang


Due date: Jan. 6, 1997
  1. (180%) Write a program to maintain a binary search tree that supports the following operations:
    1. Insert a node.
    2. Delete a node.
    3. Search a node and print out its info.
    4. Print a tree schematically.
    5. Traverse a tree in in-order.
    To make things easier, please use the program tree.c as a starting point and fill in necessary code segments whenever you see the following comment in the program:
    	/* Put your code here. */
    	
    In fact, there are only 5 places you need to insert your code. They are listed according to their degrees of difficulty:

    1. In inOrder(): This function prints the tree traversal in in-order. Please write a recursive function to accomplish this. (This should be trivial if you have read the textbook.)
    2. In treeHeight(): This function returns the height of a given node. A terminal node is consider at height 0. Please write a recurssive function to accomplish this.
    3. In searchNode(): This function returns the pointer to a node containing the given data. It also returns the pointer of the parent of the searched node via its third argument, a pointer to a pointer to TREENODE.
    4. Two places in deleteNode(): Please read exercise 12.22 (page 504) before your first attempt. In particular, in order to keep the tree balanced, the replacement node is taken from the subtree with a higher height.

    Though tree.c is missing some parts, it is ready to compile and execute. You can run the program and then supply the missing parts one by one. Also note that tree.c gives you an initial tree to work on and displays it schematically. To view the tree correctly, rotate the schematic tree 90 degrees clockwise and you'll get the normal tree with the root at top. A sample output of the final program should look like this:

    [cs20] tree
         y
              x
                        v
                   u
                             t
                                  q
                                       o
                        g
    f
                   e
              d
                   c
         b
              a
    
    You have the following options:
       I: Insert
       D: Delete
       S: Search
       P: Print
       T: Traverse in in-order
       H: Help
       Q: Quit
    
    Enter your choice: d
    Enter character to be deleted: f
         y
              x
                        v
                   u
                        t
                             q
                                  o
    g
                   e
              d
                   c
         b
              a
    
    Enter your choice: d
    Enter character to be deleted: u
         y
              x
                        v
                   t
                        q
                             o
    g
                   e
              d
                   c
         b
              a
    
    Enter your choice: d
    Enter character to be deleted: b
         y
              x
                        v
                   t
                        q
                             o
    g
                   e
              d
         c
              a
    
    Enter your choice: d
    Enter character to be deleted: g
         y
              x
                        v
                   t
                        q
    o
                   e
              d
         c
              a
    
    Enter your choice: d
    Enter character to be deleted: o
         y
              x
                        v
                   t
    q
                   e
              d
         c
              a
    
    Enter your choice: d
    Enter character to be deleted: q
         y
              x
                   v
    t
                   e
              d
         c
              a
    
    Enter your choice: h
    
    You have the following options:
       I: Insert
       D: Delete
       S: Search
       P: Print
       T: Traverse in in-order
       H: Help
       Q: Quit
    
    Enter your choice: t
    The in-order traversal is:
    a c d e t v x y 
    
    Enter your choice: q
    End of run.