二叉树
#include <iostream>
#define MaxSize 10
结构体
typedef struct treenode{
struct treenode * lchild,* rchild;
int info;
int tag;
}*tree;
1.二叉树的创建
tree buildtree()
{
char ch;
tree t;
ch=getchar();
if(ch=='#')
t=NULL;
else{
t=(tree)malloc(sizeof(treenode));
t->info=ch;
t->tag=0;
t->lchild=buildtree();
t->rchild=buildtree();
}
return t;
}
2.递归遍历二叉树
void DGpreOrder(tree t)
{
if (t){
printf("%c",t->info);
DGpreOrder(t->lchild);
DGpreOrder(t->rchild);
}
}
void DGmidOrder(tree t)
{
if (t){
DGmidOrder(t->lchild);
printf("%c",t->info);
DGmidOrder(t->rchild);
}
}
void DGlastOrder(tree t)
{
if (t){
DGlastOrder(t->lchild);
DGlastOrder(t->rchild);
printf("%c",t->info);
}
}
3.非递归遍历二叉树
void preOrder(tree t){
treenode *stack[10];
treenode *p;
p=t;
int top=-1;
while (p || top!=-1)
{
if (p)
{
top++;
stack[top]=p;
printf("%c",p->info);
p=p->lchild;
} else{
p=stack[top];
top--;
p->rchild;
}
}
}
void midOrder(tree t){
treenode *stack[10];
treenode *p;
p=t;
int top=-1;
while (p || top!=-1)
{
if (p)
{
top++;
stack[top]=p;
p=p->lchild;
} else{
p=stack[top];
top--;
printf("%c",p->info);
p->rchild;
}
}
}
void lastOrder(tree t){
treenode *stack[10];
treenode *p;
p=t;
int top=-1;
while (p || top!=-1)
{
if(p)
{
top++;
stack[top]=p;
p=p->lchild;
} else{
p=stack[top];
if (p->rchild && p->rchild->tag==0){
p=p->rchild;
}
else{
p=stack[top];
top--;
printf("%c",p->info);
p->tag=1;
p=NULL;
}
}
}
}
4.二叉树高
int DGdepth(tree t){
int lh,rh,h;
if (t==NULL)
return 0;
else{
lh= DGdepth(t->lchild);
rh= DGdepth(t->rchild);
if (lh>=rh)
h=lh+1;
else
h=rh+1;
}
return h;
}
int depth(tree t){
if (t==NULL) return 0;
treenode *s[10];
treenode *p;
int front =-1,rear=-1;
int h=0,L=0;
s[++rear]=t;
while (front<rear){
p=s[++front];
if (p->lchild)
s[++rear]=p->lchild;
if (p->rchild)
s[++rear]=p->rchild;
if (front==L){
printf("HIGH++");
printf("\n");
printf("rear:%d",rear);
printf("\n");
printf("front:%d",front);
printf("\n");
printf("L:%d",L);
printf("\n");
L=rear;
h++;
}
}
return h;
}
5.遍历最后/第一个元素
treenode* preOrderLast(tree t){
if (t->rchild)
t= preOrderLast(t->rchild);
else
t= preOrderLast(t->lchild);
return t;
}
* 中序最后
* @return
*/
treenode* midOrderLast(tree t){
if (t->rchild)
t= midOrderLast(t->rchild);
return t;
}
6.遍历第一个元素
treenode* midOrderFrist(tree t){
if (t->lchild)
t= midOrderLast(t->lchild);
return t;
}
treenode* lastOrderFrist(tree t){
if (t->lchild)
t= lastOrderFrist(t->lchild);
else
t= lastOrderFrist(t->lchild);
return t;
}
7.层序遍历
/**
* 链式队列实现层序遍历
* @param t
*/
void levelOrder(tree t){
treenode *s[10];
int front,rear,tag;
front=rear=-1;
tag=0;
treenode *p;
s[++rear]=t;
while (front<rear && tag==0)
{
p=s[++front];
printf("%c",p->info);
if (p->lchild)
s[++rear]=p->lchild;
if (p->rchild)
s[++rear]=p->rchild;
}
}
struct squeue {
struct treenode* data[MaxSize];
int front, rear, tag;
};
bool EnQueue(squeue& s, treenode *x)
{
if (s.front == s.rear && s.tag == 1)
{
printf("队满 进队失败");
return false;
}
s.data[s.rear] = x;
s.rear = (s.rear + 1) % MaxSize;
s.tag = 1;
return true;
}
int DeQueue(squeue& s, treenode* &x)
{
if (s.front == s.rear && s.tag == 0)
{
printf("空 出队失败");
return 0;
}
x = s.data[s.front];
s.front = (s.front + 1) % MaxSize;
s.tag = 0;
return 1;
}
void levelOrder2(tree t)
{
squeue q;
int front,rear,tag;
front=rear=tag=0;
treenode *p;
EnQueue(q,t);
while(!(front==rear&&tag==0))
{
DeQueue(q,p);
printf("%c",p->info);
if(p->lchild)
EnQueue(q,p->lchild);
if(p->rchild)
EnQueue(q,p->rchild);
}
}
8.交换左右子树
void change(tree &t){
tree temp;
if (t==NULL)
return;
else if (t->lchild ==NULL && t->rchild ==NULL)
return;
else{
temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
change(t->lchild);
change(t->rchild);
}
}
9.双分支结点个数
int num(tree t){
if (t==NULL)
return 0;
if (t->rchild && t->lchild)
return num(t->lchild)+ num(t->rchild)+1;
else
return num(t->lchild)+ num(t->rchild);
}
10.总结点个数
/***
* 递归
* @return
*/
int sum(tree t){
if (t==NULL) return 0;
else
return sum(t->lchild)+ sum(t->rchild)+1;
}
11.叶子结点个数
int LeafCount(tree t){
if (t==NULL)
return 0;
else if (t->lchild==NULL && t->rchild==NULL)
return 1;
else
return LeafCount(t->rchild)+ LeafCount(t->lchild);
}
int LeafCount2(tree t){
int count=0;
int top=-1;
tree stack[100];
while(t!=NULL || top!=-1){
while(t!=NULL){
if (t->lchild==NULL && t->rchild==NULL)
count++;
stack[++top]=t;
t=t->lchild;
}
if (top!=-1)
{
t=stack[--top];
t=t->rchild;
}
}
return count;
}
12.第k层结点个数
int getCountK(tree t,int k){
if (k<0 || t==NULL) return 0;
else if (k==1) return 1;
else
return getCountK(t->lchild,k-1)+ getCountK(t->rchild,k-1);
}
13.判断二叉树等价
int isequal(tree t1,tree t2){
int t=0;
if (t1==NULL && t2==NULL)
return 1;
else{
if (t1!=NULL && t2!=NULL)
if (t1->info==t2->info)
if (isequal(t1->lchild,t2->lchild))
t= isequal(t1->rchild,t2->rchild);
}
return t;
}
14.查找第x结点
tree searchX(tree t,char x){
tree p;
if (t==NULL)
return NULL;
else{
if (t->info == x)
return t;
else{
p= searchX(t->lchild,x);
if (p)
return p;
else
return searchX(t->rchild,x);
}
}
}