left Child data parent right Child parent data
- Slides: 118
二叉树的链表表示 left. Child data parent right. Child parent data right. Child left. Child 三叉链表 11/30/2020 北京化 大学信息学院 数据结构 14
root A B C D E F data parent left. Child right. Child 0 1 2 3 4 5 A B C D E F -1 0 1 1 3 3 1 2 -1 4 -1 -1 三叉链表的静态结构 11/30/2020 北京化 大学信息学院 数据结构 16 -1 3 -1 5 -1 -1
二叉树的定义 typedef char Tree. Data; //树结点数据类型 typedef struct node { //树结点定义 Tree. Data data; //结点数据域 struct node * left. Child, * rightchild; //子女指针域 } Bin. Tree. Node; typedef Bin. Tree. Node * Bin. Tree; //树定义,代表树的根指针 11/30/2020 北京化 大学信息学院 数据结构 17
二叉树递归的中序遍历算法 void In. Order ( Bin. Tree. Node *T ) { if ( T != NULL ) { In. Order ( T->left. Child ); cout << T->data; In. Order ( T->right. Child ); } } 11/30/2020 北京化 大学信息学院 数据结构 20
二叉树递归的前序遍历算法 void Pre. Order ( Bin. Tree. Node *T ) { if ( T != NULL ) { cout << T->data; Pre. Order ( T->left. Child ); Pre. Order ( T->right. Child ); } } 11/30/2020 北京化 大学信息学院 数据结构 22
二叉树递归的后序遍历算法 void Post. Order ( Bin. Tree. Node * T ) { if ( T != NULL ) { Post. Order ( T->left. Child ); Post. Order ( T->right. Child ); cout << T->data; } } 11/30/2020 北京化 大学信息学院 数据结构 24
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; 11/30/2020 北京化 大学信息学院 数据结构 27
Crt_Bin. Tree ( in, T->left. Child ); Crt_Bin. Tree ( in, T->right. Child ); } else T = NULL; //封闭叶结点 } } 建立二叉树的主程序 void create. Bin. Tree ( filename, Bin. Tree. Node *& Tree ) { //建立一棵二叉树Tree。fin是输入流对象 11/30/2020 北京化 大学信息学院 数据结构 28
Tree. Data item; ifstream fin (filename, ios: : in | ios: : nocreate ); //打开文件 if ( !fin ) { cerr << “文件未发现!” << endl; exit (1); } Crt_Bin. Tree ( ifstream& fin, Tree ); fin. close ( ); //关闭文件 } 11/30/2020 北京化 大学信息学院 数据结构 29
删除二叉树的递归算法 void destroy ( Bin. Tree. Node *T ) { if ( T != NULL ) { destroy ( T->left. Child ); destroy ( T->right. Child ); delete T; } } 11/30/2020 北京化 大学信息学院 数据结构 30
计算二叉树结点个数的递归算法 int Count ( Bin. Tree. Node *T ) { if ( T == NULL ) return 0; else return 1 + Count ( T->left. Child ) + Count ( T->right. Child ); } 11/30/2020 北京化 大学信息学院 数据结构 31
求二叉树高度的递归算法 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; } 11/30/2020 北京化 大学信息学院 数据结构 32
计算二叉树含 x 结点所在层次 k void level ( Bin. Tree T, Tree. Data x, int h, int &k ) { if ( T == NULL ) k = -1; else if ( T->data == x ) k = h; else { level ( T->left. Child, x, h+1, k); if ( k == -1 ) level ( T->right. Child, x, h+1, k); } } 主程序调用 level ( T, x, 0, k ); 11/30/2020 北京化 大学信息学院 数据结构 33
将一棵以二叉链表存储的二叉树按 顺序方式存储到一维数组中 void Link 2 Array ( Bin. Tree. Node * T, Tree. Data C[ ], int k ) { if ( T != NULL ) { C[k] = T->data; Link 2 Array ( T->left. Child, C, 2*k+1 ); Link 2 Array ( T->right. Child, C, 2*k+2 ); } } 主程序调用方式 create ( T, C, 0 ); 11/30/2020 北京化 大学信息学院 数据结构 34
ptr = new Bin. Tree. Node; //建立根结点 ptr->data = T[i]; Array 2 Link (T, n, 2*i+1, ptr->left. Child); Array 2 Link (T, n, 2*i+2, ptr->right. Child); } } 11/30/2020 北京化 大学信息学院 数据结构 36
void Pre. Order( Bin. Tree T ) { stack S; Init. Stack(&S); //递归 作栈 Bin. Tree. Node * p = T; Push (&S, NULL); while ( p != NULL ) { cout << p->data << endl; if ( p->right. Child != NULL ) Push ( &S, p->right. Child ); if ( p->left. Child != NULL ) p = p->left. Child; //进左子树 else Pop( &S, p ); //左子树空, 进右子树 } } 11/30/2020 北京化 大学信息学院 数据结构 38
void In. Order ( Bin. Tree T ) { stack S; Init. Stack( &S ); //递归 作栈 Bin. Tree. Node *p = T; //初始化 do { while ( p != NULL ) { Push(&S, p); p = p->left. Child; } if ( !Stack. Empty(&S) ) { //栈非空 Pop(&S, p); //退栈 cout << p->data << endl; //访问根 p = p->right. Child; //向右链走 } } while ( p != NULL || !Stack. Empty(&S) ); } 11/30/2020 北京化 大学信息学院 数据结构 40
a c b d b. R a. L e a. L b. L a. R 11/30/2020 e. L c. L a. R d. L b. R a. L e. R c. L a. R 北京化 大学信息学院 数据结构 c. L a. R 42 d. R b. R a. L c. R a. R
利用栈的后序遍历的非递归算法 void Post. Order ( Bin. Tree T ) { stack S; Init. Stack(&S); Stack. Node w; Bin. Tree. Node * p = T; do { while ( p != NULL ) { //向左子树走 w. ptr = p; w. tag = L; Push (&S, w); p = p->left. Child; } int continue = 1; //继续循环标记 11/30/2020 北京化 大学信息学院 数据结构 43
while ( continue && !Stack. Empty(&S) ) { Pop (&S, w); p = w. ptr; switch ( w. tag ) { //判断栈顶tag标记 case L : w. tag = R; Push (&S, w); continue = 0; p = p->right. Child; break; case R : cout << p->data << endl; } } } while ( p != NULL || !Stack. Empty(&S) ); cout << endl; } 11/30/2020 北京化 大学信息学院 数据结构 44
练习: 阅读以下二叉树操作算法 void unknown ( Bin. Tree. Node * T ) { Bin. Tree. Node *p = T, *temp; if ( p != NULL ) { temp = p->left. Child; p->left. Child = p->right. Child; p->right. Child = temp; unknown ( p->left. Child ); unknown ( p->right. Child ); } } 11/30/2020 北京化 大学信息学院 数据结构 45
void unknown ( Bin. Tree. Node * T ) { Bin. Tree. Node *p = T, *temp; while ( p != NULL ) { temp = p->left. Child; p->left. Child = p->right. Child; p->right. Child = temp; unknown ( p->left. Child ); p = p->right. Child; } } 11/30/2020 北京化 大学信息学院 数据结构 47
使用栈消去递归算法中的两个递归语句: void unknown ( Bin. Tree. Node * T ) { Bin. Tree. Node *p, *temp; stack S; Init. Empty (&S); if ( T != NULL ) { push(&S, T); while ( ! Stack. Empty(&S) ) { Pop(&S, p); //栈中退出一个结点 temp = p->left. Child; //交换子女 p->left. Child = p->right. Child; p->right. Child = temp; 11/30/2020 北京化 大学信息学院 数据结构 48
if ( p->right. Child != NULL ) push (&S, p->right. Child ); if ( p->left. Child != NULL ) push (&S, p->left. Child ); } } } 11/30/2020 北京化 大学信息学院 数据结构 49
前序序列 { ABHFDECKG } A B H A EKCG DF B D A B E C F H D EKCG F H K A G B E F H D KCG
最小堆的定义 #define Max. Heap. Size 100; typedef int Heap. Data; typedef struct { Heap. Data data[Max. Heap. Size]; //存放最小堆元素的数组 int CSize; //最小堆当前元素个数 } Min. Heap; 11/30/2020 北京化 大学信息学院 数据结构 58
void Init. Heap ( Min. Heap *H ) { H->CSize = 0; } //堆初始化,置空堆 int Heap. Empty ( Min. Heap *H ) { return H->CSize == 0; } //判堆空否 int Heap. Full ( Min. Heap *H ) { return H->CSize == Max. Heap. Size; } //判堆满否 11/30/2020 北京化 大学信息学院 数据结构 59
53 i 17 09 53 65 87 45 78 23 09 i 17 45 78 23 CPos = i = 1 11/30/2020 北京化 大学信息学院 数据结构 65 61 87
i 53 09 17 09 i 5309 65 87 45 78 23 17 45 78 23 CPos = i = 0 11/30/2020 北京化 大学信息学院 数据结构 65 62 87
09 09 i 53 17 17 65 45 78 17 87 i 23 45 78 5 3 23 11/30/2020 北京化 大学信息学院 数据结构 65 63 87
最小堆的向下调整算法 void Filter. Down ( Min. Heap *H, int start, int End. Of. Heap ) { int i = start, j = 2*i+1; // j 是 i 的左子女 Heap. Data temp = H->data[i]; while ( j <= End. Of. Heap ) { if ( j < End. Of. Heap && //两子女中选小者 H->data[j] > H->data[j+1] ) j++; if ( temp <= H->data[j] ) break; else { H->data[i] = H->data[j]; i = j; j = 2*j+1; } //向下滑动 } 11/30/2020 北京化 大学信息学院 数据结构 65
i i 1117 17 5 3 65 45 78 11 87 17 5 3 23 11/30/2020 09 j 45 78 23 北京化 大学信息学院 数据结构 65 67 87
最小堆的向上调整算法 void Filter. Up ( Min. Heap *H, int start ) { //从 start 开始, 向上直到 0, 调整堆 int j = start, i = (j-1)/2; // i 是 j 的双亲 Heap. Data temp = H->data[j]; while ( j > 0 ) { if ( H->data[i] <= temp ) break; else { H->data[j] = H->data[i]; j = i; i = (i -1)/2; } } H->data[j] = temp; } 11/30/2020 北京化 大学信息学院 数据结构 69
i 向下调整 j 17 23 65 45 78 5 3 17 i 31 j 87 31 17 23 45 78 5 3 11/30/2020 北京化 大学信息学院 数据结构 65 71 87
向下调整 17 17 i j 31 23 23 65 45 78 5 3 23 i 87 31 45 78 5 3 11/30/2020 北京化 大学信息学院 数据结构 65 72 87
最小堆的删除算法 int Remove. Min ( Min. Heap *H, Heap. Data &x ) { if ( ! H->CSize ) { cout << “ 堆已空 " << endl; return 0; } x = H->data[0]; //最小元素出队列 H->data[0] = H->data[H->CSize-1]; H->CSize--; //用最后元素填补 Filter. Down ( &H, 0, H->CSize-1 ); //调整 return 1; } 11/30/2020 北京化 大学信息学院 数据结构 73
用双亲表示实现的树定义 #define Max. Size //最大结点个数 typedef char Tree. Data; //结点数据 typedef struct { Tree. Data data; int parent; } Tree. Node; //树结点定义 //结点数据域 //结点双亲域 typedef Tree. Node Tree[Max. Size]; //树 11/30/2020 北京化 大学信息学院 数据结构 75
用左子女-右兄弟表示实现的树定义 typedef char Tree. Data; typedef struct node { Tree. Data data; struct node *first. Child, *next. Sibling; } Tree. Node; typedef Tree. Node * Tree; 11/30/2020 北京化 大学信息学院 数据结构 78
Tree. Node * Find. Parent (Tree T, Tree. Node *p) { //在根为 T 的树中找 *p 的双亲 Tree. Node *q = T->first. Child, *s; while ( q != NULL && q != p ) { //循根的长子的兄弟链, 递归在子树中搜索 if (( s = Find. Parent ( q, p ) ) != NULL ) return s; q = q->next. Sibling; } if ( q != NULL && q == p ) return T; else return NULL; //未找到双亲 } 11/30/2020 北京化 大学信息学院 数据结构 80
Tree. Node * First. Child ( Tree T, Tree. Node *p ) { //在树T中找当前结点*p的第一个子女 if ( p != NULL ) return p->first. Child; else return NULL; } Tree. Node * Next. Sibling (Tree T, Tree. Node *p) { //在树T中找结点*p的兄弟 if ( p != NULL ) return p->next. Sibling; else return NULL; } 11/30/2020 北京化 大学信息学院 数据结构 81
霍夫曼树的定义 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]; 11/30/2020 北京化 大学信息学院 数据结构 98
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 11/30/2020 北京化 大学信息学院 数据结构 99
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 11/30/2020 北京化 大学信息学院 数据结构 100
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 11/30/2020 北京化 大学信息学院 数据结构 101
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 11/30/2020 北京化 大学信息学院 数据结构 102
建立霍夫曼树的算法 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++ ) { 11/30/2020 北京化 大学信息学院 数据结构 103
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; } 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; 11/30/2020 北京化 大学信息学院 数据结构 104
T[i]. weight = T[pos 1]. weight + T[pos 2]. weight; T[pos 1]. parent = T[pos 2]. parent = i; } } 最佳判定树 考试成绩分布表 11/30/2020 北京化 大学信息学院 数据结构 105
参考答案如下: 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 11/30/2020 e/81 33 c/19 76 41 a/35 f/14 g/21 l/8 北京化 大学信息学院 数据结构 20 b/9 115 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 11/30/2020 e/81 33 c/19 76 41 a/35 f/14 g/21 l/8 北京化 大学信息学院 数据结构 20 b/9 116 k/11
- Go left left right right
- Left left right right go go go
- Right product right place right time right price
- Family time
- 승자트리
- Left child right sibling tree
- The right man on the right place at the right time
- Downstage and upstage
- Go straight ahead or turn right
- Muscle energy technique si joint
- The left recursion produces
- You put your right foot in you put your right foot out
- Write a short article about kimi raikkonen
- Tulika jain md
- Answer. go straight turn left turn right
- Spatres
- Left right story game for summer
- Short gastric arteries
- Down stage definition
- Arena stage directions
- Blocking stage
- Which day is anti clockwise left or right
- Wings of politics
- Trend for ionic radius
- Gang process chart example
- Masonic fire point left right
- Difference between right and left bronchus
- Look down to the right
- Displaying quantitative data with graphs
- Celiac trunk branches
- What is drama
- A left linear grammar is always
- Holosystolic murmur
- Apex beat
- Plane that divides body into right and left sides
- Inferior border of heart
- Middle ear ossicles
- Left right patterning
- Put your right hand in the air
- Hatchet excavator
- Right and left aortic sinus
- Go straight and turn left
- Torque of dc motor formula
- Diaphram openings
- Right and left aortic sinus
- Parietal lobe left vs right
- Right hand in the air left hand in the air
- Compact bone
- The left and right hand of god
- Oblique pericardial sinus
- Left and right in french
- Marisol left and right
- Qqqq swipe left or right to delete
- Left and right realism
- Political spectrum scale
- Ground plan theatre
- Function transformations
- Qqqq swipe left or right to delete
- Left add right subtract
- Dr nienkemper
- Difference between right and left bronchus
- Text that is aligned on the left and jagged on the right
- Closure properties of regular languages
- 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
- Kathryn e barnard nursing theory
- Cpp triangle of explanation
- Kathryn barnard child interaction theory
- Sample letter of someone living with you
- Palin pci training
- Meaning of child right
- Mla left corner
- Shift in supply curve
- Moon phases when the sun is on the left
- P factor
- Mary give me tony's address before she left
- Left sided varicocele
- Left sacroanterior position
- Drop line headline
- What are the vehicle reference points used for
- Recursive descent parser
- Superior mediastinum contents
- Great gatsby activities
- Integral
- Tribeculated
- Boundaries of epiploic foramen
- 4 left turning tendencies
- Syntax analyzer produces
- Supply curve shift right
- Left lower extremity
- Smartness and good order
- Blood supply of colon
- Skewed left
- Left lateral recumbent
- Anterior view of right hand
- Are zeros to the left of the decimal significant
- Left driving countries
- Hypothesis testing
- Se + verb spanish
- Why rizal left london for paris in march 1899?
- Bronchial branching
- Which of the following does not have left recursions?
- Mla top left corner
- Selective set operation
- Barium meal position
- In the position shown collar b moves to the left
- Unacceptable blood smear
- Portosystemic anastomosis
- Dorsal supine position
- Operating room positions for patient
- Lung fissures surface anatomy
- Root of left lung
- Lymphatic drainage of pleura
- Sitting duck heart persistent truncus arteriosus
- Foramen of winslow
- Types of restraints
- Left sided hemiplegia