终于会写二叉树了!贴出来高兴一下

早就看数据结构后面的章节里有个二叉树,觉得很深奥,很神秘,没想到现在自己也会了,高兴啊!!!

#include "stdio.h"
#define OK 1
#define LEN sizeof(struct BiTNode)
#define TRUE 1
#define FALSE 0
struct BiTNode    //定义一个二叉树结点
{
 char data;
 struct BiTNode *lchild,*rchild;
};

struct QueueNode    //定义一个队列节点
{
 struct BiTNode data;
 struct QueueNode *next;
};

struct Queue     //定义一个队列
{
 struct QueueNode *front;
 struct QueueNode *rear;
};

struct BiTNode *CreateTree(char [],int);
struct BiTNode *FindInTree(struct BiTNode *,struct BiTNode *);
struct BiTNode *FindPrInTree(struct BiTNode *,struct BiTNode *);
struct BiTNode *LeftChild(struct BiTNode *,struct BiTNode *);
struct BiTNode *Parent(struct BiTNode *,struct BiTNode *);
struct BiTNode *RightSibling(struct BiTNode *,struct BiTNode *);

main()
{
 struct BiTNode *root1,*root2;  //定义两个二叉树的根节点
 int chi=1;      //用作建立二叉树的游标
 char ch1[10]="abcdefghi";    //定义两个二叉树
 char ch2[10]="jklmnogpr";
 root1=CreateTree(ch1,chi);
 root2=CreateTree(ch2,chi);
}

DestroyTree(struct BiTNode *root)  //销毁树
{
 root->data=0;
 root->lchild=root->rchild=root=NULL;
}

ClearTree(struct BiTNode *root)     //清空树
{
 root->data=0;
 root->lchild=root->rchild=NULL;
}

TreeEmpty(struct BiTNode *root)     //判断二叉树是否为空
{
 if (root!=NULL)
 return TRUE;
 else
 return FALSE;
}

TreeDepth(struct BiTNode *root)     //返回二叉树的深度
{
 int i=1;
 struct BiTNode *p;
 p=root;
 while(p->lchild!=NULL)
 {
  i++;
  p=p->lchild;
 }
 return i;
}

struct BiTNode *Root(struct BiTNode *root)       //返回二叉树的根节点
{
 return root;
}

Value(struct BiTNode *root,struct BiTNode *cur)     //返回指定节点的值
{
 struct BiTNode *p;
 p=FindInTree(root,cur);
 if (p==NULL)
 return FALSE;
 else
 return p->data;
}

Assign(struct BiTNode *root,struct BiTNode *cur,char value)   //给指定节点赋值
{
 struct BiTNode *p;
 p=FindInTree(root,cur);
 if(p==NULL)
 return FALSE;
 else
 {
  p->data=value;
 }
}

struct BiTNode *FindInTree(struct BiTNode *root,struct BiTNode *cur)     //查找指定节点
{
 if(root->data==cur->data)
 return root;
 else
 {
  root=FindInTree(root->lchild,cur);
  root=FindInTree(root->rchild,cur);
 }
}

struct BiTNode *FindPrInTree(struct BiTNode *root,struct BiTNode *cur)  //查找指定节点的双亲
{
 if(root->lchild->data==cur->data||root->rchild->data==cur->data)
 return root;
 else
 {
  root=FindPrInTree(root->lchild,cur);
  root=FindPrInTree(root->rchild,cur);
 }
}

struct BiTNode *LeftChild(struct BiTNode *root,struct BiTNode *cur)  //返回指定节点的最左孩子
{
 struct BiTNode *p;
 p=FindInTree(root,cur);
 if(p->lchild==NULL)
 return NULL;
 while(p->lchild!=NULL)
 p=p->lchild;
 return p;
}

struct BiTNode *Parent(struct BiTNode *root,struct BiTNode *cur)  //返回指定节点的双亲
{
 struct BiTNode *pr;
 pr=FindPrInTree(root,cur);
 return pr;
}

struct BiTNode *RightSibling(struct BiTNode *root,struct BiTNode *cur)  //返回指定节点的右兄弟
{
 struct BiTNode *pr;
 pr=FindPrInTree(root,cur);
 pr=pr->rchild;
 if(pr==NULL)
 return NULL;
 else
 return pr;
}

InsertChild(struct BiTNode *root1,struct BiTNode *root2,struct BiTNode *cur)    //在指定节点下插入子树
{
 struct BiTNode *p;
 p=FindInTree(root1,cur);
 if (p->lchild!=NULL&&p->rchild!=NULL)
 return NULL;
 else
 {
  if(p->lchild==NULL)
  p->lchild=root2;
  else
  p->rchild=root2;
 }
}

DeleteChild(struct BiTNode *root,struct BiTNode *cur)      //在指点节点下删除子树
{
 struct BiTNode *p;
 p=FindInTree(root,cur);
 p->lchild=p->rchild=NULL;
}

struct BiTNode *CreateTree(char ch[],int chi)      //按定义建立二叉树
{
 if(chi>=10)
 return NULL;
 else
 {
  struct BiTNode *root;
  root=(struct BiTNode *)malloc(LEN);
  root->data=ch[chi-1];
  root->lchild=CreateTree(ch,chi*2);
  root->rchild=CreateTree(ch,chi*2+1);
  return root;
 }
}

PreOrderTraverse(struct BiTNode *root)        //先序遍历二叉树
{
 printf("%c/n",root->data);
 getch();
 if (root->lchild!=NULL)
 PreOrderTraverse(root->lchild);
 if(root->rchild!=NULL)
 PreOrderTraverse(root->rchild);
}

InOrderTraverse(struct BiTNode *root)         //中序遍历二叉树
{
 if(root->lchild!=NULL)
 InOrderTraverse(root->lchild);
 printf("%c/n",root->data);
 getch();
 if(root->rchild!=NULL)
 InOrderTraverse(root->rchild);
}

PostOrderTraverse(struct BiTNode *root)      //后序遍历二叉树
{
 if(root->lchild!=NULL)
 PostOrderTraverse(root->lchild);
 if(root->rchild!=NULL)
 PostOrderTraverse(root->rchild);
 printf("%c/n",root->data);
 getch();

EnQueue(struct Queue *Q,struct BiTNode *TreeNode)     //进队操作
{
 struct QueueNode *p;
 p=(struct QueueNode *)malloc(sizeof(struct QueueNode));
 p->data=*TreeNode;
 p->next=NULL;
 if(Q->front==NULL)
 {
  Q->front=p;
  Q->rear=p;
 }
 else
 {
  Q->rear->next=p;
  Q->rear=p;
 }
}

DeQueue(struct Queue *Q)           //出队操作
{
 struct QueueNode *p;
 p=Q->front;
 printf("%c/n",p->data.data);
 Q->front=p->next;
}

QueueIsEmpty(struct Queue *Q)                 //判断队列是否为空
{
 if(Q->front==NULL)
 return 1;
 else
 return 0;
}

LevelOrderTraverse(struct BiTNode *root)    //层序遍历二叉树
{
 struct Queue Q;
 struct BiTNode *p;
 p=root;
 Q.rear=(struct QueueNode *)malloc(sizeof(struct QueueNode));
 Q.rear=NULL;
 Q.front=Q.rear;
 EnQueue(&Q,p);
 while(!QueueIsEmpty(&Q))
 {
  if(p->lchild)
  EnQueue(&Q,p->lchild);
  if(p->rchild)
  EnQueue(&Q,p->rchild);
  DeQueue(&Q);
  *p=Q.front->data;
 }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值