最近在面试中呢老是被问到一些关于这个该死的二叉树,闲暇之余我就花时间写了一点对于这个讨厌的树的一些代码实现,可能不够全面,之后有时间我再来补充:
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef char Type;
typedef struct Node
{
Type data;
Node *leftchild;
Node *rightchild;
}Node;
Node * BuyNode()
{
Node *newNode = (Node *)malloc(sizeof(Node));
if(newNode == NULL)return NULL;
memset(newNode,0,sizeof(newNode));
return newNode;
}
void FreeNode(Node *p)
{
free(p);
}
//前序遍历::递归
void ShowPro(Node *root)
{ if(root != NULL)
{
cout<<root->data;
ShowPro(root->leftchild);
ShowPro(root->rightchild);
}
}
//前序遍历::非递归
void ShowPro1(Node *root)
{
if(root == NULL)return;
stack<Node*> s;
s.push(root);
while(!s.empty())
{
Node *tmp = s.top();s.pop();
cout<<tmp->data;
if(tmp->rightchild != NULL)s.push(tmp->rightchild);
if(tmp->leftchild != NULL)s.push(tmp->leftchild);
}
}
//中序遍历::递归
void ShowIn(Node *root)
{ if(root != NULL)
{
ShowIn(root->leftchild);
cout<<root->data;
ShowIn(root->rightchild);
}
}
//中序遍历::非递归
void ShowIn1(Node *root)
{
if(root == NULL)return;
stack<Node*> s;
//s.push(root);
while(root !=NULL || !s.empty())
{
//Node *tmp = root;
while(root != NULL)
{
//if(root->leftchild != NULL)
s.push(root);
root = root->leftchild;
}
root = s.top();s.pop();
cout<<root->data;
root = root->rightchild;
}
}
//后序遍历::递归
void ShowLast(Node *root)
{ if(root != NULL)
{
ShowLast(root->leftchild);
ShowLast(root->rightchild);
cout<<root->data;
}
}
//后序遍历::非递归
void ShowLast1(Node *root)
{
if(root == NULL) return;
stack<Node*> s;
Node *tag;
while(root != NULL || !s.empty())
{
while(root!=NULL)
{
s.push(root);
root = root->leftchild;
}
root = s.top();s.pop();
if(root->rightchild == NULL || tag == root->rightchild)
{
cout<<root->data;
tag = root;
root = NULL;
}
else
{
s.push(root);
root = root->rightchild;
}
}
}
//层次遍历
void ShowLevel(Node *root)
{
queue<Node *> q;
q.push(root);
while(!q.empty())
{
Node *tmp = q.front();q.pop();
cout<<tmp->data;
if(tmp->leftchild != NULL)q.push(tmp->leftchild);
if(tmp->rightchild != NULL)q.push(tmp->rightchild);
}
}
static int FindPos(Type *str,Type key)
{
if(str == NULL)return -1;
int i = 0;
for(; str[i]!='\0'; ++i)
{
if(str[i] == key)
return i;
}
return -1;
}
//前序中序建树
Node *ProInCreate(Type *pro,Type *in,int n)
{
Node *ptr = NULL;
if(pro!=NULL && in!=NULL && n>0)
{
ptr = BuyNode();
ptr->data = pro[0];
int index = FindPos(in,pro[0]);
ptr->leftchild = ProInCreate(pro+1,in,index);
ptr->rightchild = ProInCreate(pro+index+1,in+index+1,n-index-1);
}
return ptr;
}
//后序中序建树
Node *LastInCreate(Type *last,Type *in,int n)
{
Node *ptr = NULL;
if(last!=NULL && in!=NULL && n>0)
{
ptr = BuyNode();
ptr->data = last[n-1];
int index = FindPos(in,last[n-1]);
ptr->leftchild = LastInCreate(last,in,index);
ptr->rightchild = LastInCreate(last+index,in+index+1,n-index-1);
}
return ptr;
}
//求二叉树深度::递归
int Depth1(Node *root)
{
if(root == NULL) return 0;
if(root->leftchild == NULL && root->rightchild == NULL) return 1;
int depthl = Depth1(root->leftchild);
int depthr = Depth1(root->rightchild);
return depthl>depthr ? depthl+1:depthr+1;
}
//求二叉树深度::非递归
int Depth2(Node *root)
{
if(root == NULL) return 0;
if(root->leftchild == NULL && root->rightchild == NULL) return 1;
queue<Node*> q;
q.push(root);
int depth = 0;
while(!q.empty())
{
int size = q.size();
for(int i=0; i<size; ++i)
{
Node *tmp = q.front();q.pop();
if(tmp->leftchild !=NULL)q.push(tmp->leftchild);
if(tmp->rightchild!=NULL)q.push(tmp->rightchild);
}
depth++;
}
return depth;
}
//求二叉树节点个数
int Size(Node *root)
{
if(root == NULL) return 0;
if(root->leftchild == NULL && root->rightchild == NULL) return 1;
int sizel = Size(root->leftchild);
int sizer = Size(root->rightchild);
return sizel+sizer+1;
}
//判断是不是满二叉树
bool IsFullTree(Node *root)
{
if(root == NULL)return false;
if(root->leftchild == NULL && root->rightchild == NULL) return true;
int depth = Depth2(root);
int num = depth*2+1;
int size = Size(root);
if(num!= size)
return false;
return true;
}
//判断是不是完全二叉树
bool IsCompTree(Node *root)
{
queue<Node *> q;
q.push(root);
bool tag = true;
while(!q.empty())
{
root = q.front();q.pop();
if(root->leftchild == NULL && root->rightchild !=NULL)
return false;
if(!tag && (root->leftchild || root->rightchild))
return false;
if(root->leftchild==NULL || root->rightchild == NULL)
tag = false;
if(root->leftchild)
q.push(root->leftchild);
if(root->rightchild)
q.push(root->rightchild);
}
return true;
}
int main()
{
/*char *pro = "ABCDEFGH";
char *in = "CBEDFAGH";
char *last = "CEFDBHGA";*/
char *pro = "ABDCFG";
char *in = "DBAFCG";
char *last = "DBFGCA";
int len = strlen(pro);
Node *root = LastInCreate(last,in,len);
ShowLevel(root);
cout<<endl;
int depth = Depth2(root);
cout<<"Depth:"<<depth<<endl;
int size = Size(root);
cout<<"Size:"<<size<<endl;
if(IsFullTree(root))
cout<<"Tree is full"<<endl;
else
cout<<"Tree is nofull"<<endl;
if(IsCompTree(root))
cout<<"Tree is Comp"<<endl;
else
cout<<"Tree is noComp"<<endl;
return 0;
}