Notes 10b:
Addendum B to Essentials of Data Structures, Cpt 10

Math 61 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 61 Homepage | Return to Notes Listing Page]

Contents


Inserting Data Into an Ordered Tree

(DCS 10.3.3, p. 116) (Cf. F&T, p 581-82)

NOTE: The algorithm given in The Essential of Data Structures makes use of one loop to find the proper location at which to insert a new node. At each pass of the loop, a test is made to determine whether the current node has a child, in which the looping continues, or whether the data should be inserted at that point in a new node.

In contrast, the algorithm presented in F&T separates the "searching" aspect from the "insertion" aspect of the code. The loop to find the proper location occurs in the code first. Then a node is created and linked to the proper place (as the new left or right child).

Deleting Data From an Ordered Tree

(DCS 10.3.5, p. 117) (Cf. F&T, p 583ff) The algorithms in The Essentials of Data Structures and F&T are similar, but there are certain differences.

The cases are enumerated differently in the two books (as to be expected).

In the last case, F&T makes use of the INORDER PREDECESSOR as the replacement node and adjusts the tree accordingly.

Other Cross-References

Code for Inorder Traversal of a Threaded Tree

The following code implements an inorder traversal of a threaded tree. The advantage of this code (in comparison with more typical implementation) is that it avoid recursion and thus is usually more efficient, though the data structure for each node is slightly more complicated.

We assume the node has an extra field called rthread which is TRUE (i.e., 1) if the right pointer actually is a thread pointing to an ancestor rather than a link to a child.

     void intraversal(node * tree)
     {
         node * p, * q;
	 p = tree;
	 do {
	    q = NULL;
	    while (p != NULL) {  // traverse left branch
	       q = p;            // q is the parent of p
	       p = p->left;
	    }
	    // after reaching a NULL left pointer,
	    // print out the parent info and move right
	    if (q != NULL) {      
	       cout << q->info << endl;
	       p = q->right;  
	       while ( q->rthread && p != NULL) {
	         cout << p->info << endl;
		 q = p;
		 p = p->right;
	       }
            }
          } while (q != NULL)
      }

This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 7 April 1999.