头文件:
#include<assert.h>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
定义二叉树的结点类型:
typedef char ELemType;
#define END '#'
typedef struct BtNode
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode,*BinaryTree;
构造树结点:
BtNode *Buynode()
{
BtNode *p=(BtNode*)malloc(sizeof(BtNode));
p->leftchild=NULL;
p->rightchild=NULL;
p->data=0;
}
二叉树的三种遍历方式:
//先序
void PreOrder(BtNode *p)
{
if(p!=NULL)
{
cout<<p->data<<" ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
//中序
void InOrder(BtNode *p)
{
if(p!=NULL)
{
InOrder(p->leftchild);
cout<<p->data<<" ";
InOrder(p->rightchild);
}
}
//后序
void PastOrder(BtNode *p)
{
if(p!=NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout<<p->data<<“ ”;
}
}
二叉树的实现方法:
//1.输入结点+#号
BtNode* CreateTree1()
{
ElemType item;
cin>>item;
BtNode*p=NULL;
if(item!=END)
{
p=Buynode();
p->data=item;
p->leftchild=CreateTree1();
p->rightchild=CreateTree1();
}
return p;
}
//2.
BtNode* CreateTree2(char *str)
{
BtNode *p = NULL;
if (str != NULL && *str != END)
{
p = Buynode();
p->data = *str;
p->leftchild = CreateTree2(++str);
p->rightchild = CreateTree2(++str);
}
return p;
}
//3.
BtNode *CreateTree3(ElemType ** const pstr)
{
BtNode *s = NULL;
if (pstr != NULL && *pstr != NULL && **pstr != END)
{
s = Buynode();
s->data = **pstr;
s->leftchild = CreateTree3(&++*pstr);//*pstr->++*pstr->&++*pstr
s->rightchild = CreateTree3(&++*pstr);
}
return s;
}
//4.已知先序和中序,求后序
int FindIs(ElemType *is, ElemType ps, int n)
{
for (int i = 0; i < n; ++i)
{
if (is[i] == ps)
{
return i;
}
}
return -1;
}
BtNode *CreatePI(ElemType *ps, ElemType *is, int n)
{
BtNode *s = NULL;
if (n > 0)
{
s = Buynode();
s->data = ps[0];
int pos = FindIs(is, ps[0], n);
if (pos == -1)
{
exit(1);
}
s->leftchild = CreatePI(ps + 1, is, pos);
s->rightchild = CreatePI(ps + pos + 1, is + pos + 1, n - pos - 1);
}
return s;
}
BtNode *CreateTreePI(ElemType *ps, ElemType *is, int n)
{
if (ps == NULL && is == NULL && n < 1)
{
return NULL;
}
else
{
return CreatePI(ps, is, n);
}
}
//5、已知中序和后序,求先序
int FindIL(ElemType *is, ElemType ls, int n)
{
for (int i = 0; i < n; ++i)
{
if (is[i] == ls)
{
return i;
}
}
return -1;
}
BtNode *CreateIL(ElemType *is, ElemType *ls, int n)
{
BtNode *s = NULL;
if (n > 0)
{
s = Buynode();
s->data = ls[n - 1];
int pos = FindIL(is, ls[n - 1], n);
if (pos == -1)
{
exit(1);
}
s->leftchild = CreatePI(is, ls, pos);
s->rightchild = CreatePI(is + pos + 1, ls + pos, n - pos - 1);
}
return s;
}
BtNode *CreateTreeIL(ElemType *is, ElemType *ls, int n)
{
if (is == NULL && ls == NULL && n < 1)
{
return NULL;
}
else
{
return CreateIL(is, ls, n);
}
}
//6、
void Tree6(int *ar, int i, int n)
{
if (i<n)
{
Tree6(ar, i * 2 + 1, n);
cout << ar[i] << " ";
Tree6(ar, i * 2 + 2, n);
}
}
void CreateTree6(int *ar, int n)
{
if (ar == NULL || n < 1)
{
return;
}
else
{
Tree6(ar, 0, n);
}
}
//7、数组转换成二叉树
BtNode *Create(int *ar, int i, int n)
{
BtNode *s = NULL;
if (i < n)
{
s = Buynode();
s->data = ar[i];
s->leftchild = Create(ar, i * 2 + 1, n);
s->rightchild = Create(ar, i * 2 + 2, n);
}
return s;
}
BtNode *CreateAr(int *ar, int n)
{
if (ar == NULL || n < 1)
{
return NULL;
}
else
{
return Create(ar, 0, n);
}
}
//8、二叉树转换成数组
void Link_to_Ar(BtNode *ptr, int *buff, int i)
{
if (ptr != NULL)
{
buff[i] = ptr->data;
Link_to_Ar(ptr->leftchild, buff, i * 2 + 1);
Link_to_Ar(ptr->rightchild, buff, i * 2 + 2);
}
}
void CreateLink(BtNode *ptr, int *buff)
{
if (ptr == NULL || buff == NULL)
{
return;
}
Link_to_Ar(ptr, buff, 0);
}
//9.树的结点个数
int Size_Tree(BtNode *ptr)
{
if (ptr == NULL)
{
return 0;
}
else return Size_Tree(ptr->leftchild) + Size_Tree(ptr->rightchild) + 1;
}
//10.树的深度
int Depth_Tree(BtNode *ptr)
{
if (ptr == NULL)
{
return 0;
}
else return Depth_Tree(ptr->leftchild) > Depth_Tree(ptr->rightchild) ? Depth_Tree(ptr->leftchild) + 1 : Depth_Tree(ptr->rightchild) + 1;
}
//11.找节点
BtNode *FindValue(BtNode *ptr, ElemType x)
{
if (ptr == NULL || ptr->data == x)
{
return ptr;
}
else
{
BtNode *p = FindValue(ptr->leftchild, x);
if (NULL == p)
{
p = FindValue(ptr->rightchild, x);
}
return p;
}
}
//12.非递归的前序
void NicePreOrder(BtNode *ptr)
{
if (ptr == NULL) return;
stack<BtNode*> st;
st.push(ptr);
while (!st.empty())
{
ptr = st.top(); st.pop();
cout << ptr->data << " ";
if (ptr->rightchild != NULL)
{
st.push(ptr->rightchild);
}
if (ptr->leftchild)
{
st.push(ptr->leftchild);
}
}
cout << endl;
}
//13.非递归的中序遍历(引入栈是为了模拟二叉树的递归调用)
void NiceInOrder(BtNode *ptr)
{
if (ptr == NULL) return;
stack<BtNode*> st;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
cout << ptr->data << " ";
ptr = ptr->rightchild;
}
}
//14.非递归的后序
void NicePastOrder(BtNode *ptr)
{
if (ptr == NULL) return;
stack<BtNode*> st;
BtNode * tag = NULL;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
if (ptr->rightchild == NULL || ptr->rightchild == tag)
{
cout << ptr->data << " ";
tag=ptr;
ptr = NULL;
}
else
{
st.push(ptr);
ptr = ptr->rightchild;
}
}
cout << endl;
}
//15.非递归的层次,广度遍历
void NiceLeveOrder(BtNode* ptr)
{
if (ptr == NULL) return;
queue<BtNode*> sq;
sq.push(ptr);
while (!sq.empty())
{
ptr=sq.front();
sq.pop();
cout << ptr->data << " ";
if (ptr->leftchild != NULL)
{
sq.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
sq.push(ptr->rightchild);
}
}
cout<<endl;
}
//16.递归的层次,广度遍历
void Print_K(BtNode *ptr,int k)
{
if(ptr != NULL && k == 0)
{
cout<<ptr->data<<" ";
}
else if(ptr != NULL)
{
Print_K(ptr->leftchild,k-1);
Print_K(ptr->rightchild,k-1);
}
}
void Print_K_Item(BtNode *ptr,int k)//打印第k层结点
{
if(ptr == NULL || k < 0)
return ;
Print_K(ptr,k);
cout<<endl;
}
void LevelOrder(BtNode *ptr)
{
int n = Depth(ptr);
for(int i = 0;i<n;++i)
{
Print_K_Item(ptr,i);
}
}
//17.根据结点
bool Is_Full_BinaryTree1(BtNode *ptr)
{
int h = Depth_Tree(ptr);
int total = Size_Tree(ptr);
if (total = pow(2, h + 1) - 1)
{
return true;
}
}
//18.根据高度
bool Is_Full_BinaryTree2(BtNode* ptr)
{
return (ptr == NULL) ||
(Is_Full_BinaryTree2(ptr->leftchild) &&
Is_Full_BinaryTree2(ptr->rightchild)&&
Depth_Tree(ptr->leftchild) == Depth_Tree(ptr->rightchild));
}
//19.层次
bool Is_Full_BinaryTree3(BtNode* ptr)
{
bool res = true;
if (ptr == NULL){ return true; }
queue<BtNode*> sq;
int n = 1;
sq.push(ptr);
while (!sq.empty())
{
for (int i = 0; i < n&&!sq.empty(); ++i)
{
BtNode*p= sq.front();
sq.pop();
if (p->leftchild != NULL)
{
sq.push(p->leftchild);
}
if (p->rightchild != NULL)
{
sq.push(p->rightchild);
}
if (i < n)
{
res = false;
break;
}
}
n += n;
}
return res;
}
//20.寻找最近双亲
BtNode *FindNearParent();
//21.寻找双亲(都打印)
BtNode* Find(BtNode* ptr, BtNode* child)
{
if (ptr == NULL || ptr->leftchild == child || ptr->rightchild == child)
{
return ptr;
}
else
{
BtNode* p = Find(ptr->leftchild, child);
if (NULL == p)
{
p = Find(ptr->rightchild, child);
}
return p;
}
}
BtNode *FindParent(BtNode *ptr, BtNode *child)
{
if (ptr == NULL||child==NULL||ptr == child){return NULL;}
else return Find(ptr,child);
}
//22.判断是不是完全二叉树
bool Is_Comp_BinaryTree(BtNode *ptr)
{
bool res = true;
if (ptr == NULL){ return true; }
queue<BtNode*> sq;
sq.push(ptr);
while (!sq.empty())
{
BtNode*p= sq.front();
sq.pop();
if (p == NULL)
{
break;
}
sq.push(p->leftchild);
sq.push(p->rightchild);
}
while (!sq.empty())
{
BtNode*q = sq.front;
sq.pop();
if (q != NULL)
{
res = false;
break;
}
}
return res;
}
总结:代码不是很繁琐,主要在于思想,加油!