The nature of the Binary Search Tree is: For each node X in the tree, its left subtree value is smaller than X, and its right subtree value is greater than X.
BinaryTree. h
[Cpp]
# Include "Utility. h"
// Typedef struct TreeNode * PtrToNode;
Typedef struct TreeNode * Position;
Typedef struct TreeNode * SearchTree;
Struct TreeNode
{
Int data;
TreeNode * left;
TreeNode * right;
};
SearchTree MakeEmpty (SearchTree T)
{
If (T! = NULL)
{
MakeEmpty (T-> left );
MakeEmpty (T-> right );
Free (T );
}
Return NULL;
}
Position Find (int x, SearchTree T)
{
If (T = NULL)
Return NULL;
If (x <T-> data)
Return Find (x, T-> left );
Else if (x> T-> data)
Return Find (x, T-> right );
Else
Return T;
}
Position FindMin (SearchTree T)
{
If (T = NULL)
Return NULL;
Else if (T-> left = NULL)
Return T;
Else
Return FindMin (T-> left );
// Follow FindMax to implement non-recursive Methods
}
Position FindMax (SearchTree T)
{
If (T! = NULL)
While (T-> right! = NULL)
T = T-> right;
Return T;
}
SearchTree Insert (int x, SearchTree T)
{
If (T = NULL)
{
T = (TreeNode *) malloc (sizeof (TreeNode ));
If (T = NULL)
Cout <"insufficient storage space! "<Endl;
Else
{
T-> data = x;
T-> left = T-> right = NULL;
}
} Else if (x <T-> data ){
T-> left = Insert (x, T-> left );
} Else if (x> T-> data ){
T-> right = Insert (x, T-> right );
}
Return T;
}
SearchTree Delete (int x, SearchTree T)
{
Position tmp;
If (T = NULL ){
Cout <"No element found! "<Endl;
} Else if (x <T-> data ){
T-> left = Delete (x, T-> left );
} Else if (x> T-> data ){
T-> right = Delete (x, T-> right );
} Else if (T-> left & T-> right) {// x = T-> data
Tmp = FindMin (T-> right );
T-> data = tmp-> data;
T-> right = Delete (T-> data, T-> right );
} Else {
Tmp = T;
If (T-> left = NULL)
T = T-> right;
Else if (T-> right = NULL)
T = T-> left;
Free (tmp );
}
Return T;
}
Void InOrderPrint (SearchTree T)
{
If (T! = NULL)
{
InOrderPrint (T-> left );
Cout <T-> data <"";
InOrderPrint (T-> right );
}
}
Void PreOrderPrint (SearchTree T)
{
If (T! = NULL)
{
Cout <T-> data <"";
PreOrderPrint (T-> left );
PreOrderPrint (T-> right );
}
}
# Include "Utility. h"
// Typedef struct TreeNode * PtrToNode;
Typedef struct TreeNode * Position;
Typedef struct TreeNode * SearchTree;
Struct TreeNode
{
Int data;
TreeNode * left;
TreeNode * right;
};
SearchTree MakeEmpty (SearchTree T)
{
If (T! = NULL)
{
MakeEmpty (T-> left );
MakeEmpty (T-> right );
Free (T );
}
Return NULL;
}
Position Find (int x, SearchTree T)
{
If (T = NULL)
Return NULL;
If (x <T-> data)
Return Find (x, T-> left );
Else if (x> T-> data)
Return Find (x, T-> right );
Else
Return T;
}
Position FindMin (SearchTree T)
{
If (T = NULL)
Return NULL;
Else if (T-> left = NULL)
Return T;
Else
Return FindMin (T-> left );
// Follow FindMax to implement non-recursive Methods
}
Position FindMax (SearchTree T)
{
If (T! = NULL)
While (T-> right! = NULL)
T = T-> right;
Return T;
}
SearchTree Insert (int x, SearchTree T)
{
If (T = NULL)
{
T = (TreeNode *) malloc (sizeof (TreeNode ));
If (T = NULL)
Cout <"insufficient storage space! "<Endl;
Else
{
T-> data = x;
T-> left = T-> right = NULL;
}
} Else if (x <T-> data ){
T-> left = Insert (x, T-> left );
} Else if (x> T-> data ){
T-> right = Insert (x, T-> right );
}
Return T;
}
SearchTree Delete (int x, SearchTree T)
{
Position tmp;
If (T = NULL ){
Cout <"No element found! "<Endl;
} Else if (x <T-> data ){
T-> left = Delete (x, T-> left );
} Else if (x> T-> data ){
T-> right = Delete (x, T-> right );
} Else if (T-> left & T-> right) {// x = T-> data
Tmp = FindMin (T-> right );
T-> data = tmp-> data;
T-> right = Delete (T-> data, T-> right );
} Else {
Tmp = T;
If (T-> left = NULL)
T = T-> right;
Else if (T-> right = NULL)
T = T-> left;
Free (tmp );
}
Return T;
}
Void InOrderPrint (SearchTree T)
{
If (T! = NULL)
{
InOrderPrint (T-> left );
Cout <T-> data <"";
InOrderPrint (T-> right );
}
}
Void PreOrderPrint (SearchTree T)
{
If (T! = NULL)
{
Cout <T-> data <"";
PreOrderPrint (T-> left );
PreOrderPrint (T-> right );
}
}
BinaryTree. cpp
[Cpp]
# Include "BinaryTree. h"
Void main ()
{
SearchTree T;
Position P;
Int n = 0;
Int x = 0;
T = MakeEmpty (NULL );
Cout <"input binary tree size:" <endl;
Cin> n;
While (n --)
{
Cout <"input insertion node :";
Cin> x;
T = Insert (x, T );
}
// For (int I = 0; I <n; I ++)
Cout <"maximum element:" <FindMax (T)-> data <endl;
Cout <"minimum element:" <FindMin (T)-> data <endl;
Cout <"sequential traversal :";
InOrderPrint (T );
Cout <endl;
Cout <"Forward traversal :";
PreOrderPrint (T );
Cout <endl;
/*
Cout <"Enter the node to be deleted:" <endl;
Cin> x;
// Delete (x, T );
T = Delete (x, T );
InOrderPrint (T );
*/
System ("PAUSE ");
}
# Include "BinaryTree. h"
Void main ()
{
SearchTree T;
Position P;
Int n = 0;
Int x = 0;
T = MakeEmpty (NULL );
Cout <"input binary tree size:" <endl;
Cin> n;
While (n --)
{
Cout <"input insertion node :";
Cin> x;
T = Insert (x, T );
}
// For (int I = 0; I <n; I ++)
Cout <"maximum element:" <FindMax (T)-> data <endl;
Cout <"minimum element:" <FindMin (T)-> data <endl;
Cout <"sequential traversal :";
InOrderPrint (T );
Cout <endl;
Cout <"Forward traversal :";
PreOrderPrint (T );
Cout <endl;
/*
Cout <"Enter the node to be deleted:" <endl;
Cin> x;
// Delete (x, T );
T = Delete (x, T );
InOrderPrint (T );
*/
System ("PAUSE ");
}