C ++ basic operations for Binary Trees
Including adding nodes, deleting nodes, pre-order traversal, middle-order traversal, subsequent traversal, sequence traversal, maximum value, minimum value, and binary tree height
// Tree. h header file
# Include <stdio. h>
Class tree
{
PRIVATE:
// The Node element type is struct.
Struct linknode
{
Int data;
Linknode * left;
Linknode * right;
Linknode (const Int & dat, linknode * l, linknode * r): Data (DAT), left (L), right (r ){}
};
Linknode * head; // header Node
// Add a node
void addtreenode (linknode * node, linknode * newnode );
// display the Middle-order display
void showclr (linknode * root);
// display the front-order display
void showlcr (linknode * root );
// display the right order
void showlrc (linknode * root);
// height
int height (linknode * root );
Public:
// Add a node
Bool addtreenode (INT data );
// Display the Middle Order
Bool showclr ();
// Display the forward order
Bool showlcr ();
// Display the right order
Bool showlrc ();
// Forward order
Bool floor ();
// Minimum value
Bool min (INT ** minvalue );
// Maximum value
Bool max (INT ** maxvalue );
// Whether it is an empty tree
Bool empty ();
// Height
Void height (INT ** height );
~ Tree ()
{
Delete [] head;
}
Tree ()
{
Head = new linknode (-1, null, null );
}
};
// Implement file tree. cpp
# Include "stdafx. H"
# Include <stdio. h>
# Include <iostream>
Using namespace STD;
# Include "tree. H ";
# Include <queue>
// Add a node
Void tree: addtreenode (linknode * node, linknode * newnode)
{
If (node-> DATA> newnode-> data)
{
If (node-> left = NULL)
{
Node-> left = newnode;
} Else {
Addtreenode (node-> left, newnode );
}
} Else if (node-> data <newnode-> data)
{
If (node-> right = NULL)
{
Node-> right = newnode;
} Else {
Addtreenode (node-> right, newnode );
}
}
}
// Add a node
Bool tree: addtreenode (INT data)
{
Linknode * node = new linknode (data, null, null );
If (Head-> left = NULL)
{
Head-> left = node;
}
Addtreenode (Head-> left, node );
Return true;
}
// Sequential Traversal
Void tree: showclr (linknode * root)
{
If (root! = NULL ){
Cout <root-> data <"";
}
If (root-> left! = NULL)
{
Showclr (root-> left );
}
If (root-> right! = NULL)
{
Showclr (root-> right );
}
}
// Sequential Traversal
Bool tree: showclr ()
{
If (empty ())
{
Return false;
}
Showclr (Head-> left );
Return true;
}
// Pre-order traversal
Void tree: showlcr (linknode * root)
{
If (root-> left! = NULL)
{
Showlcr (root-> left );
}
If (root! = NULL ){
Cout <root-> data <"";
}
If (root-> right! = NULL)
{
Showlcr (root-> right );
}
}
// Pre-order traversal
Bool tree: showlcr ()
{
If (empty ())
{
Return false;
}
Showlcr (Head-> left );
Return true;
}
// Post-order traversal
Void tree: showlrc (linknode * root)
{
If (root-> left! = NULL)
{
Showlrc (root-> left );
}
If (root-> right! = NULL)
{
Showlrc (root-> right );
}
If (root! = NULL ){
Cout <root-> data <"";
}
}
// Post-order traversal
Bool tree: showlrc ()
{
If (empty ())
{
Return false;
}
Showlrc (Head-> left );
Return true;
}
// Minimum value
Bool tree: min (INT ** minvalue)
{
If (empty ())
{
Return false;
}
Linknode * TMP = head-> left;
While (TMP! = NULL & TMP-> left! = NULL)
{
TMP = TMP-> left;
}
** Minvalue = TMP-> data;
Return true;
}
// Maximum value
Bool tree: max (INT ** maxvalue)
{
If (empty ())
{
Return false;
}
Linknode * TMP = head-> left;
While (TMP! = NULL & TMP-> right! = NULL)
{
TMP = TMP-> right;
}
** Maxvalue = TMP-> data;
Return true;
}
// Determine whether the tree is empty
Bool tree: Empty ()
{
Return head-> left = NULL;
}
// Use queues to achieve binary tree sequence Traversal
// 1: add the root node
// 2: print the data of the root node and add the child node of the root node. The root node is displayed.
// 3: loop Step 2
Bool tree: floor ()
{
Queue <linknode *> q;
Linknode * cur = head-> left;
Q. Push (Head-> left );
While (! Q. Empty ())
{
Cur = Q. Front ();
Cout <cur-> data <"";
If (cur-> left! = NULL ){
Q. Push (cur-> left );
}
If (cur-> right! = NULL)
{
Q. Push (cur-> right );
}
Q. Pop ();
}
Return true;
}
// Calculate a larger value of the two numbers
Int max (int A, int B)
{
If (A> B)
{
Return;
} Else
{
Return B;
}
}
// Recursively calculate the height of a binary tree
// The height of the binary tree is the height of the first binary tree in the left and right subtree.
Int tree: height (linknode * root)
{
If (root = NULL)
{
Return 0;
}
Return 1 + max (height (root-> left), height (root-> right ));
}
// Use a pointer pointing to the pointer to accept the height of the Binary Tree
Void tree: height (INT ** height)
{
** Height = height (Head-> left );
}
# Include "stdafx. H"
# Include <stdio. h>
# Include <iostream>
Using namespace STD;
# Include "tree. H"
Void treetest ();
Int main ()
{
Treetest ();
Int A = 0;
Cin>;
Return 0;
}
// Call the property and method of the pointer type. Use "->" to use "." For the object type.
// Release the memory of a variable when it is not needed
Void treetest ()
{
TREE tree;
Int A [] = {1, 3, 6, 7, 8, 2, 4, 9, 10, 5 };
For (INT I = 0; I <10; I ++ ){
Tree. addtreenode (A [I]);
}
Cout <"----------- original array ----------" <Endl;
For (INT I = 0; I <10; I ++)
{
Cout <A [I] <"";
}
Cout <Endl;
Cout <Endl;
Cout <"----------- ordered in the middle ----------" <Endl;
Tree. showclr ();
Cout <Endl;
Cout <"----------- pre-ordered ----------" <Endl;
Tree. showlcr ();
Cout <Endl;
Cout <"----------- sort ----------" <Endl;
Tree. showlrc ();
Cout <Endl;
Cout <Endl;
Cout <"----------- sequence arrangement ----------" <Endl;
Cout <Endl;
Tree. Floor ();
Cout <Endl;
Int min =-1;
Int * pmin = & min;
Tree. Max (& pmin );
Cout <Endl;
Cout <"maximum value:" <min <Endl;
Cout <Endl;
Tree. Min (& pmin );
Cout <"minimum value:" <min <Endl;
Cout <Endl;
Int H =-1;
Int * Height = & H;
Tree. Height (& height );
Cout <"height:" <H <Endl;
}