二叉树节点的插入
全部代码
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4
5 typedef struct node
6 {
7 int nValue;
8 struct node *pLeft;
9 struct node *pRight;
10 }BiTree;
11
12 typedef struct node2
13 {
14 BiTree *nValue;
15 struct node2 *pNext;
16 }MyQueue;
17
18 typedef struct node3
19 {
20 int nCount;
21 MyQueue *pHead;
22 MyQueue *pTail;
23 }Queue;
24
25 void q_Init(Queue **ppQueue)
26 {
27 assert(ppQueue!=NULL);
28
29 *ppQueue = (Queue*)malloc(sizeof(Queue));
30 if(NULL == *ppQueue)
31 {
32 printf("*ppQueue空间分配失败!\n");
33 exit(-1);
34 }
35 (*ppQueue)->nCount = 0;
36 (*ppQueue)->pHead = NULL;
37 (*ppQueue)->pTail = NULL;
38 }
39
40 void q_Push(Queue *pQueue,BiTree* nNum)
41 {
42 MyQueue *pTemp = NULL;
43
44 assert(pQueue!=NULL);
45
46 pTemp = (MyQueue*)malloc(sizeof(MyQueue));
47 if(NULL == pTemp)
48 {
49 printf("pTemp空间分配失败!\n");
50 exit(-1);
51 }
52 pTemp->nValue = nNum;
53 pTemp->pNext = NULL;
54
55 //队列空
56 if(pQueue->pHead == NULL)
57 {
58 pQueue->pHead = pTemp;
59 }
60 else
61 {
62 pQueue->pTail->pNext = pTemp;
63 }
64 pQueue->pTail = pTemp;
65
66 //更新队列元素
67 ++pQueue->nCount;
68 }
69
70 BiTree *q_Pop(Queue *pQueue)
71 {
72 BiTree* nNum= NULL;
73 MyQueue *pDel = NULL;
74
75 assert(pQueue!=NULL && pQueue->pHead!=NULL);
76
77 pDel = pQueue->pHead ;
78 nNum = pDel->nValue;
79
80 pQueue->pHead = pQueue->pHead->pNext;
81 free(pDel);
82 pDel = NULL;
83
84 --pQueue->nCount;
85
86 //队列在删除后为空 尾置空
87 if(pQueue->nCount == 0)
88 {
89 pQueue->pTail =NULL;
90 }
91
92 return nNum;
93 }
94
95 int q_IsEmpty(Queue *pQueue)
96 {
97 assert(pQueue!=NULL);
98
99 return pQueue->nCount == 0 ?1:0;
100 }
101
102 BiTree *CreateBiTree()
103 {
104 BiTree *pRoot = NULL;
105
106 //根
107 pRoot = (BiTree*)malloc(sizeof(BiTree));
108 pRoot->nValue = 1;
109
110 //根的左
111 pRoot->pLeft = (BiTree*)malloc(sizeof(BiTree));
112 pRoot->pLeft->nValue = 2;
113
114 //左的左
115 pRoot->pLeft->pLeft = (BiTree*)malloc(sizeof(BiTree));
116 pRoot->pLeft->pLeft->nValue = 4;
117 pRoot->pLeft->pLeft->pLeft = NULL;
118 pRoot->pLeft->pLeft->pRight = NULL;
119
120 //左的右
121 pRoot->pLeft->pRight= (BiTree*)malloc(sizeof(BiTree));
122 pRoot->pLeft->pRight->nValue = 5;
123 pRoot->pLeft->pRight->pLeft = NULL;
124 pRoot->pLeft->pRight->pRight = NULL;
125
126 //根的右
127 pRoot->pRight = (BiTree*)malloc(sizeof(BiTree));
128 pRoot->pRight->nValue = 3;
129
130 //右的左
131 pRoot->pRight->pLeft = (BiTree*)malloc(sizeof(BiTree));
132 pRoot->pRight->pLeft->nValue = 6;
133 pRoot->pRight->pLeft->pLeft = NULL;
134 pRoot->pRight->pLeft->pRight = NULL;
135
136 //右的右
137 pRoot->pRight->pRight = NULL;
138
139 return pRoot;
140 }
141
142 BiTree *LevelSearch(BiTree *pRoot,int nNum)
143 {
144 Queue *pQueue = NULL;
145 BiTree *pTemp = NULL;
146
147 assert(pRoot!=NULL);
148
149 //初始化辅助队列
150 q_Init(&pQueue);
151
152 //根 入队
153 q_Push(pQueue,pRoot);
154
155 //队列为空 遍历结束
156 while(!q_IsEmpty(pQueue))
157 {
158 pTemp = q_Pop(pQueue);
159 //查找到该结点
160 if(pTemp->nValue == nNum)
161 {
162 return pTemp;
163 }
164
165 if(pTemp->pLeft != NULL)
166 {
167 q_Push(pQueue,pTemp->pLeft);
168 }
169 if(pTemp->pRight != NULL)
170 {
171 q_Push(pQueue,pTemp->pRight);
172 }
173 }
174
175 //查找失败
176 return NULL;
177 }
178
179 enum DIRECTION{LEFT,RIGHT};
180
181 void InsertNode(BiTree *pRoot, int nValue, int nNum, int nDirection)
182 {
183 BiTree *pNode = NULL;
184 BiTree *pTemp = NULL;
185
186 assert(pRoot!=NULL);
187
188 //查找
189 pNode = LevelSearch(pRoot,nValue);
190
191 if(NULL == pNode)
192 {
193 return;
194 }
195
196 pTemp = (BiTree*)malloc(sizeof(BiTree));
197 if(NULL == pTemp)
198 {
199 printf("pTemp空间分配失败!\n");
200 exit(-1);
201 }
202 pTemp->nValue = nNum;
203 pTemp->pLeft = NULL;
204 pTemp->pRight = NULL;
205
206 if(LEFT == nDirection)
207 {
208 pNode->pLeft = pTemp;
209 }
210 else
211 {
212 pNode->pRight = pTemp;
213 }
214 }
215
216 void Traversal(BiTree *pRoot)
217 {
218 if(NULL == pRoot)
219 {
220 return;
221 }
222
223 printf("%d\n",pRoot->nValue);
224 Traversal(pRoot->pLeft);
225 Traversal(pRoot->pRight);
226 }
227
228 int main(void)
229 {
230 BiTree *pRoot = NULL;
231
232 pRoot = CreateBiTree();
233 InsertNode(pRoot,2,10000,0);
234 Traversal(pRoot);
235
236 return 0;
237 }