#include <iostream>
using namespace Std;
Enumeration class, the last three ways of traversing
Enum Order_mode
{
Order_mode_prev = 0,
Order_mode_mid,
Order_mode_post
};
The structure of the tree node
Template <class t>
struct Binarynode
{
T element;
Binarynode *left;
Binarynode *right;
Binarynode (const t& theelement,
Binarynode *lt,
Binarynode *rt):
Element (Theelement),
Left (LT),
Right (RT)
{
}
};
Template <class t>
Class Binarysearchtree
{
Private
Binarynode<t> *m_root;
Public
Binarysearchtree ();
Binarysearchtree (const binarysearchtree& RHS);
~binarysearchtree ();
Const t& findmin () const;
Const t& Findmax () const;
BOOL Contains (const t& x) const;
void Printtree (Order_mode eordermode = order_mode_prev) const;
void Makeempty ();
void Insert (const t& x);
void Remove (const t& x);
Private
void Insert (const t& x, binarynode<t>* &t);
void Remove (const t& x, binarynode<t>* &t);
binarynode<t>* findmin (binarynode<t>* T) const;
binarynode<t>* Findmax (binarynode<t>* T) const;
BOOL Contains (const t& x, const binarynode<t>* T) const;
void Makeempty (binarynode<t>* &t);
void Printtreeinprev (binarynode<t>* T) const;
void Printtreeinmid (binarynode<t>* T) const;
void Printtreeinpost (binarynode<t>* T) const;
};
Construction method
Template <class t>
Binarysearchtree<t>::binarysearchtree ()
{
M_root = NULL;
}
Use another binary to search the tree's constructor
Template <class t>
Binarysearchtree<t>:: Binarysearchtree (const binarysearchtree& RHS)
{
M_root = Rhs.m_root;
}
destructor, freeing memory
Template <class t>
Binarysearchtree<t>:: ~binarysearchtree ()
{
Makeempty ();
}
To determine if an X element exists
Template <class t>
BOOL Binarysearchtree<t>::contains (const t& x) const
{
return contains (x, M_root);
}
Recursive call
Template <class t>
BOOL Binarysearchtree<t>::contains (const t& x, const binarynode<t>* T) const
{
if (!t)
return false;
else if (x < t->element)
return contains (x, T->left);
else if (x > T->element)
return contains (x, t->right);
Else
return true;
}
Finding the minimum value in a tree
Template <class t>
Const t& binarysearchtree<t>::findmin () const
{
Return Findmin (m_root)->element;
}
The smallest value in a recursive search tree
Template <class t>
binarynode<t>* binarysearchtree<t>::findmin (binarynode<t>* T) const
{
A characteristic of a binary tree is that the value of the Zo is smaller than the root node, and the right cotyledon is larger than the root node.
if (!t)
return NULL;
if (T->left = NULL)
return t;
Else
Return Findmin (T->left);
}
Find the maximum value in the tree
Template <class t>
Const t& Binarysearchtree<t>::findmax () const
{
Return Findmax (m_root)->element;
}
Recursive search for maximum value in tree
Template <class t>
binarynode<t>* Binarysearchtree<t>::findmax (binarynode<t>* T) const
{
A characteristic of a binary tree is that the value of the Zo is smaller than the root node, and the right cotyledon is larger than the root node.
if (t!= NULL)
while (T->right!= NULL)
t = t->right;
return t;
}
inserting elements
Template <class t>
void Binarysearchtree<t>:: Insert (const t& x)
{
Insert (x, m_root);
}
Recursive insertion
Template <class t>
void Binarysearchtree<t>::insert (const t& x, binarynode<t>* &t)
{
if (t = = NULL)
t = new binarynode<t> (x, NULL, NULL);/Note this pointer parameter is a reference
else if (x < t->element)
Insert (x, T->left);
else if (x > T->element)
Insert (x, t->right);
Else
;//do Nothing
}
removing elements from
Template <class t>
void Binarysearchtree<t>::remove (const t& x)
{
Return remove (x, m_root);
}
//Recursive removal
template <class t>
void binarysearchtree<t>::remove (const t& x, binarynode<t>* &t)
{
if (t = = NULL)
return
if (x < t->element)
remove (x, t->left);
else if (x > T->element)
remove (x, t->right);
else//now = =
{
if (t->left!= NULL &&
t->right!= NULL)//two child
{
t->element = findmin (t->right)->element;
remove (T->element, t->right);
}
else
{
BinaryNode<T> *oldnode = T;
t = (t->left!= NULL)? t->left:t->right;
delete OldNode;
}
}
}
Clear the two-pronged tree
Template <class t>
void Binarysearchtree<t>::makeempty ()
{
Makeempty (M_root);
}
Recursive emptying
Template <class t>
void Binarysearchtree<t>::makeempty (binarynode<t>* &t)
{
if (t)
{
Makeempty (T->left);
Makeempty (T->right);
Delete T;
}
t = NULL;
}
Print two-fork search tree
Template <class t>
void Binarysearchtree<t>::p rinttree (order_mode eordermode/*= order_mode_prev*/) const
{
if (Order_mode_prev = = Eordermode)
Printtreeinprev (M_root);
else if (Order_mode_mid = = Eordermode)
Printtreeinmid (M_root);
else if (order_mode_post = = Eordermode)
Printtreeinpost (M_root);
Else
;//do Nothing
}
Pre-order Printing
Template <class t>
void Binarysearchtree<t>::p Rinttreeinprev (binarynode<t>* T) const
{
if (t)
{
cout << t->element;
Printtreeinprev (T->left);
Printtreeinprev (T->right);
}
}
Print in sequence
Template <class t>
void Binarysearchtree<t>::p rinttreeinmid (binarynode<t>* T) const
{
if (t)
{
Printtreeinprev (T->left);
cout << t->element;
Printtreeinprev (T->right);
}
}
Subsequent print
Template <class t>
void Binarysearchtree<t>::p rinttreeinpost (binarynode<t>* T) const
{
if (t)
{
Printtreeinpost (T->left);
Printtreeinpost (T->right);
cout << t->element;
}
}
```
Test code
===
"" C + +
#include "BinarySearchTree.h"
int main ()
{
Binarysearchtree<int> BinaryTree;
Binarytree.insert (5);
Binarytree.insert (1);
Binarytree.insert (2);
Binarytree.insert (3);
Binarytree.insert (6);
Binarytree.insert (8);
Print before the test
cout <<endl<< "pre-preface:" <<endl;
Binarytree.printtree (Order_mode_prev);
cout <<endl<< "In the preface:" <<endl;
Binarytree.printtree (Order_mode_mid);
cout <<endl<< "After:" <<endl;
Binarytree.printtree (Order_mode_post);
cout <<endl;
Testing basic operations
BOOL B = binarytree.contains (1);
cout<< "Is there 1:" <<b<<endl;
int x = Binarytree.findmin ();
cout << "min:" << x <<endl;
x = Binarytree.findmax ();
cout << "Maximum:" << x <<endl;
Binarytree.remove (2);
cout << "removing element 2" <<endl;
Print before the test
cout <<endl<< "pre-preface:" <<endl;
Binarytree.printtree (Order_mode_prev);
cout <<endl<< "In the preface:" <<endl;
Binarytree.printtree (Order_mode_mid);
cout <<endl<< "After:" <<endl;
Binarytree.printtree (Order_mode_post);
cout <<endl;
return 0;
}