In the following discussion, although arbitrarily complex keywords are allowed, for simplicity, it is assumed that they are integers and that all the keywords are different.
General overview
The nature of making a two-fork tree a binary lookup tree is that for each node x in the tree, all the key values in its left subtree are less than the keyword value of x, and all the key values in its right subtree are greater than the key value of X. Note that this means that all elements of the tree can be sorted in a uniform way.
Operation
#ifndef __tree_hstruct TreeNode *position;typedef struct TreeNode *searchtree; Searchtree makeempty (Searchtree T); Position Find (ElementType x,searchtree T); Position findmin (Searchtree T); Position Findmax (Searchtree T); Searchtree Insert (ElementType x,searchtree T); Searchtree Delete (ElementType x,searchtree T); ElementType Retrieve (Position P); #endifstruct treenode{ ElementType Element; Searchtree left; Searchtree right;};
1, Makeempty
This operation is primarily intended for initialization.
Searchtree Makeempty (Searchtree t) { if (t! = NULL) { makeempty (t->left); Makeempty (t->right); Free (T); } return NULL;}
2. Find
This operation typically returns a pointer to a node in the tree t that has the keyword X and returns null if such a node does not exist. Note the order of the tests. The key question is whether you want to test for empty trees first, or you might be circling around a null pointer. The rest of the tests should make the most unlikely scenario to be scheduled at the end.
Position Find (ElementType x,searchtree t) { if (t = = null) return null; if (X < t->element) return Find (x,t->left); else if (X > T->element) return Find (x,t->right); else return T;}
3, Findmin and Findmax
To perform a findmin, start at the root and go left as long as there is a left son. The Findmax routine is the same as the rest of the process, except for the branch facing the right son.
Position findmin (Searchtree t) //recursive { if (t = = null) return null; else if (T->left = = NULL) return T; else return findmin (t->left);} Position Findmax (Searchtree t) //non-recursive { if (T! = null) while (t->right! = null) T = t->right; return T;}
4. Insert
The routines for inserting operations are conceptually simple. In order to insert X into the tree T, we can find it along the tree like find. If x is found, nothing is done (or some updates are made), otherwise x is inserted at the last point of the traversed path. The insertion of a repeating element can be handled by reserving an additional field in the node record to indicate how often it occurs. This allows the entire tree to add some additional space, but it is better than putting the repeating information in the tree (it will make the depth of the tree very Large). Of course, if the keyword is just part of a larger structure, then this approach doesn't work, and we can keep all structures with the same keyword in a secondary data structure, such as a table or another search tree.
Searchtree Insert (ElementType x,searchtree t) { if (t = = NULL) { /*create and return a one-node tree*/ T = malloc (sizeof (struct TreeNode)); if (T = = NULL) { fatalerror ("Out of Space!!!"); } 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;we ' ll does nothin*/ return T;/*do not forget this line!*/}
5. Delete
If the node is a leaf, it can be deleted immediately, and if the node has a son, the node can be deleted after its parent has adjusted the pointer to bypass the node. Note that the deleted node is no longer referenced, and the node can be removed only if the pointer to it has been omitted.
Handling complex situations with two sons, but that. The general deletion strategy is to delete the node (now it is empty) by using the smallest data of its right subtree (which is easy to find) instead of the node's data sickness recursively. Because the smallest node in the right subtree cannot have a left son, the second delete is easy.
searchtree Delete (ElementType X, Searchtree T) {Position Tmpcell; if (T = = NULL) Error ("Element not Found"); 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)/*/-Children */{ /* Replace with smallest in right subtree */Tmpcell = Findmin (t->right); T->element = tmpcell->element; T->right = Delete (t->element, t->right); } else/* One or zero children */{Tmpcell = T; if (T->left = = NULL)/* Also handles 0 Children */T = t->right; else if (t->right = = NULL) T = t->left; Free (Tmpcell); } return T;}
Find Tree adt--two forks search tree