首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 终于会写二叉树了!贴出来高兴一下
【标  题】:终于会写二叉树了!贴出来高兴一下
【关键字】:
【来  源】:http://blog.csdn.net/czlt86/archive/2006/11/27/1416580.aspx

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

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

#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;
 }
}

[Mudos]LPC实现快速排序:【上一篇】
Week Overview(1124):【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 2005 C++技术大会观感——我的流水帐
  • http、tomcat进程监控脚本
  • 构建安全Linux系统十二守则
  • PS视频教程:数码手的制作(4)
  • [推荐]系统发邮件测试 Dumbster
  • 一个男朋友写给女朋友的信.爆笑(真勇敢)
  • telnet详解
  • 多语言界面 Web 站点的几个 Tip
  • 树结构查询
  • db2 install/restore/client connect
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.