@基本操作 查找类 e Root(T ∥求树的根结点 Value(, cur e ∥求当前结点的元素值 o Parent(,cur e ∥求当前结点的双亲结点 G LeftChild(T, cur ∥求当前结点的最左孩子 Rightsibling(T, cur e ∥求当前结点的右兄弟 TreeDepth(T) ∥求树的深度 的 Traverse Tree(T,Ⅴisit0)∥遍历 插入类 ca InitTree(&t ∥初始化置空树 Createtree((&T, definition)∥/按定义构造树 Assign(,curc, value)∥给当前结点赋值 Insertchild(&工,&Pc)∥将以c为根的树插入为结点p的第棵子树 删除类 Delete child(&T,&p,i)∥删除结点p的第裸子树 Deletechild(&T,&p,i)∥删除结点p的第裸棵子树 第6页 Delete child(&T,&p,i)∥删除结点p的第棵子树
Data Structure 数 据 结 构—— 第 6 章 树 和 二 叉 树 胡建华 2021/2/19 计算机教研室 第6页 基本操作: • 查 找 类 Root(T) // 求树的根结点 Value(T, cur_e) // 求当前结点的元素值 Parent(T, cur_e) // 求当前结点的双亲结点 LeftChild(T, cur_e) // 求当前结点的最左孩子 RightSibling(T, cur_e) // 求当前结点的右兄弟 TreeDepth(T) // 求树的深度 TraverseTree( T, Visit() ) // 遍历 • 插 入 类 InitTree(&T) // 初始化置空树 CreateTree(&T, definition) // 按定义构造树 Assign(T, cur_e, value) // 给当前结点赋值 InsertChild(&T, &p, i, c) // 将以c为根的树插入为结点p的第i棵子树 • 删 除 类 DeleteChild(&T, &p, i) // 删除结点p的第i棵子树 DeleteChild(&T, &p, i) // 删除结点p的第i棵子树 DeleteChild(&T, &p, i) // 删除结点p的第i棵子树
@632树和森林的存储结构 ·1.双亲表示法: data parent A 0A-1 r=0 1B0 BCD n=6 2C0 E( 3D0 4E2 5F2 6G5 计算机教研宦 第7页 2021/2/19
Data Structure 数 据 结 构—— 第 6 章 树 和 二 叉 树 胡建华 2021/2/19 计算机教研室 第7页 6.3.2 树和森林的存储结构 • 1.双亲表示法: A B C D E F G 0 A -1 1 B 0 2 C 0 3 D 0 4 E 2 5 F 2 6 G 5 r=0 n=6 data parent
@C语言的类型描述: #define MAX TREE sIze 100 结点结构: data parent typedef struct PTNode i Elem data int parent;∥双亲位置域 3 PTNode 树结构: 0 ty pede struct i PTNode nodes MAX TREE Size intr.n:∥/根结点的位置和结点个数 3 PTree 计算机教研宦 第8页 2021/2/19
Data Structure 数据结构—— 第6章树和二叉树 胡建华 2021/2/19 计算机教研室 第 8 页 C语言的类型描述 : #define MAX_TREE_SIZE 100 • 结点结构: typedef struct PTNode { Elem data; int parent; // 双亲位置域 } PTNode; • 树结构 : typedef struct { PTNode nodes [MAX_TREE_SIZE]; int r, n; // 根结点的位置和结点个数 } PTree; data parent
@2.孩子链表表示法: data firstchild 0A BQ①1B A 2 C 5∧ 3 D 4 E G 5F ∧ 6∧ 06 6 G 计算机教研室 第9页 2021/2/19
Data Structure 数 据 结 构—— 第 6 章 树 和 二 叉 树 胡建华 2021/2/19 计算机教研室 第9页 A B C D E F G 0 A 1 B 2 C 3 D 4 E 5 F 6 G r=0 n=6 data firstchild 1 2 3 4 5 6 2.孩子链表表示法:
C语言的类型描述: 孩子结点结构: typedef struct CTNode i int child child next struct CTNode * next: 3*ChildPtr; 双亲结点结构 图 typedef struct{ Elem data: ChildPtr firstchild;∥/孩子链的头指针 3 CTBOx; 树结构: typedef struct t CTBox nodes MAX TREE Size; intn,r;∥/结点数和根结点的位置 3 CTree 计算机教研宦 第10页 2021/2/19
Data Structure 数 据 结 构—— 第 6 章 树 和 二 叉 树 胡建华 2021/2/19 计算机教研室 第10页 C语言的类型描述: • 孩子结点结构: typedef struct CTNode { int child; struct CTNode *next; } *ChildPtr; • 双亲结点结构 typedef struct { Elem data; ChildPtr firstchild; // 孩子链的头指针 } CTBox; • 树结构: typedef struct { CTBox nodes[MAX_TREE_SIZE]; int n, r; // 结点数和根结点的位置 } CTree; child next