1、代码
2、运行结果
3、分析
1、代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct _Node 5 { 6 int value; 7 struct _Node * pLeft; 8 struct _Node * pRight; 9 } Node; 10 11 Node * getNewNode(int iValue) 12 { 13 Node * p = (Node *)malloc(sizeof(Node)); 14 if(NULL != p) 15 { 16 p->value = iValue; 17 p->pLeft = NULL; 18 p->pRight = NULL; 19 } 20 21 return p; 22 } 23 24 void deleteNode(Node * p) 25 { 26 if(NULL != p) 27 { 28 free(p); 29 } 30 } 31 32 int addElm(Node * p, int value) 33 { 34 Node * pX = p; 35 while(pX) 36 { 37 if(value < pX->value) 38 { 39 if(NULL == pX->pLeft) 40 { 41 pX->pLeft = getNewNode(value); 42 printf("add [%2d] to left of [%2d] ", value, pX->value); 43 break; 44 } 45 else 46 { 47 pX = pX->pLeft; 48 } 49 } 50 else 51 { 52 if(NULL == pX->pRight) 53 { 54 pX->pRight = getNewNode(value); 55 printf("add [%2d] to right of [%2d] ", value, pX->value); 56 break; 57 } 58 else 59 { 60 pX = pX->pRight; 61 } 62 } 63 } 64 65 return 0; 66 } 67 68 Node * makeBinaryTree(int iList[], int iNum) 69 { 70 Node * p = getNewNode(iList[0]); 71 int i = 0; 72 for(i = 1; i < iNum; i++) 73 { 74 addElm(p, iList[i]); 75 } 76 printf(" "); 77 78 return p; 79 } 80 81 void destroyBinaryTree(Node * p) 82 { 83 if(NULL == p) 84 { 85 return; 86 } 87 destroyBinaryTree(p->pLeft); 88 destroyBinaryTree(p->pRight); 89 deleteNode(p); 90 } 91 92 int preorderTraversal(Node * p) 93 { 94 if(NULL == p) 95 { 96 return -1; 97 } 98 printf("%2d ", p->value); 99 preorderTraversal(p->pLeft); 100 preorderTraversal(p->pRight); 101 return 0; 102 } 103 104 int postorderTraversal(Node * p) 105 { 106 if(NULL == p) 107 { 108 return -1; 109 } 110 postorderTraversal(p->pLeft); 111 postorderTraversal(p->pRight); 112 printf("%2d ", p->value); 113 return 0; 114 } 115 116 int inorderTraversal(Node * p) 117 { 118 if(NULL == p) 119 { 120 return -1; 121 } 122 inorderTraversal(p->pLeft); 123 printf("%2d ", p->value); 124 inorderTraversal(p->pRight); 125 return 0; 126 } 127 128 void preTrvl(Node * p) 129 { 130 printf("pre : "); 131 preorderTraversal(p); 132 printf(" "); 133 } 134 135 void postTrvl(Node * p) 136 { 137 printf("post : "); 138 postorderTraversal(p); 139 printf(" "); 140 } 141 142 void inTrvl(Node * p) 143 { 144 printf("in : "); 145 inorderTraversal(p); 146 printf(" "); 147 } 148 149 void printList(int iList[], int iNum) 150 { 151 for(int i = 0; i < iNum; i++) 152 { 153 printf("%d ", iList[i]); 154 } 155 printf(" "); 156 } 157 158 int main() 159 { 160 int iList[15] = {6, 9, 8, 3, 5, 4, 7, 2, 12, 1, 10, 11}; 161 int iNum = 12; 162 163 printList(iList, iNum); 164 Node * p = makeBinaryTree(iList, iNum); 165 preTrvl(p); 166 postTrvl(p); 167 inTrvl(p); 168 destroyBinaryTree(p); 169 }
2、运行结果
1 $ ./binaryTree 2 6 9 8 3 5 4 7 2 12 1 10 11 3 add [ 9] to right of [ 6] 4 add [ 8] to left of [ 9] 5 add [ 3] to left of [ 6] 6 add [ 5] to right of [ 3] 7 add [ 4] to left of [ 5] 8 add [ 7] to left of [ 8] 9 add [ 2] to left of [ 3] 10 add [12] to right of [ 9] 11 add [ 1] to left of [ 2] 12 add [10] to left of [12] 13 add [11] to right of [10] 14 15 pre : 6 3 2 1 5 4 9 8 7 12 10 11 16 post : 1 2 4 5 3 7 8 11 10 12 9 6 17 in : 1 2 3 4 5 6 7 8 9 10 11 12
3、分析
从运行结果的第三行开始,就是开始进行数据插入的地方,下面对运行结果中,每一行插入动作后二叉树的情况进行画图描述。
第03行:add [ 9] to right of [ 6] 第04行:add [ 8] to left of [ 9]
第05行:add [ 3] to left of [ 6] 第06行:add [ 5] to right of [ 3]
第07行:add [ 4] to left of [ 5] 第08行:add [ 7] to left of [ 8]
第09行:add [ 2] to left of [ 3] 第10行:add [12] to right of [ 9]
第11行:add [ 1] to left of [ 2]
第12行:add [10] to left of [12]
第13行:add [11] to right of [10]