left Child data right Child data right Child
- Slides: 80
二叉树的链表表示 left. Child data right. Child data right. Child left. Child 二叉链表 2020/11/22 北京化 大学信息学院 数据结构 13
二叉树的链表表示 left. Child data parent right. Child parent data right. Child left. Child 三叉链表 2020/11/22 北京化 大学信息学院 数据结构 14
root A B C D E F data parent left. Child right. Child 0 1 2 3 4 5 A -1 B 0 2 3 C 1 -1 D 1 4 5 E 3 -1 F 3 -1 三叉链表的静态结构 2020/11/22 北京化 大学信息学院 数据结构 16
二叉树的定义 typedef char Tree. Data; //树结点数据类型 typedef struct node { //树结点定义 Tree. Data data; //结点数据域 struct node * left. Child, * rightchild; //子女指针域 } Bin. Tree. Node; typedef Bin. Tree. Node * Bin. Tree; //树定义,代表树的根指针 2020/11/22 北京化 大学信息学院 数据结构 17
二叉树递归的中序遍历算法 int In. Traverse(Bi. Tree T) { if(T==NULL) return 0; In. Traverse(T->lchild); Visit(T); In. Traverse(T->rchild); return 1; } 2020/11/22 北京化 大学信息学院 数据结构 21
二叉树递归的前序遍历算法 int Pre. Traverse(Bi. Tree T) { if(T==NULL) return 0; Visit(T); Pre. Traverse(T->lchild); Pre. Traverse(T->rchild); return 1; } 2020/11/22 北京化 大学信息学院 数据结构 23
二叉树递归的后序遍历算法 int Suc. Traverse(Bi. Tree T) { if(T==NULL) return 0; Suc. Traverse(T->lchild); Suc. Traverse(T->rchild); Visit(T); return 1; } 2020/11/22 北京化 大学信息学院 数据结构 25
void Crt_Bin. Tree ( ifstream& in, Bin. Tree. Node *& T ) { Tree. Data x; if ( !in. eof ( ) ) { in >> x; //读入根结点的值 if ( x != Ref. Value ) { T = new Bin. Tree. Node; //建立根结点 if ( T == NULL ) { cerr << “存储分配错!” << endl; exit (1); } T->data = x; Crt_Bin. Tree ( in, T->left. Child ); Crt_Bin. Tree ( in, T->right. Child ); } else T = NULL; //封闭叶结点 } } 2020/11/22 北京化 大学信息学院 数据结构 31
删除二叉树的递归算法 void destroy ( Bin. Tree. Node *T ) { if ( T != NULL ) { destroy ( T->left. Child ); destroy ( T->right. Child ); delete T; } } 2020/11/22 北京化 大学信息学院 数据结构 32
计算二叉树结点个数的递归算法 int Count ( Bin. Tree. Node *T ) { if ( T == NULL ) return 0; else return 1 + Count ( T->left. Child ) + Count ( T->right. Child ); } 2020/11/22 北京化 大学信息学院 数据结构 33
求二叉树高度的递归算法 int Height ( Bin. Tree. Node * T ) { if ( T == NULL ) return -1; else { int m = Height ( T->left. Child ); int n = Height ( T->right. Child ) ); return (m > n) ? m+1 : n+1; } 2020/11/22 北京化 大学信息学院 数据结构 34
前序遍历的非递归算法 int Pre. Traverse(Bi. Tree T) { if (T==NULL) return 0; Stack S; Init. Stack(S); Push(S, T); while (!Empty(S)) { T = Top(S); Pop(S); Visit(T); if (T->rchild!=NULL) Push(S, T->rchild); if (T->lchild!=NULL) Push(S, T->lchild); } Destroy. Stack(S); return 1; } 2020/11/22 38 北京化 大学信息学院 数据结构
中序遍历的非递归算法 int In. Traverse(Bi. Tree T) { Stack S; Init. Stack(S); while(T!==NULL || !Empty(S)) { while(T!=NULL) { Push(S, T); T = T->lchild; } T = Top(S); Pop(S); Visit(T); T = T->rchild; } Destroy. Stack(S); return 1; } 2020/11/22 北京化 大学信息学院 数据结构 39
前序序列 { ABHFDECKG } 中序序列 { HBDFAEKCG } A HBDF EKCG A D H DF B D DF B E F H EKCG A A EKCG F H B EKCG B H B A A KCG E C F H D K G
树与森林 树的存储表示 data 双亲表示 A B E C F D G 2020/11/22 parent 0 1 2 3 4 5 6 data A B C D E F G parent -1 0 0 0 1 1 3 北京化 大学信息学院 数据结构 42
用双亲表示实现的树定义 #define Max. Size //最大结点个数 typedef char Tree. Data; //结点数据 typedef struct { //树结点定义 Tree. Data data; //结点数据域 int parent; //结点双亲域 } Tree. Node; typedef Tree. Node Tree[Max. Size]; //树 2020/11/22 北京化 大学信息学院 数据结构 43
等数量的链域表示法 data child 1 child 2 child 3 A A B E C F childd D G B E 2020/11/22 C F 北京化 大学信息学院 数据结构 44 D G
左子女-右兄弟表示法 data first. Child next. Sibling A A B E C F B D G E C D F G 2020/11/22 北京化 大学信息学院 数据结构 45
用左子女-右兄弟表示实现的树定义 typedef char Tree. Data; typedef struct node { Tree. Data data; struct node *first. Child, *next. Sibling; } Tree. Node; typedef Tree. Node * Tree; 2020/11/22 北京化 大学信息学院 数据结构 46
int Level. Traverse(Bi. Tree T) { if (T==NULL) return 0; Queue Q; Init. Queue(Q); En. Queue(Q, T); while(!Empty(Q)) { T = Top(Q); De. Queue(Q); Visit(T); if (T->lchild!=NULL) En. Queue(Q, T->lchild); if (T->rchild!=NULL) En. Queue(Q, T->rchild); } Destroy. Queue(Q); return 1; } 2020/11/22 北京化 大学信息学院 数据结构 52
哈夫曼树的定义 const int n = 20; const int m = 2*n -1; typedef struct { float weight; int parent, left. Child, right. Child; } HTNode; typedef HTNode Huffman. Tree[m]; 2020/11/22 北京化 大学信息学院 数据结构 60
Weight parent left. Child right. Child 7 5 2 4 0 7 -1 -1 -1 1 5 -1 -1 -1 2 2 -1 -1 -1 3 4 -1 -1 -1 5 -1 -1 -1 6 -1 -1 -1 2020/11/22 北京化 大学信息学院 数据结构 61
Weight parent left. Child right. Child p 1 p 2 i 7 5 6 2 4 0 7 -1 -1 -1 1 5 -1 -1 -1 2 2 4 -1 -1 -1 3 4 4 -1 -1 -1 4 6 -1 2 -1 3 -1 5 -1 -1 -1 6 -1 -1 -1 2020/11/22 北京化 大学信息学院 数据结构 62
Weight parent left. Child right. Child 7 11 p 1 6 5 2 4 p 2 i 0 7 -1 -1 -1 1 5 5 -1 -1 -1 2 2 4 -1 -1 3 4 4 -1 -1 4 6 5 -1 2 3 5 11 -1 1 -1 4 -1 -1 6 2020/11/22 北京化 大学信息学院 数据结构 63
Weight parent left. Child right. Child p 1 18 7 11 6 5 2 4 p 2 i 0 7 6 -1 -1 -1 1 5 5 -1 -1 2 2 4 -1 -1 3 4 4 -1 -1 4 6 5 2 3 5 11 6 -1 1 4 6 18 -1 0 -1 5 -1 2020/11/22 北京化 大学信息学院 数据结构 64
建立哈夫曼树的算法 void Create. Huffman. Tree ( Huffman. Tree T, float fr[ ] ) { for ( int i = 0; i < n; i++ ) T[i]. weight = fr[i]; for ( i = 0; i < m; i++ ) { T[i]. parent = -1; T[i]. left. Child = -1; T[i]. right. Child = -1; } for ( i = n; i < m; i++ ) { 2020/11/22 北京化 大学信息学院 数据结构 65
int min 1 = min 2 = Max. Num; int pos 1, pos 2; for ( int j = 0; j < i; j++ ) //每次都要搜索一遍 if ( T[j]. parent == -1 ) if ( T[j]. weight < min 1 ) { pos 2 = pos 1; min 2 = min 1; pos 1 = j; min 1 = T[j]. weight; }//总是让 1指向最小的,让 2记录前面的,下面那个只有满足比 1小比 2大才进入 else if ( T[j]. weight < min 2 ) { pos 2 = j; min 2 = T[j]. weight; } T[i]. left. Child = pos 1; T[i]. right. Child = pos 2; 2020/11/22 北京化 大学信息学院 数据结构 66
T[i]. weight = T[pos 1]. weight + T[pos 2]. weight; T[pos 1]. parent = T[pos 2]. parent = i; } } 最佳判定树 考试成绩分布表 2020/11/22 北京化 大学信息学院 数据结构 67
参考答案如下: a: 35, b: 9, c: 19, d: 27, e: 81, f: 14, g: 21, h: 12, i: 25, j: 5, k: 11, l: 8 267 110 157 50 60 25 i/25 h/12 d/27 13 j/5 2020/11/22 e/81 33 c/19 76 41 a/35 f/14 g/21 l/8 北京化 大学信息学院 数据结构 20 b/9 77 k/11
参考答案如下: a: 35, b: 9, c: 19, d: 27, e: 81, f: 14, g: 21, h: 12, i: 25, j: 5, k: 11, l: 8 267 110 157 50 60 25 i/25 h/12 d/27 13 j/5 2020/11/22 e/81 33 c/19 76 41 a/35 f/14 g/21 l/8 北京化 大学信息学院 数据结构 20 b/9 78 k/11
- Left left right right turn around go go go
- Left left right right go go go
- 전위 순회
- Right product right place right time right price
- Family time
- Abcei
- The right man on the right place at the right time
- Downstage and upstage
- Turn right at the supermarket
- Muscle energy technique
- Left factoring algorithm
- Put your left foot in
- You can't turn left
- Right sided vs left sided murmurs
- Answer. go straight turn left turn right
- Rational sequential and analytical strategizing
- Left right story game for summer
- Tubular esophagus
- Up stage down stage
- Advantage of proscenium stage
- Blocking stage
- What is clockwise and anticlockwise
- Right wing meaning
- Radius size trend
- Gang process chart example
- Masonic firing glasses
- Pleura parietal visceral
- Look to the right look to the left
- Quantitative data graphs
- Left and right gastric artery anastomosis
- The word drama comes from
- Convert left linear to right linear grammar
- Right sided vs left sided murmurs
- Right sided vs left sided murmurs
- Plane that divides body into right and left sides
- Inferior border of the heart
- Middle ear ossicles
- Left right patterning
- Put your right hand in the air
- Angle former excavator
- Annulus of vieussens
- Walk straight and turn (1 نقطة) left right around
- Fleming's left hand rule
- Peripheral part of diaphragm
- Right and left aortic sinus
- Right vs left parietal lobe
- Right hand in the air left hand in the air
- Right and left femur
- The left and right hand of god
- Lower border of heart
- Left and right in french
- Pormte
- Qqqq swipe left or right to delete
- James q wilson right realism
- Political left right scale
- Ground plans for theatre
- Transformations of functions
- Qqqq swipe left or right to delete
- What is that
- Dr nienkemper
- Pulmonary ventilation consists of two cyclic phases
- Text that is aligned on the left and jagged on the right
- Right linear grammar
- Privacy is the right to be left alone when you want to be
- No child left behind act special education
- No child left behind act special education
- Last child left in the woods
- Meaning of child right
- Essay top left corner
- Supply curve shift to right
- Moon phases of the sun is on the right
- Propeller torque reaction
- Indicators of past perfect
- Grades of varicocele
- Left sacroanterior position
- Headline story example
- Reference points driving
- Recursive descent parser
- Posterior mediastinum
- How did daisy react after gatsby left for military service
- Right riemman sum