Binary Search tree implementation-C ++

Source: Internet
Author: User

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 ");
}

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.