二叉树的定义:
二叉排序树又称二叉查找数。其定义为:二叉排序树是空树,或者是满足如下性质的二叉树。
(1)若它的左子树非空,则左子树上所有的节点的值均小于根节点的值。
(2)若它的右子树非空,则右子树上所有的节点的值均大于根节点的值。
(3)左、右子树本身又各是一棵二叉排序树。
下面是二叉树的相关操作的C++代码实现:
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
class Node
{
public:
int data;
Node * parent;
Node * left;
Node * right;
int tag;
public:
Node(): data(-1),parent(NULL),left(NULL),right(NULL),tag(0){};
Node(int num): data(num), parent(NULL),left(NULL),right(NULL),tag(0){};
};
class Tree
{
public:
Tree(int num[], int len); //insert data from array num
void insertNode(int data); //insert node use recursion
void insertNode1(int data); //insert node use non_recursion
Node *searchNode(int data); //search node
void deleteNode(int data); //delete the node and its' child tree
void InOrderTree(); //middle_access recursion
void InOrderTreeUnRec(); //middle_access non_recursion
void PreOrderTree(); //pre_access recursion
void PreOrderTreeUnRec(); //pre_access non_recursion
void PostOrderTree(); //postorder access with recursion
void PostOrderTreeUnRec(); //postorder access with non_recursion
void LevelOrderTree(); //levelorder access by queue
bool IsSortedTree(Tree tree); // judge a tree is sorted_tree or not
};
Tree::Tree(int num[], int len)
{
root = new Node(num[0]);
for (int index = 1;index < len ;index++ )
{
insertNode1(num[index]);
}
}
//insert data with non_recursion
void Tree::insertNode1(int data)
{
Node *p,*par;
Node *newNode = new Node(data);
p = par = root;
while(NULL != p)
{
par = p;
if(data > p->data)
{
p = p->right;
}
else if (data < p->data)
{
p = p->left;
}
else
{
delete newNode;
return;
}
}
newNode->parent = par;
if(par->data > new->data)
{
par->left = newNode;
}
else
{
par->right = newNode;
}
}
//insert data with recursion
void Tree::insertNode(int data)
{
if(NULL != root)
{
insertNode(root, data); //call the private function
}
}
//the private function with recursion
void Tree::insertNode(Node *current,int data)
{
if(data < current->data)
{
if(NULL == current->left)
{
current->left = new Node(data);
current->left->parent = current;
}
else
{
insertNode(current->left,data);
}
}
else if(data > current->data)
{
if(NULL ==current->right)
{
current->right = new Node(data);
current->right->parent = current;
}
else
{
insertNode(current->right,data);
}
}
return;
}
// search node
Node * Tree::searchNode(int data)
{
Node * current = NULL;
if(NULL != root)
{
current = searchNode(root,data); //call the private function to search node
}
return current;
}
//the private funcion
Node * Tree::searchNode(Node *current, int data)
{
if(data < current->data)
{
if(NULL == current->left)
{
return NULL;
}
return searchNode(current->left,data);
}
else if(data > curren->data)
{
if(NULL == current->right)
return NULL;
return searchNode(current->right,data);
}
return current;
}
//delete node which data is the given
void Tree::deleteNode(int data)
{
Node * curent = NULL;
current = searchNode(data); // find the node
if(NULL != current)
deleteNode(current); // call the private function
}
//call the private function
void Tree::deleteNode(Node * current)
{
if (NULL != current->left)
{
deleteNode(current->left);
}
else if (NULL != current->right)
{
deleteNode(current->right);
}
if (NULL == current->parent)
{
delete current;
root =NULL;
return;
}
if (current->parent->data > current->data)
{ // current is the left node
current->parent->left = NULL;
}
else
{ // current is the right node
current->parent->right = NULL;
}
delete current;
}
//InOrder the sorted_tree with recurion
void Tree::InOrderTree()
{
if(NULL == root)
return;
InOrderTree(root); // call the private function
}
//call the private function
void Tree::InOrderTree(Node * current)
{
if (NUll != current)
{
InOrderTree(current->left);
cout<<current->data<<" ";
InOrderTree(current->right);
}
}
//InOrder the sorted_tree with non_recursion by use stack
void Tree::InorderTreeUnRec()
{
stack<Node *>s;
Node *p = root;
while(NULL != P || !s.empty())
{
while(NULL != p)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
cout<<p->data <<" ";
p = p->right;
}
}
}
//preOrder access with recursion
void Tree::PreOrderTree()
{
if(NULL == root)
return;
PreOrderTree(root); //call the private function
}
//call the private to preOrderTreee
void Tree::PreOrderTree(Node * current)
{
if(NULL !=current)
{
cout<<current->data<<" ";
PreOrderTree(current->left);
PreOrderTree(current->right);
}
}
//preOrder access with recursion by using a stack
void Tree::PreOrderTreeUnRec()
{
stack<Node *> s;
Node *p = root;
while(NULL !=P || !s.empty())
{
while(NULL != p)
{
cout<< p->data <<" ";
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top;
s.pop();
p = p->right;
}
}
}
void Tree::PostOrderTree()
{
if(NULL == root)
return;
PostOrderTree(root); //call the pri
}
//call the private function
void Tree::PostOrderTree(node * current)
{
if (NUll != current)
{
PostOrderTree(current->left);
PostOrderTree(current->right);
cout<<current->data<<" ";
}
}
//postOrder the sorted_tree with non_recursion
void Tree::PostOrderTreeUnRec()
{
stack<Node *> s;
Node * p = root;
while(NULL != p || !s.empty())
{
while ( NULL != p)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
if (p->tag)
{
cout<<p->data<<" ";
s.pop();
p = NULL;
}
else
{
p->tag = 1;
p = p->right;
}
}
}
}
//level order access
void Tree::LevelOrderTree()
{
queue<Node *> p;
Node * ptr = root;
q.push(root);
while(!q.empty())
{
ptr = q.front();
q.pop();
cout<<ptr->data<<" ";
if(NULL != ptr->left)
{
q.push(ptr->left);
}
if(NULL != ptr->right)
{
q.push(ptr->right);
}
}
}
bool Tree::IsSortedTree(Tree tree)
{
int lastvalue = 0;
stack<Node *> s;
Node *p = tree.root;
while(NULL != p || !s.empty())
{
while(NULL != p)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
if(0 == lastvalue || lastvalue < p->data)
{
lastvalue = p->data;
}
else if(lastvalue >= p->data)
{
return false;
}
p = p->right;
}
}
return true;
}