二叉树的基本操作如下,还会陆续补充....
#include <iostream>
#include<stack>
#include <queue>
#include <deque>
using namespace std;
typedef struct _BTNode //树节点
{
char data;
struct _BTNode *lchild,*rchild;
}BTNode;
typedef struct combine_node //数据域
{
BTNode *t;
int level;
}node;
stack<node*> mystack1;
stack<node*> mystack2;
stack<node*> mystack3;
class BTtree
{
public:
void createTree(BTNode *&t); //二叉树的递归创建
void PreorderTravel(BTNode *t,int level); //前序递归周游
void notPreorderTravel(BTNode *t,int level);//前序非递归周游
void InorderTravel(BTNode *t,int level);//中序递归周游
void notInorderTravel(BTNode *t,int level);//中序非递归周游
void PostorderTravel(BTNode *t,int level);//后序递归周游
void notPostorderTravel(BTNode *t);//后序非递归周游
void LevelorderTravel(BTNode *t);//层次遍历
int count_leaf(BTNode *t);//计算叶子节点
int count_height(BTNode *t);//计算数高度
int count_width(BTNode *root);//计算数的宽度
void FindPath(BTNode *t,char path[],char tempPath[],int length,int &ans);//找出树中的最长路径
void PrintPath(BTNode *t,int n);//打印最长路径
bool isInTree(BTNode *r,BTNode *t);//判断节点t是否在以根节点r的树中
BTNode *root; //根节点
};
void visit(BTNode *t,int level)
{
cout<<"level:"<<level<<" data:"<<t->data<<endl;
}
void BTtree::createTree(BTNode *&t)
{
char ch;
cin>>ch;
if(ch=='0')
{
t=NULL;
return;
}
else
{
t=new BTNode;
t->data=ch;
createTree(t->lchild);
createTree(t->rchild);
}
}
void BTtree::PreorderTravel(BTNode *t,int level)
{
if(t!=NULL)
{
visit(t,level);
PreorderTravel(t->lchild,level+1);
PreorderTravel(t->rchild,level+1);
}
}
/*
⑴ 访问p所指向的结点;
⑵ q=p->Rchild ,若q不为空,则q进栈
⑶ p=p->Lchild ,若p不为空,转(1),否则转(4)
⑷ 退栈到p ,转(1),直到栈空为止
*/
void BTtree::notPreorderTravel(BTNode *t,int level)
{
if(this->root==NULL)
return;
while(t!=NULL||!mystack1.empty())
{
visit(t,level); //访问元素
if(t->rchild!=NULL)//右孩子不为空,右孩子入栈
{
node *m_node=new node;
m_node->t=t->rchild;
m_node->level=level+1;
mystack1.push(m_node);
}
if(t->lchild!=NULL)//左孩子不为空,继续遍历左孩子
{
t=t->lchild;
level++;
}
else //左孩子为空,弹出右孩子,继续遍历
{
if(mystack1.empty())
{
return;
}
t=mystack1.top()->t;
level=mystack1.top()->level;
mystack1.pop();
}
}
}
void BTtree::InorderTravel(BTNode *t,int level)
{
if(t!=NULL)
{
InorderTravel(t->lchild,level+1);
visit(t,level);
InorderTravel(t->rchild,level+1);
}
}
/*
⑴ 若p不为空,p进栈, p=p->Lchild
⑵ 否则(即p为空),退栈到p,访问p所指向的结点
⑶ p=p->Rchild ,转(1)
直到栈空为止
*/
void BTtree::notInorderTravel(BTNode *t,int level)
{
if(this->root==NULL)
return;
do
{
while(t!=NULL) //左节点不为空,左节点进栈,指向左孩子
{
node *m_node=new node;
m_node->t=t;
m_node->level=level;
mystack2.push(m_node);
t=t->lchild;
level++;
}
if(!mystack2.empty())
{
t=mystack2.top()->t; //t为空,取出栈顶元素,访问元素 ,指向右孩子
level=mystack2.top()->level;
mystack2.pop();
visit(t,level);
t=t->rchild;
}
}while(t!=NULL||!mystack2.empty());//直到栈空 而且t为空才停止遍历
}
void BTtree::PostorderTravel(BTNode *t,int level)
{
if(t!=NULL)
{
PostorderTravel(t->lchild,level+1);
PostorderTravel(t->rchild,level+1);
visit(t,level);
}
}
/*
用一个节点记录前一次访问过的节点
把左子树压栈,遍历左子树
再遍历右子树
*/
stack<BTNode*> mystack4;
void BTtree::notPostorderTravel(BTNode *t)
{
if(this->root==NULL)
return;
BTNode *tempTree=NULL; //记录上一次访问的节点
while(!mystack4.empty()||t!=NULL)
{
while(t!=NULL) //访问左子树
{
mystack4.push(t);
t=t->lchild;
}
t=mystack4.top();
if(t->rchild==NULL||tempTree==t->rchild)
{
cout<<"data:"<<t->data<<endl;
tempTree=t;
mystack4.pop();
t=NULL;
}
else
{
t=t->rchild;
}
}
}
/*
⑴ 队首元素出队到p
⑵访问p所指向的结点
⑶将p所指向的结点的左、右子结点依次入队。直到队空为止
*/
deque<BTNode*> mydeque;
void BTtree::LevelorderTravel(BTNode *t)
{
if(t==NULL)
{
return;
}
mydeque.push_back(t);
cout<<"data: ";
while(!mydeque.empty())
{
BTNode *tempNode=mydeque.front();
cout<<tempNode->data<<" ";
mydeque.pop_front();
if(tempNode->lchild!=NULL)
{
mydeque.push_back(tempNode->lchild);
}
if(tempNode->rchild!=NULL)
{
mydeque.push_back(tempNode->rchild);
}
}
}
/*
递归的概念,先递归求出左叶子总数,再递归求出右叶子总数
最后返回他们两者之和
*/
int BTtree::count_leaf(BTNode *t)//递归的概念
{
if(t==NULL) //递归返回条件
{
return 0;
}
if(t->lchild==NULL&&t->rchild==NULL)//递归返回条件
{
return 1;
}
return count_leaf(t->lchild)+count_leaf(t->rchild);
}
/*
也是递归的概念,先递归求出左子树高度,再递归求出右子树高度
最后比较两者返回两者较大者
*/
int BTtree::count_height(BTNode *t)//递归的概念
{
if(t==NULL)
{
return 0;
}
if(t->lchild==NULL&&t->rchild==NULL)
{
return 1;
}
int lheight=count_leaf(t->lchild);
int rheight=count_leaf(t->rchild);
return (lheight>=rheight?lheight:rheight)+1;//树的高度 =max(左子树的高度,右子树的高度)+1
}
/*
计算数的宽度
*/
void count(BTNode *p,int *a,int depth)
{
if(p==NULL)
{
return;
}
a[depth]++;
count(p->lchild,a,depth+1);
count(p->rchild,a,depth+1);
}
int BTtree::count_width(BTNode *root)//返回最大宽度
{
int n=count_height(root);
int *a=new int[n];
memset(a,0,n*sizeof(int));
count(root,a,0);
int max=a[0];//max存储了最大宽度
for(int i=1;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
delete[] a;
return max;
}
/*
找出树中的最长路径,等长路径没有考虑
其中t为以t为根节点的树,path存储先前最长的路径,tempPath存储当前路径,
length为当前路径的长度,ans为先前 路径的最长长度
*/
void BTtree::FindPath(BTNode *t,char path[],char tempPath[],int length,int &ans)
{
if(t==NULL)
{
if(length-1>=ans)
{
ans=length;
for(int i=0;i<ans;i++)
{
path[i]=tempPath[i];
}
}
return;
}
tempPath[length]=t->data;
FindPath(t->lchild,path,tempPath,length+1,ans);
FindPath(t->rchild,path,tempPath,length+1,ans);
}
/*
打印最长路径
*/
void BTtree::PrintPath(BTNode *t,int n)
{
int ans=0;
char *path=new char[n];
char *tempPath=new char[n];
memset(path,0,n*sizeof(char));
memset(tempPath,0,n*sizeof(char));
FindPath(t,path,tempPath,0,ans);
cout<<"最长路径是:"<<endl;
for(int i=0;i<ans;i++)
{
cout<<path[i]<<" ";
}
cout<<endl;
cout<<"长度:"<<ans<<endl;
delete[] path;
delete[] tempPath;
}
bool BTtree::isInTree(BTNode *r,BTNode *t)
{
if(r==NULL)
{
return false;
}
else if(r->data==t->data)
{
return true;
}
else
{
bool flag=false;
if(r->lchild!=NULL)
{
flag=isInTree(r->lchild,t);
}
if(!flag&&r->rchild!=NULL)//!flag判断如果存在于左子树中则返回
{
flag=isInTree(r->rchild,t);
}
return flag;
}
}
int main(int argc, char *argv[])
{
BTtree *tree;
int level=1;
tree->createTree(tree->root);
cout<<"前序递归周游:"<<endl;
tree->PreorderTravel(tree->root,level);
cout<<endl;
cout<<"前序非递归周游:"<<endl;
tree->notPreorderTravel(tree->root,1);
cout<<endl;
cout<<"中序递归周游:"<<endl;
tree->InorderTravel(tree->root,1);
cout<<endl;
cout<<"中序非递归周游:"<<endl;
tree->notInorderTravel(tree->root,1);
cout<<endl;
cout<<"后序递归周游:"<<endl;
tree->PostorderTravel(tree->root,1);
cout<<endl;
cout<<"后序非递归周游:"<<endl;
tree->notPostorderTravel(tree->root);
cout<<endl;
cout<<"层次遍历周游:"<<endl;
tree->LevelorderTravel(tree->root);
cout<<endl<<endl;
int leaf_count=tree->count_leaf(tree->root);
cout<<"叶子数为: "<<leaf_count<<endl<<endl;
int tree_height=tree->count_height(tree->root);
cout<<"树的高度为: "<<tree_height<<endl<<endl;
BTNode *tempNode=new BTNode;
tempNode->data='b';
tempNode->lchild=tempNode->rchild=NULL;
if(tree->isInTree(tree->root,tempNode))
{
cout<<"在数中..."<<endl;
}
else
{
cout<<"不在树中..."<<endl;
}
cout<<endl;
int max_width=tree->count_width(tree->root);
cout<<"树的最大宽度为: "<<max_width<<endl<<endl;
//最长路径
int n=20;
tree->PrintPath(tree->root,n);
return 0;
}
这是第二次修改的,补充了计算数的宽度的程序,如下:
/*
计算数的宽度
*/
void count(BTNode *p,int *a,int depth)
{
if(p==NULL)
{
return;
}
a[depth]++;
count(p->lchild,a,depth+1);
count(p->rchild,a,depth+1);
}
int BTtree::count_width(BTNode *root)//返回最大宽度
{
int n=count_height(root);
int *a=new int[n];
memset(a,0,n*sizeof(int));
count(root,a,0);
int max=a[0];//max存储了最大宽度
for(int i=1;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
delete[] a;
return max;
}
过几天还会陆续更新求两子节点的最近公共祖先,交换左右子树等常见问题的程序...
计算二叉树的最长路径,函数声明如下:
void FindPath(BTNode *t,char path[],char tempPath[],int length,int &ans);//找出树中的最长路径
void PrintPath(BTNode *t,int n);//打印最长路径
函数定义:
/*
找出树中的最长路径,等长路径没有考虑
其中t为以t为根节点的树,path存储先前最长的路径,tempPath存储当前路径,
length为当前路径的长度,ans为先前 路径的最长长度
*/
void BTtree::FindPath(BTNode *t,char path[],char tempPath[],int length,int &ans)
{
if(t==NULL)
{
if(length-1>=ans)
{
ans=length;
for(int i=0;i<ans;i++)
{
path[i]=tempPath[i];
}
}
return;
}
tempPath[length]=t->data;
FindPath(t->lchild,path,tempPath,length+1,ans);
FindPath(t->rchild,path,tempPath,length+1,ans);
}
/*
打印最长路径
*/
void BTtree::PrintPath(BTNode *t,int n)
{
int ans=0;
char *path=new char[n];
char *tempPath=new char[n];
memset(path,0,n*sizeof(char));
memset(tempPath,0,n*sizeof(char));
FindPath(t,path,tempPath,0,ans);
cout<<"最长路径是:"<<endl;
for(int i=0;i<ans;i++)
{
cout<<path[i]<<" ";
}
cout<<endl;
cout<<"长度:"<<ans<<endl;
delete[] path;
delete[] tempPath;
}
判断两颗二叉树是否相等:
/*
判断两棵树是否相等
(1)如果两棵树都为空则相等
(2)存在叶子节点则继续递归
*/
bool compare(BTtree *first,BTtree *second)
{
if(first==NULL&&second==NULL)return true;
if(first==NULL||second==NULL)return false;
return compare(first->lchild,second->lchild)&&compare(first->rchild,second->rchild);
}