您的位置:首页 > 编程语言 > C语言/C++

c++二叉树的各种操作

2016-07-13 15:13 465 查看
头文件

#ifndef _BINARYTREE_H_
#define _BINARYTREE_H_

const int MaxSize = 100;

template<class T>
struct BTNode
{
T data;
BTNode<T> *lchild;
BTNode<T> *rchild;
};

template<class T>
class BinaryTree
{
public:
BinaryTree(); //构造函数
~BinaryTree(); //析构函数
void PreOrder(){PreOrder(r);} //递归前序遍历
void InOrder(){InOrder(r);} //递归中序遍历
void PostOrder(){PostOrder1(r);} //递归后序遍历
void PreOrder1(){PreOrder1(r);} //非递归前序遍历
void InOrder1(){InOrder1(r);} //非递归中序遍历
void PostOrder1(){PostOrder(r);} //非递归后序遍历
void LevelOrder(){LevelOrder(r);}//层序遍历
BTNode<T>* FindNode(T x){FindNode(r,x);}//查找结点
int BTNodeHeigth(){BTNodeHeigth(r);}//树的高度
int NodeCount1(){NodeCount1(r);} //基于前序遍历求结点个数
int NodeCount2(){NodeCount2(r);} //基于中序遍历求结点个数
int NodeCount3(){NodeCount3(r);} //基于后序遍历求结点个数
int NodeCount4(){NodeCount4(r);} //递归求结点个数
void DispLeaf(){DispLeaf(r);} //输出树的叶子结点
void printRouteLength(){printLeavesDepth(r, 0);}//输出树的叶子结点到根结点的路径长度
bool printAncestor(T x){printAncestor(r,x);} //输出值为x的结点的祖先结点
private:
BTNode<T> *r;
BTNode<T>* CreateTree(BTNode<T> *t);//构造函数调用
void DestroyTree(BTNode<T> *t); //析构函数调用
void PreOrder(BTNode<T> *t); //递归前序遍历调用
void InOrder(BTNode<T> *t); //递归中序遍历调用
void PostOrder(BTNode<T> *t); //递归后序遍历调用
void PreOrder1(BTNode<T> *t); //非递归前序遍历调用
void InOrder1(BTNode<T> *t); //非递归中序遍历调用
void PostOrder1(BTNode<T> *t); //非递归后序遍历调用
void LevelOrder(BTNode<T> *t); //层序遍历调用
BTNode<T>* FindNode(BTNode<T> *t, T x);
int BTNodeHeigth(BTNode<T> *t);
int NodeCount1(BTNode<T> *t); //前序遍历求结点个数调用
int NodeCount2(BTNode<T> *t); //中序遍历求结点个数调用
int NodeCount3(BTNode<T> *t); //后序遍历求结点个数调用
int NodeCount4(BTNode<T> *t); //递归求结点个数调用
void DispLeaf(BTNode<T> *t);
void printLeavesDepth(BTNode<T> *t,int depth);
bool printAncestor(BTNode<T> *t,T x);
};
//
template<class T>
BinaryTree<T>::BinaryTree()
{
r = CreateTree(r);
}
//
template<class T>
BinaryTree<T>::~BinaryTree()
{
DestroyTree(r);
}
//
template<class T>
void BinaryTree<T>::DestroyTree(BTNode<T> *t)
{
if(t != NULL)
{
DestroyTree(t->lchild);
DestroyTree(t->rchild);
delete t;
}
}
//
template<class T>
BTNode<T>* BinaryTree<T>::CreateTree(BTNode<T> *t)
{
T ch;
std::cin>>ch;
if(ch == '#')
{
t = NULL;
}
else
{
t = new BTNode<T>;
t->data = ch;
t->lchild = CreateTree(t->lchild);
t->rchild = CreateTree(t->rchild);
}
return t;
}
//
template<class T>
void BinaryTree<T>::PreOrder(BTNode<T> *t)
{
if(t != NULL)
{
std::cout<<t->data<<" ";
PreOrder(t->lchild);
PreOrder(t->rchild);
}
}
//
template<class T>
void BinaryTree<T>::InOrder(BTNode<T> *t)
{
if(t != NULL)
{
InOrder(t->lchild);
std::cout<<t->data<<" ";
InOrder(t->rchild);
}
}
//
template<class T>
void BinaryTree<T>::PostOrder(BTNode<T> *t)
{
if(t != NULL)
{
PostOrder(t->lchild);
PostOrder(t->rchild);
std::cout<<t->data<<" ";
}
}
//
template<class T>
BTNode<T>* BinaryTree<T>::FindNode(BTNode<T> *t,T x)
{
BTNode<T> *p;
if(t == NULL)
{
return NULL;
}
else if(t->data == x)
{
return t;
}
else
{
p = FindNode(t->lchild,x);
if(p != NULL)
{
return p;
}
return FindNode(t->rchild,x);
}
}
//
template<class T>
int BinaryTree<T>::BTNodeHeigth(BTNode<T> *t)
{
int lchildh,rchildh;
if(t == NULL)
{
return 0;
}
else
{
lchildh = BTNodeHeigth(t->lchild);
rchildh = BTNodeHeigth(t->rchild);
return (lchildh > rchildh)?(lchildh + 1):(rchildh + 1);
}
}
//
template<class T>
int BinaryTree<T>::NodeCount4(BTNode<T> *t)
{
if(t == NULL)
{
return 0;
}
else
{
return (NodeCount4(t->lchild) + NodeCount4(t->rchild) + 1);
}
}
//
template<class T>
int BinaryTree<T>::NodeCount1(BTNode<T> *t)
{
int m,n,k;
if(t != NULL)
{
m = NodeCount1(t->lchild);
k = 1;
n = NodeCount1(t->rchild);
return m + n + k;
}
return 0;
}
//
template<class T>
int BinaryTree<T>::NodeCount2(BTNode<T> *t)
{
int m,n,k;
if(t != NULL)
{
m = NodeCount2(t->lchild);
n = NodeCount2(t->rchild);
k = 1;
return m + n + k;
}
return 0;
}
//
template<class T>
int BinaryTree<T>::NodeCount3(BTNode<T> *t)
{
int m,n,k;
if(t != NULL)
{
k = 1;
m = NodeCount3(t->lchild);
n = NodeCount3(t->rchild);
return m + n + k;
}
return 0;
}
//
template<class T>
void BinaryTree<T>::DispLeaf(BTNode<T> *t)
{
if(t != NULL)
{
if((t->lchild == NULL)&&(t->rchild == NULL))
{
std::cout<<t->data<<" ";
}
DispLeaf(t->lchild);
DispLeaf(t->rchild);
}
}
//
template<class T>
//输出路径长度
void BinaryTree<T>::printLeavesDepth(BTNode<T> *t, int depth)
{
if (t == NULL) return;
if (t->lchild == NULL && t->rchild == NULL)
{
std::cout<<t->data<<": "<<depth<<std::endl;
}
else
{
printLeavesDepth(t->lchild, depth+1);
printLeavesDepth(t->rchild, depth+1);
}
}
//
template<class T>
bool BinaryTree<T>::printAncestor(BTNode<T> *t, T x)
{
if(t == NULL)
{
return false;
}
if((t->lchild != NULL)&&(t->lchild->data == x))
{
std::cout<<t->data<<" ";
return true;
}
if((t->rchild != NULL)&&(t->rchild->data == x))
{
std::cout<<t->data<<" ";
return true;
}
if((printAncestor(t->lchild,x))||(printAncestor(t->rchild,x)))
{
std::cout<<t->data<<" ";
return true;
}
return false;
}
//
template<class T>
void BinaryTree<T>::PreOrder1(BTNode<T> *t)
{
BTNode<T> *st[MaxSize];
int top = -1;
BTNode<T> *p;
top++;
st[top] = t; //将根结点入栈
while(top != -1) //栈非空
{
p = st[top];
top--;
std::cout<<p->data<<" ";
if(p->rchild != NULL) //右孩子先进栈
{
top++;
st[top] = p->rchild;
}
if(p->lchild != NULL) //左孩子再进栈
{
top++;
st[top] = p->lchild;
}
}
}
//中序遍历非递归算法
template<class T>
void BinaryTree<T>::InOrder1(BTNode<T> *t)
{
BTNode<T> *st[MaxSize];
BTNode<T> *p;
int top = -1;
p = t;
while((top != -1)||(p!=NULL)) //栈不空或p不为空时
{
while(p != NULL) //扫描所有左结点并进栈
{
top++;
st[top] = p;
p = p->lchild;
}
if(top > -1) //若栈不为空
{
p = st[top];
top--;
std::cout<<p->data<<" ";
p = p->rchild; //转向处理右子树
}
}
}
//
template<class T>
void BinaryTree<T>::PostOrder1(BTNode<T> *t)
{
BTNode<T> *st[MaxSize],*p,*q;
p = t;
int top = -1;
bool flag;
do
{
while(p != NULL) //将p结点及所有的左下结点进栈
{
top++;
st[top] = p;
p = p->lchild;
}
q = NULL; //q指向栈顶结点的前一个已经访问的结点
flag =true; //表示p结点的左子树已经遍历完
while((top != -1)&&(flag == true))//若p结点及其右子树已访问或为空
{
p = st[top];
if(p->rchild == q)
{
std::cout<<q->data<<" ";
top--;
q = p;
}
else
{
p = p->rchild;
flag = false;
}
}
}
while(top != -1);
}
//
template<class T>
void BinaryTree<T>::LevelOrder(BTNode<T> *t)
{
BTNode<T> *qu[MaxSize],*p;
int front = 0, rear = 0;
rear++;
qu[rear] = t;
while(front != rear) //队列不为空
{
front = (front + 1) % MaxSize;
p = qu[front];
std::cout<<p->data<<" ";
if(p->lchild != NULL)
{
rear = (rear + 1) % MaxSize;
qu[rear] = p->lchild;
}
if(p->rchild != NULL)
{
rear = (rear + 1) % MaxSize;
qu[rear] = p->rchild;
}
}
}
#endif // _BINARYREE_H_


源文件
#include <iostream>
#include "BinaryTree.h"

using namespace std;

int main()
{
BinaryTree<char> a;

cout<<"前序遍历: ";
a.PreOrder();
cout<<endl;
cout<<"中序遍历: ";
a.InOrder();
cout<<endl;
cout<<"后序遍历: ";
a.PostOrder();
cout<<endl;
cout<<"层次遍历: ";
a.LevelOrder();
cout<<endl;

cout<<"该树的高度是: "<<a.BTNodeHeigth()<<endl;
cout<<"该树的结点个数为: "<<a.NodeCount1()<<endl;
cout<<"该树的叶子结点为: ";
a.DispLeaf();
cout<<endl;
a.printRouteLength();
cout<<"D的祖先结点有: ";
a.printAncestor('D');

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: