[Cpp]
# Include <iostream>
# Include <vector>
Using namespace std;
/* Binary Search Tree Structure */
Typedef struct BSTree
{
Int node_value;
Struct BSTree * left;
Struct BSTree * right;
Struct BSTree * parent;
} Tree;
Tree * root = NULL;
****************************** ****************/
Void CreateBSTree (Tree * root, int node_value );
Tree * CreateBSTree (int * array_list, int array_length );
Void Print (Tree * root );
/*************************************** ************************/
Int Minimum (Tree * p );
Tree * TreeMinimum (Tree * root );
Int Maximum (Tree * p );
Tree * TreeMaximum (Tree * root );
Tree * FindNode (Tree * root, int node_value );
Tree * Successor (Tree * root );
Tree * PredeSuccessor (Tree * p );
Bool DeleteTreeNode (Tree * root, int node_value );
Bool DeleteTreeNode (Tree * n );
/*************************************** ************************/
Int main (int argc, char * argv [])
{
// Int list [] = {5, 3, 11 };
Int list [] = {15, 5, 16, 3, 12, 20, 10, 13, 18, 23, 6, 7 };
Root = CreateBSTree (list, 12 );
Std: cout <"Cearte BSTree." <std: endl;
// Print (root );
// Std: cout <Successor (FindNode (root, 4)-> node_value;
// Print (root );
// Std: cout <std: endl;
// DeleteTreeNode (root, 15 );
// Print (root );
Tree * t = FindNode (root, 18 );
Std: cout <PredeSuccessor (t)-> node_value;
Getchar ();
Return 0;
}
/* Find the smallest node in the tree
Root Node with p count
*/
Int Minimum (Tree * p)
{
Tree * t = TreeMinimum (p );
If (t! = NULL)
{
Return t-> node_value;
}
Else
{
Return-1;
}
}
Tree * TreeMinimum (Tree * p)
{
If (p-> left = NULL)
{
Return p;
}
TreeMinimum (p-> left );
}
/* Find the maximum node in the tree */
Int Maximum (Tree * p)
{
Tree * t = TreeMaximum (root );
If (t! = NULL)
{
Return t-> node_value;
}
Else
{
Return-1;
}
}
Tree * TreeMaximum (Tree * p)
{
If (p-> right = NULL)
{
Return p;
}
TreeMaximum (p-> right );
}
/* If all node values are different, find the pointer to a node.
Root Node of the p tree,
Node_value value of the node to be searched
*/
Tree * FindNode (Tree * p, int node_value)
{
If (p = NULL)
{
Return NULL;/* recursive return flag */
}
If (p-> node_value = node_value)
{
Return p;
}
If (p-> node_value <node_value)
{
FindNode (p-> right, node_value );
}
Else
{
FindNode (p-> left, node_value );
}
}
/* Find the successor node of a node */
Tree * Successor (Tree * p)
{
If (p = NULL)
{
Return NULL;
}
If (p-> right! = NULL)
{
Return TreeMinimum (p-> right );
}
Tree * t = p-> parent;
While (t! = NULL) & (p = t-> right ))
{
P = t;
T = t-> parent;
}
Return t;
}
/* Find the precursor node of a node
P is the pointer of a node.
*/
Tree * PredeSuccessor (Tree * p)
{
If (p = NULL)
{
Return NULL;
}
Else if (p-> left! = NULL)
{/* If the left subtree is not empty, the parent node is the maximum node of the Left subtree */
Return TreeMaximum (p-> left );
}
Else
{
Tree * t = p-> parent;
While (t! = NULL) & (p = t-> left ))
{/* Pay attention to the direction of node t, which is opposite to finding a successor node */
P = t;
T = t-> parent;
}
Return t;
}
}
/* Delete a node
P indicates the root node pointer.
Node_value value of the node to be deleted
*/
Bool DeleteTreeNode (Tree * p, int node_value)
{
Tree * t = FindNode (p, node_value );
If (t = NULL)
{
Return false;
}
If (t-> left = NULL) & (t-> right = NULL ))
{/* No subnode */
Tree * tmp = t;
If (tmp-> parent-> left = tmp)
{
Tmp-> parent-> left = NULL;
}
Else
{
Tmp-> parent-> right = NULL;
}
Delete tmp;
Tmp = NULL;
}
Else if (t-> left = NULL) | (t-> right = NULL ))
{/* A subnode */
Tree * tmp = t;
If (tmp-> parent-> left = tmp)
{
Tmp-> parent-> left = (tmp-> left = NULL )? Tmp-> right: tmp-> left;
}
Else
{
Tmp-> parent-> right = (tmp-> left = NULL )? Tmp-> right: tmp-> left;
}
Delete tmp;
Tmp = NULL;
}
Else
{/* Two subnodes */
Tree * s = Successor (t );
If (s = NULL)
{
Return false;
}
T-> node_value = s-> node_value;
DeleteTreeNode (s );
}
}
/* Delete a node
P indicates the root node pointer.
*/
Bool DeleteTreeNode (Tree * n)
{
If (n = NULL)
{
Return NULL;
}
Else if (n-> left = NULL) & (n-> right = NULL ))
{/* No subnode */
Tree * tmp = n;
If (tmp-> parent-> left = tmp)
{
Tmp-> parent-> left = NULL;
}
Else
{
Tmp-> parent-> right = NULL;
}
Delete tmp;
Tmp = NULL;
}
Else if (n-> left = NULL) | (n-> right = NULL ))
{/* A subnode */
Tree * tmp = n;
If (tmp-> parent-> left = tmp)
{
Tmp-> parent-> left = (tmp-> left = NULL )? Tmp-> right: tmp-> left;
}
Else
{
Tmp-> parent-> right = (tmp-> left = NULL )? Tmp-> right: tmp-> left;
}
Delete tmp;
Tmp = NULL;
}
Else
{/* Two subnodes */
Tree * s = Successor (n );
If (s = NULL)
{
Return false;
}
N-> node_value = s-> node_value;
DeleteTreeNode (s );
}
}
/* Generate a binary search tree */
Tree * CreateBSTree (int * array_list, int array_length)
{
If (array_length <= 0)
{
Return false;
}
Tree * root = NULL;
Root = new BSTree ();
Root-> left = NULL;
Root-> right = NULL;
Root-> parent = NULL;
Root-> node_value = array_list [0];
For (int I = 1; I <array_length; I ++)
{
CreateBSTree (root, array_list [I]);
}
Return root;
}
Void CreateBSTree (Tree * root, int node_value)
{
If (root = NULL)
{
Return;
}
If (root-> node_value)
{
If (root-> left = NULL)
{
Tree * node = new Tree ();
Node-> left = NULL;
Node-> right = NULL;
Node-> node_value = node_value;
Node-> parent = root;
Root-> left = node;
}
Else
{
CreateBSTree (root-> left, node_value );
}
}
Else
{
If (root-> right = NULL)
{
Tree * node = new Tree ();
Node-> left = NULL;
Node-> right = NULL;
Node-> node_value = node_value;
Node-> parent = root;
Root-> right = node;
}
Else
{
CreateBSTree (root-> right, node_value );
}
}
}
/* Output the Binary Search Tree in the sorted order */
Void Print (Tree * root)
{
If (root = NULL)
{
Return;
}
Print (root-> left );
Std: cout <root-> node_value <"\ t ";
Print (root-> right );
}