最近写的一些程序,基本上都是二叉树相关的东西,现在贴出来啦!下面一个是一棵普通的二叉树,包括增删改,前序,中序,后序,层次打印,还有打印数的高度,还有整棵数的树状打印(这个花了比较多时间,主要考虑如何控制空格的打印)
测试程序不贴了,很乱!结果显示如下:f
*
a b
c d _ h
e f _ g _ _ j _
*
a b
c d _ h
e _ _ g _ _ j _
h *
a b
c d _ 0
e _ _ g _ _ j _
Press any key to continue
主要程序如下(头文件)Head_BiTree.h
#include<iostream>
#include<deque>
using namespace std;
int Max(int x1,int x2) //最大值函数
{
if(x1>x2)
return x1;
else
return x2;
}
int Ming(int x,int n) //冥函数
{
int result = 1;
for(int i = 0;i<n;i++)
{
result = result*x;
}
return result;
}
void Disspace(int n) //输出n个空格函数
{
for(int i = 0;i<n;i++)
{
cout<<" ";
}
}
template<class T> class BiTree;
template<class T>
class BiTreeNode
{
friend class BiTree<T>;
private:
BiTreeNode<T> *right;
BiTreeNode<T> *left;
T item;
public:
BiTreeNode():right(NULL),left(NULL){}
BiTreeNode(T item,BiTreeNode<T> *right = NULL,BiTreeNode<T> *left = NULL)
{
this->item = item;
this->right = right;
this->left = left;
}
~BiTreeNode(){}
};
template<class T>
class BiTree
{
private:
BiTreeNode<T> *root;
BiTreeNode<T> *current;
BiTreeNode<T>* SearchParent(BiTreeNode<T>* &parent,BiTreeNode<T>* child);//
public:
BiTree(T x)
{
BiTreeNode<T> *p = new BiTreeNode<T>(x);
this->root = p;
this->current = root;
}
~BiTree(); //
BiTreeNode<T>* Root(); //return the root node of the BiTree
BiTreeNode<T>* Parent(); //return the parent node of the current node
BiTreeNode<T>* Brother(); // return the brother node of the current node
BiTreeNode<T>* Child(); //return the child node of the current node
void DeleteNode(); //delete the current node
void DeleteRoot(BiTreeNode<T> *cur); //delete the root node
void SearchNode(T x,BiTreeNode<T> *pSearch); //seach the item that values x and return the node
void InsertNode(T x,int RorL = 0); //insert a node values x to the current node /**/
void ChangeNode(T x); // change the current node'value to x
void PreOrder(BiTreeNode<T> *pNode); //preorder to visit the BiTree
void MidOrder(BiTreeNode<T> *pNode); //Midorder to visit the BiTree
void LastOrder(BiTreeNode<T> *pNode); //Lastorder to visit the BiTree
void LayOrder(); //Layorder to visit the BiTree
void Display(); //display the current node's value
void DisplayTotal(); //display the Bitree totally(tree model)
int CountLayer(BiTreeNode<T> *cNode); //return the total layer of the tree
};
template<class T>
BiTree<T>::~BiTree()
{
DeleteRoot(root);
}
template<class T>
void BiTree<T>::DeleteRoot(BiTreeNode<T>* cur)
{
if(cur != NULL)
{
if((cur->left == NULL)&&(cur->right == NULL))
{
BiTreeNode<T> *p;
p = cur;
delete p;
}
else
{
DeleteRoot(cur->left);
DeleteRoot(cur->right);
}
}
}
template<class T>
BiTreeNode<T>* BiTree<T>::Root()
{
if(root != NULL)
{
this->current = root;
return this->root;
}
else
{
return NULL;
}
}
template<class T>
BiTreeNode<T>* BiTree<T>::SearchParent(BiTreeNode<T>* &parent,BiTreeNode<T>* child)
{
if(parent == NULL)
return NULL;
if((parent->left == child)||(parent->right == child))
{
current = parent;
return parent;
}
else
{
if((parent->left !=child)&&(parent->left != NULL))
SearchParent(parent->left,child);
if((parent->right !=child)&&(parent->right != NULL))
SearchParent(parent->right,child);
return current;
}
}
template<class T>
BiTreeNode<T>* BiTree<T>::Parent()
{
if((current == NULL)||(current == root))
return root;
else
{
current = SearchParent(root,current);
return current;
}
}
template<class T>
void BiTree<T>::Display()
{
cout<<current->item<<" ";
}
template<class T>
void BiTree<T>::InsertNode(T x,int RorL) //0 is to insert a node left to right
{ //1 is to insert a node only left
if(current != NULL) //2 is to insert a node only right
{
if(((RorL == 0)||(RorL == 1))&&(current->left == NULL))
{
BiTreeNode<T> *p = new BiTreeNode<T>(x);
current->left = p;
current = p;
}
else if(((RorL == 0)||(RorL == 2))&&(current->right == NULL))
{
BiTreeNode<T> *p = new BiTreeNode<T>(x);
current->right = p;
current = p;
}
else
{
cout<<"can't insert a node.."<<endl;
}
}
else
{
cout<<"can't insert a node.."<<endl;
}
}
template<class T>
BiTreeNode<T>* BiTree<T>::Brother()
{
if(current == NULL)
{
return NULL;
}
else
{
BiTreeNode<T> *p;
p = current;
current = this->Parent();
if((current->left == p)&&(current->right != NULL))
{
current = current->right;
return current;
}
else if((current->right == p)&&(current->left != NULL))
{
current = current->left;
return current;
}
else
{
return NULL;
}
}
}
template<class T>
BiTreeNode<T>* BiTree<T>::Child()
{
if(current == NULL)
{
return NULL;
}
else
{
if(current->left != NULL)
{
current = current->left;
return current;
}
else if(current->right != NULL)
{
current = current->right;
return current;
}
}
}
template<class T>
void BiTree<T>::DeleteNode()
{
BiTreeNode<T> *p1,*p2;
if(current != NULL)
{
if((current->left == NULL)&&(current->right == NULL))
{
p1 = current;
p2 = this->Parent();
if(p2->left == p1)
{
delete p1;
p2->left = NULL;
}
else if(p2->right == p1)
{
delete p1;
p2->right = NULL;
}
}
}
}
template<class T>
void BiTree<T>::SearchNode(T x,BiTreeNode<T> *pSearch)
{
if(pSearch != NULL)
{
if(pSearch->item == x)
{
this->current = pSearch;
}
else
{
if(pSearch->left != NULL)
SearchNode(x,pSearch->left);
if(pSearch->right != NULL)
SearchNode(x,pSearch->right);
}
}
}
template<class T>
void BiTree<T>::ChangeNode(T x)
{
if(current != NULL)
{
current->item = x;
}
}
template<class T>
void BiTree<T>::PreOrder(BiTreeNode<T> *pNode)
{
if(pNode != NULL)
{
cout<<pNode->item<<" ";
PreOrder(pNode->left);
PreOrder(pNode->right);
}
}
template<class T>
void BiTree<T>::MidOrder(BiTreeNode<T> *pNode)
{
if(pNode != NULL)
{
MidOrder(pNode->left);
cout<<pNode->item<<" ";
MidOrder(pNode->right);
}
}
template<class T>
void BiTree<T>::LastOrder(BiTreeNode<T> *pNode)
{
if(pNode != NULL)
{
LastOrder(pNode->left);
LastOrder(pNode->right);
cout<<pNode->item<<" ";
}
}
template<class T>
int BiTree<T>::CountLayer(BiTreeNode<T> *cNode)
{
if(cNode == NULL)
return 0;
else
{
return 1+Max(CountLayer(cNode->left),CountLayer(cNode->right));
}
}
template<class T>
void BiTree<T>::LayOrder()
{
deque<BiTreeNode<T>*> deque;
BiTreeNode<T> *tmp;
deque.push_back(this->root);
while(!deque.empty())
{
tmp = deque.front();
deque.pop_front();
cout<<tmp->item<<" ";
if(tmp->left != NULL)
deque.push_back(tmp->left);
if(tmp->right != NULL)
deque.push_back(tmp->right);
}
}
template<class T>
void BiTree<T>::DisplayTotal()
{
deque<BiTreeNode<T>*> deque1;
deque<T> deque2;
BiTreeNode<T> *tmp;
int Tl = 0;
int Tr = 0;
int i;
int count = this->CountLayer(this->root);
deque1.push_back(this->root);
for(i = 1;i<=Ming(2,count)-1;i++)
{
tmp = deque1.front();
deque1.pop_front();
deque2.push_back(tmp->item);
if(tmp->left != NULL)
{
Tl = 0;
deque1.push_back(tmp->left);
}
else
{
BiTreeNode<T> *p = new BiTreeNode<T>('_');
deque1.push_back(p);
Tl = 1;
}
if(tmp->right != NULL)
{
Tr = 0;
deque1.push_back(tmp->right);
}
else
{
BiTreeNode<T> *p = new BiTreeNode<T>('_');
deque1.push_back(p);
Tr = 1;
}
}
//以上是把二叉树补完整,成为完全二叉树,存放于一个队列中deque2
//以下就弹出deque2打印
int temp = Ming(2,count+1);
int temp1 = count;
int dis;
T tmp2;
for(i = 1;i<=count;i++)
{
if(i == 1)
{
Disspace(temp);
tmp2 = deque2.front();
deque2.pop_front();
cout<<tmp2;
}
else
{
dis = temp - (Ming(2,i-2)-1)*Ming(2,temp1) - Ming(2,temp1)/2;
Disspace(dis);
for(int j = 1;j<=Ming(2,i-1);j++)
{
tmp2 = deque2.front();
deque2.pop_front();
cout<<tmp2;
Disspace(Ming(2,temp1)-1);
}
temp1--;
}
cout<<endl;
cout<<endl;
}
}