1.1 二叉数据的定义
typedef struct BTNode
{
int data;
struct BTNode *lchild;
struct BTNode *rchild;
} BTNode;
2.1 二叉树的先序,中序,后续遍历(递归)
// 先序遍历
void preorder(BTNode *p)
{
if (p!=NULL)
{
visit(p);
preorder(p->rchild);
preorder(p->lchild);
}
}
// 中序遍历
void inorder(BTNode *p)
{
if (p!=NULL)
{
inorder(p->rchild);
visit(p);
inorder(p->lchild);
}
}
// 后序遍历
void posorder(BTNode *p)
{
if (p!=NULL)
{
posorder(p->rchild);
posorder(p->lchild);
visit(p);
}
}
2.2 二叉树的先序,中序,后续遍历(非递归)
// 先序非递归遍历
void preorderNonrecursion(BTNode *bt)
{
if{bt!=NULL}
{
BTNode *Stack[maxSize]; int top =-1;
BTNode *p;
p = bt
Stack[++top] = p;
while(top!=-1)
{
p =Stack[top--];
Visit(p);
if(p->rchild != NULL)
Stack[++top]=p->rchild;
if(p->lchild != NULL)
Stack[++top]=p->lchild;
}
}
}
// 中序非递归遍历
void inorderNonrecursion(BTNode *bt)
{
if(bt !=NULL)
{
BTNode *stack[maxSize];top =-1;
BTNode *p;
p = bt;
while(top!=-1||p!=NULL)
{
while(p!=NULL)
{
Stack[++top] = P;
p = p->lchild
}
if(top != -1)
{
p = Stack(top--);
Visit(p);
p = p->rchild;
}
}
}
}
// 后序非递归遍历
void posorderNonrecursion(BTNode *bt)
{
if{bt!=NULL}
{
BTNode *Stack1[maxSize] ;int top1 =-1;
BTNode *Stack2[maxSize] ;int top2 =-1;
BTNode *p = NULL;
BTNode[++top1] = bt;
while(top1!=-1)
{
p = Stack1[top1--]
Stack2[++top2] = p;
if(p->lchild!=NULL)
Stack1[++top1] = p->lchild;
if(p->rchild!=NULL)
Stack1[++top1] = p->rchild;
}
while (top2!=-1)
{
p= Stack2[top2--];
Visit(p);
}
}
3.1 线索二叉树的定义
typedef struct TBTNode
{
char data;
int ltag,rtag;
struct TBTNode *lchild;
struct TBTNode *rchild;
}TBTNode;
3.2 线索二叉树的遍历(中序)
void InThread(TBTNode *p,TBTNode *&pre)
{
if(p!=NULL)
{
InThread(p->lchild,pre)
if(p->lchild == NULL)
{
p->lchild = pre;
p->ltag=1;
}
if(pre !=NULL&&pre->rchild ==NULL)
{
pre->rchild =p;
pre->rtag =1;
}
pre = p;
InThread(p->rchild,pre)
}
}
void createInThread(TBTNode *root)
{
TBTNode *pre = NULL;
if(root != NULL)
{
InThread(root,pre);
pre->rchild = NULL;
pre->rtag = 1;
}
}
TBTNode *First(TBTNode *p)
{
while(p->rtag==0)
{
p = p->rchild
}
return p;
}
TBTNode *Next(TBTNode *p)
{
if(p->rtag = 0)
return First(p->rchild)
else
return p->rchild
}
void Inorder(TBNode *root)
{
for(TBNode *p=First(root);p!=NULL;p=Next(p))
Visit(p);
}
4.1 层次遍历二叉树
void level(BTNode *p)
{
if(p != NULL)
{
int front,rear;
BTNode *que[maxSize];
front = rear =0;
BTNode *q;
rear = [rear+1]%maxSize;
que[rear] = p;
while(front != rear)
{
front = (front+1)%maxSize;
q = que(front);
Visit(q);
if(q->lchild != NULL)
{
rear = (rear + 1)%maxSize
que[rear]=q->lchild;
}
if(q->rchild != NULL)
{
rear = (rear + 1)%maxSize
que[rear]=q->rchild;
}
}
}
}
5.1 二叉树深度的三种写法(递归,非递归层次遍历,非递归中序遍历)
// 二叉树的深度
int getDepth(BTNode *p)
{
int LD,RD;
if (p==NULL)
{
return 0;
}
else
{
LD=getDepth(p->lchild);
RD=getDepth(p->rchild);
return(LD>RD?LD:RD)+1;
}
}
// 二叉树的深度非递归写法--层次遍历
int btDepth(BiTree *bt)
{
if (bt==NULL)
return 0;
BiTree *que[MaxSize];
int front=-1;rear=-1;
int last =0; level =0;
BiTree *p;
que[++rear] =T;
while(front<rear)
{
p=que[++front];
if(p->lchild)
que[++rear]=p->lchild;
if(p->rchild)
que[++rear]=p->rchild;
if(front=last)
{
level++;
last=rear;
}
}
return level;
}
// 二叉树的深度非递归写法--中序遍历
#define MaxSize 50;
int Depth(BTNode bt)
{
if(p ==NULL)
return 0;
BTNode *stack1[MaxSize];
int stack2[MaxSize];
int curdepth,maxdepth =0,top =-1;
BTNode *p;
p=bt;
if(p !=NULL)
{
curdepth =1;
while(p!=NULL||top!=-1)
{
while(p!=NULL)
{
stack1[++top]=p;
stack2[top]=curdepth;
p=p->lchild;
}
if(p!=-1)
{
p=stack1[top--];
curdepth=stack2[top];
if(p->lchild==NULL&&p->rchild ==NULL)
{
if(curdepth>maxdepth)
maxdepth=curdepth;
}
p=p->rchild;
curdepth++;
}
}
return maxdepth;
}
5.2 判断是否是完全二叉树
int iscomplete(BTNode *p)
{
if(p != NULL)
{
int front,rear;
BTNode *que[maxSize];
front = rear =0;
BTNode *q;
rear = [rear+1]%maxSize;
que[rear] = p;
while(front != rear)
{
front = (front+1)%maxSize;
q = que(front);
if(p!=NULL)
{
rear = (rear + 1)%maxSize
que[rear]=q->lchild;
rear = (rear + 1)%maxSize
que[rear]=q->rchild;
}
else
{
while(front != rear)
front = (front+1)%maxSize;
q = que(front);
if(p!=NULL)
return 0;
}
}
}
return 1;
}
5.3 双分支数的个数--层次遍历
// 双分支数的个数--层次遍历
int DsonDodes(BTNode *p)
{
if(p != NULL)
{
int n;
n=0
int front,rear;
BTNode *que[maxSize];
front = rear =0;
BTNode *q;
rear = [rear+1]%maxSize;
que[rear] = p;
while(front != rear)
{
front = (front+1)%maxSize;
q = que(front);
if(p->lchild==NULL&&p->rchild==NULL)
n++;
if(q->lchild != NULL)
{
rear = (rear + 1)%maxSize
que[rear]=q->lchild;
}
if(q->rchild != NULL)
{
rear = (rear + 1)%maxSize
que[rear]=q->rchild;
}
}
}
return n;
}
5.4 删除以x为根节点的子树
// 后序遍历删除
void DeleteTree(BTNode *p)
{
if (p!=NULL)
{
DeleteTree(p->rchild);
DeleteTree(p->lchild);
free(p);
}
}
void Deletsearch(BTNode *p,int x)
{
if(p != NULL)
{
int front,rear;
BTNode *que[maxSize];
front = rear =0;
BTNode *q;
rear = [rear+1]%maxSize;
que[rear] = p;
while(front != rear)
{
front = (front+1)%maxSize;
q = que(front);
Visit(q);
if(q->lchild != NULL)
{
if (q->lchild->data==x)
{
DeleteTree(p->lchild);
q->lchild=NULL;
}
else
{
rear = (rear + 1)%maxSize
que[rear]=q->lchild;
}
}
if(q->rchild != NULL)
{
if (q->rchild->data==x)
{
DeleteTree(p->rchild);
q->rchild=NULL;
}
else
{
rear = (rear + 1)%maxSize
que[rear]=q->rchild;
}
}
}
}
}
5.5 先序遍历出第K个节点的值
// 先序遍历出第K个节点的值
int n=0;
vod trave(BTNode *p,int k)
{
if(p!=NULL)
{
++n;
if(k==n)
printf(p->data);
return ;
trave(p->lchild,k)
trave(p->rchild,k)
}
}
}
5.6 交换左右子树
void swap(BTNode *p)
{
if(p!=NULL)
{
swap(b->lchild)
swap(b->rchild)
temp=b->lchild;
b->lchild=b->rchild;
b->rchild=temp;
}
}