《大话数据结构》第六章 树
来源:互联网 发布:济南富士软件怎么样 编辑:程序博客网 时间:2024/06/06 03:06
树
ADT 树
Operation:
InitTree(*T);
DestroyTree(*T);
CreateTree(*T,definition);
ClearTree(*T);
TreeEmpty(T);
TreeDepth(T);
Root(T);
Value(T,cur_e);
Assign(T,cur_e,value);
Parent(T,cur_e);
LeftChild(T,cur_e);
RightSibling(T,cur_e);
InsertChild(*T,*p,i,c);
DeleteChild(*T,*p,i);
双亲表示法
根节点没有双亲,约定根节点的位置域设置为-1
#define Max_Tree_Size 100typedef int TElemType;typedef struct PTNode{ TElemType data; int parent;}PTNode;typedef struct PTree{ PTNode nodes[Max_Tree_Size]; int r,n; //根的位置和节点数 }; //根节点没有双亲,约定根节点的位置域设置为-1
孩子表示法
#define Max_Tree_Size 100typedef struct CTNode{ int child; struct CTNode *next;}*ChildPtr;typedef struct CTBox{ TElemType data; ChildPtr firstchild;}CTBox;typedef struct CTree{ CTBox node[Max_Tree_Size]; int r,n;//根的位置和节点数}CTree;
双亲孩子表示法
#define Max_Tree_Size 100typedef struct PCTNode{ int child; struct PCTNode *next;}*ChildPtr; typedef struct PCTBox{ TElemType data; int parent; ChildPtr firstchild;};typedef struct PCTree{ PCTBox nodes[Max_Tree_Size]; int n,r;}PCTree;
孩子兄弟表示法
想找双亲,完全可以增加一个parent指针域
/*孩子兄弟表示法*/typedef struct CSNode{ TElemType data; struct CSNode *firstchild, *rightsib;}CSNode, *CSTree; //想找双亲,完全可以增加一个parent指针域/* struct CSNode *parent;*/
特殊的二叉树:斜树、满树、完全二叉树
二叉树的存储结构
1.顺序存储结构
2.二叉链表
typedef struct BiTNode{ TElemType data; struct BiTNode *lchild, *rchild;}BiTNode, *BiTree;
遍历二叉树
1.前序遍历
void PreOrderTraverse(BiTree T){ if(T==NULL) return; printf("%c",T->data); PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild);}
2.中序遍历
void InOrderTraverse(BiTree T){ if(T==NULL) return; InOrderTraverse(T->lchild); printf("%c",T->data); InOrderTraverse(T->rchild);}
3.后序遍历
void PostOrderTraverse(BiTree T){ if(T==NULL) return; PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); printf("%c",T->data);}
二叉树的建立
前序遍历
void CreateBiTree(BiTree *T){ TElemType ch; scanf("%c", &ch); if(ch=='#') *T=NULL; else{ *T=(BiTree)malloc(sizeof(BiTNode)); if(!T) exit(OVERFLOW); (*T)->data=ch; CreateBiTree(&(*T)->lchild); CreateBiTree(&(*T)->rchild); }}
中序遍历
后序遍历
搜索二叉树
把指向前驱和后继的指针称为线索,加上线索的二叉链表成为搜索链表,相应的二叉树成为搜索二叉树
/*二叉树的二叉搜索存储结构定义*/typedef enum {Link, Thread} PointerTag;typedef struct BiThrNode{ TElemType data; struct BiThrNode *lchild, *rchild; PointerTag LTag; PointerTag RTag;}BiThrNode *BiThrTree;/*中序遍历搜索化的递归函数代码*/BiThrTree pre; void InThreading(BiThrTree p){ if(p){ InThreading(p->lchild); if(!p->lchild){ p->LTag = Thread; p->lchild = pre; } if(!pre->rchild){ pre->RTag=Thread; pre->lchild=p; } pre=p; InTreading(p->rchild); }} /*遍历代码如下*//*T指向头结点,头结点左链lchild指向根节点,头结点右链rchild指向中序遍历的最后一个节点*/Status InOrderTraverse_Thr(BiThrTree T){ BiThrTree p; p = T->lchild; while(p!=T) { while(p->LTag==Link) p=p->lchild; printf("%c", p->data); while(p->RTag==Thread && p->rchild!=T){ p=p->rchild; printf("%c", p->data); } p=p->rchild; } return OK;}
树、森林、二叉树之间的转换
树和森林的遍历
树的遍历:先根遍历、后根遍历
森林的遍历:前序遍历、后续遍历
阅读全文
0 0
- 《大话数据结构》第六章 树
- 《大话数据结构》笔记之 第六章 树
- 大话数据结构 第六章 树(读书笔记)
- 《大话数据结构》—第六章—树
- 大话数据结构读书笔记——第六章 树
- [数据结构]第六章-树
- 《大话数据结构》第6章.树
- 大话数据结构--树
- 大话数据结构中的树
- 大话数据结构4 - 树
- 大话数据结构笔记-树
- 《大话数据结构》样章试读
- 大话数据结构 第二章 读书笔记
- 大话数据结构 -- 第八章 查找
- 大话数据结构 -- 第九章 排序
- 《大话数据结构》第四章 队列
- 《大话数据结构》 第七章 图
- 《大话数据结构》第九章 排序
- js分页插件
- Ubuntu删除客人帐号
- 分享一百多套开发视频教程的下载地址
- Spring Uploading Files 官方示例项目解析
- PHP select筛选提交form表单请求
- 《大话数据结构》第六章 树
- RBAC权限管理
- 数据结构与算法之图计算
- 将sqoop2的默认元数据库derby 迁移到mysql
- 【雪野实训记录】Oracle聊天室-L1阶段项目案例
- java打jar架包 笨笨的我 弄了好久
- 使用appium+python对android进行自动化测试
- 2017/8/10开通博客,测试一下文章格式
- 解决谷歌chrome浏览器新版 58以上不支持浏览器编码的解决办法