当前位置:   article > 正文

孩子兄弟表示法(二叉链表树)_孩子兄弟二叉链表

孩子兄弟二叉链表

考虑下面这森林:


如果用孩子兄弟表示法可以表示为:


顾名思义,孩子兄弟表示法的每个节点有两个指针域,一个指向其长子,另一个指向其兄弟.

实现:
  1. /**************************************************
  2. 树的孩子兄弟表示法(二叉链表树)
  3. by Rowandjj
  4. 2014/5/25
  5. **************************************************/
  6. #include<iostream>
  7. using namespace std;
  8. typedef char ElemType;
  9. //--------二叉链表(孩子-兄弟)存储表示-------
  10. typedef struct _TREENODE_
  11. {
  12. struct _TREENODE_ *pFirstChild;
  13. struct _TREENODE_ *pNextSibling;
  14. ElemType data;
  15. }TreeNode,*pTreeNode,**ppTreeNode;
  16. //----------辅助队列-------------------
  17. typedef struct _QUEUENODE_
  18. {
  19. struct _QUEUENODE_ *pNext;
  20. pTreeNode data;
  21. }QueueNode,*pQueueNode;
  22. typedef struct _QUEUE_
  23. {
  24. pQueueNode pHead;
  25. pQueueNode pTail;
  26. int nodeCount;
  27. }Queue,*pQueue;
  28. //-----------队列操作定义-----------------------
  29. void InitQueue(pQueue pQueueTemp);
  30. void Enqueue(pQueue pQueueTemp,pTreeNode pTreeNodeTemp);
  31. void Dequeue(pQueue pQueueTemp,ppTreeNode ppTreeNodeTemp);
  32. bool IsQueueEmpty(Queue QueueTemp);
  33. void DestroyQueue(pQueue pQueueTemp);
  34. //------------二叉树操作定义--------------------
  35. void CreateTree(ppTreeNode ppTreeNodeTemp);
  36. void DestroyTree(ppTreeNode ppTreeNodeTemp);
  37. int GetDepth(pTreeNode pTreeNodeTemp);
  38. void PreTravel(pTreeNode pTreeNodeTemp);
  39. void PostTravel(pTreeNode pTreeNodeTemp);
  40. void MidTravel(pTreeNode pTreeNodeTemp);
  41. void LevelTravel(pTreeNode pTreeNodeTemp);
  42. pTreeNode Point(pTreeNode pTreeNodeTemp,ElemType e);
  43. ElemType GetParent(pTreeNode pTreeNodeTemp,ElemType e);
  44. void InsertTree(ppTreeNode ppTreeNodeTemp,ElemType data,int i,pTreeNode pSub);//将psub插入到ppTreeNodeTemp中的值为data的节点的第i个子树
  45. void DeleteTree(ppTreeNode ppTreeNodeTemp,ElemType data,int i);//删除ppTreeNodeTemp中的值为data的节点的第i个子树
  46. //---------队列操作实现---------------
  47. void InitQueue(pQueue pQueueTemp)
  48. {
  49. pQueueTemp->pHead = pQueueTemp->pTail = (pQueueNode)malloc(sizeof(QueueNode));
  50. if(pQueueTemp->pHead == NULL)
  51. {
  52. return;
  53. }
  54. pQueueTemp->nodeCount = 0;
  55. pQueueTemp->pHead->pNext = NULL;
  56. }
  57. void Enqueue(pQueue pQueueTemp,pTreeNode pTreeNodeTemp)
  58. {
  59. if(pQueueTemp == NULL)
  60. {
  61. return;
  62. }
  63. pQueueNode pNew = (pQueueNode)malloc(sizeof(QueueNode));
  64. if(pNew == NULL)
  65. {
  66. return;
  67. }
  68. pNew->data = pTreeNodeTemp;
  69. pNew->pNext = NULL;
  70. pQueueTemp->pTail->pNext = pNew;
  71. pQueueTemp->pTail = pNew;
  72. pQueueTemp->nodeCount++;
  73. }
  74. void Dequeue(pQueue pQueueTemp,ppTreeNode ppTreeNodeTemp)
  75. {
  76. if(pQueueTemp == NULL)
  77. {
  78. return;
  79. }
  80. pQueueNode pDel = pQueueTemp->pHead->pNext;
  81. pQueueTemp->pHead->pNext = pDel->pNext;
  82. if(pDel == pQueueTemp->pTail)
  83. {
  84. pQueueTemp->pTail = pQueueTemp->pHead;
  85. }
  86. *ppTreeNodeTemp = pDel->data;
  87. free(pDel);
  88. pQueueTemp->nodeCount--;
  89. }
  90. bool IsQueueEmpty(Queue QueueTemp)
  91. {
  92. return QueueTemp.nodeCount == 0;
  93. }
  94. void DestroyQueue(pQueue pQueueTemp)
  95. {
  96. if(pQueueTemp != NULL)
  97. {
  98. pQueueNode pTravel = pQueueTemp->pHead->pNext;
  99. while(pTravel != NULL)
  100. {
  101. pQueueTemp->pHead->pNext = pTravel->pNext;
  102. free(pTravel);
  103. pTravel = pQueueTemp->pHead->pNext;
  104. }
  105. free(pQueueTemp->pHead);
  106. pQueueTemp->nodeCount = 0;
  107. }
  108. }
  109. //------------二叉树操作实现-------------------------
  110. void CreateTree(ppTreeNode ppTreeNodeTemp)//创建
  111. {
  112. char szBuffer[20];
  113. char a;
  114. cout<<"输入根节点:";
  115. cin>>a;
  116. Queue queue;
  117. InitQueue(&queue);
  118. if(a != '#')
  119. {
  120. *ppTreeNodeTemp = (pTreeNode)malloc(sizeof(TreeNode));
  121. (*ppTreeNodeTemp)->data = a;
  122. (*ppTreeNodeTemp)->pNextSibling = NULL;
  123. Enqueue(&queue,*ppTreeNodeTemp);//入队根节点
  124. pTreeNode pTemp,pTemp1;
  125. while(!IsQueueEmpty(queue))
  126. {
  127. Dequeue(&queue,&pTemp);
  128. cout<<"输入"<<pTemp->data<<"的孩子节点:";
  129. cin>>szBuffer;
  130. if(szBuffer[0] != '#')
  131. {
  132. pTemp->pFirstChild = (pTreeNode)malloc(sizeof(TreeNode));
  133. pTemp->pFirstChild->data = szBuffer[0];
  134. pTemp1 = pTemp->pFirstChild;
  135. for(int i = 1; i < strlen(szBuffer); i++)
  136. {
  137. pTemp1->pNextSibling = (pTreeNode)malloc(sizeof(TreeNode));
  138. Enqueue(&queue,pTemp1);
  139. pTemp1->pNextSibling->data = szBuffer[i];
  140. pTemp1 = pTemp1->pNextSibling;
  141. }
  142. pTemp1->pNextSibling = NULL;
  143. Enqueue(&queue,pTemp1);
  144. }else
  145. {
  146. pTemp->pFirstChild = NULL;
  147. }
  148. }
  149. }else
  150. {
  151. *ppTreeNodeTemp = NULL;
  152. }
  153. }
  154. void DestroyTree(ppTreeNode ppTreeNodeTemp)
  155. {
  156. if(*ppTreeNodeTemp != NULL)
  157. {
  158. if((*ppTreeNodeTemp)->pFirstChild != NULL)
  159. {
  160. DestroyTree(&(*ppTreeNodeTemp)->pFirstChild);
  161. }
  162. if((*ppTreeNodeTemp)->pNextSibling != NULL)
  163. {
  164. DestroyTree(&(*ppTreeNodeTemp)->pNextSibling);
  165. }
  166. free(*ppTreeNodeTemp);
  167. *ppTreeNodeTemp = NULL;
  168. }
  169. }
  170. int GetDepth(pTreeNode pTreeNodeTemp)
  171. {
  172. if(pTreeNodeTemp == NULL)
  173. {
  174. return 0;
  175. }
  176. if(pTreeNodeTemp->pFirstChild == NULL)
  177. {
  178. return 1;
  179. }
  180. int depth,max = 0;
  181. pTreeNode pTemp = pTreeNodeTemp->pFirstChild;
  182. for(;pTemp != NULL; pTemp = pTemp->pNextSibling)
  183. {
  184. depth = GetDepth(pTemp);
  185. if(depth > max)
  186. {
  187. max = depth;
  188. }
  189. }
  190. return max+1;
  191. }
  192. void PreTravel(pTreeNode pTreeNodeTemp)
  193. {
  194. if(pTreeNodeTemp)
  195. {
  196. cout<<pTreeNodeTemp->data<<" ";
  197. PreTravel(pTreeNodeTemp->pFirstChild);
  198. PreTravel(pTreeNodeTemp->pNextSibling);
  199. }
  200. }
  201. void MidTravel(pTreeNode pTreeNodeTemp)
  202. {
  203. if(pTreeNodeTemp)
  204. {
  205. MidTravel(pTreeNodeTemp->pFirstChild);
  206. cout<<pTreeNodeTemp->data<<" ";
  207. MidTravel(pTreeNodeTemp->pNextSibling);
  208. }
  209. }
  210. void PostTravel(pTreeNode pTreeNodeTemp)
  211. {
  212. if(pTreeNodeTemp)
  213. {
  214. PostTravel(pTreeNodeTemp->pFirstChild);
  215. PostTravel(pTreeNodeTemp->pNextSibling);
  216. cout<<pTreeNodeTemp->data<<" ";
  217. }
  218. }
  219. pTreeNode Point(pTreeNode pTreeNodeTemp,ElemType e)
  220. {
  221. Queue queue;
  222. InitQueue(&queue);
  223. if(pTreeNodeTemp != NULL)
  224. {
  225. Enqueue(&queue,pTreeNodeTemp);
  226. pTreeNode pTemp;
  227. while(!IsQueueEmpty(queue))
  228. {
  229. Dequeue(&queue,&pTemp);
  230. if(pTemp->data == e)
  231. {
  232. DestroyQueue(&queue);
  233. return pTemp;
  234. }else
  235. {
  236. pTreeNode pSibling = NULL;
  237. if(pTemp->pFirstChild != NULL)
  238. {
  239. Enqueue(&queue,pTemp->pFirstChild);//入队长子
  240. pSibling = pTemp->pFirstChild->pNextSibling;
  241. }
  242. while(pSibling != NULL)
  243. {
  244. Enqueue(&queue,pSibling);//入队兄弟
  245. pSibling = pSibling->pNextSibling;
  246. }
  247. }
  248. }
  249. }
  250. return NULL;
  251. }
  252. ElemType GetParent(pTreeNode pTreeNodeTemp,ElemType e)
  253. {
  254. Queue queue;
  255. InitQueue(&queue);
  256. if(pTreeNodeTemp != NULL)
  257. {
  258. if(pTreeNodeTemp->data == e)
  259. {
  260. return -1;
  261. }
  262. Enqueue(&queue,pTreeNodeTemp);
  263. pTreeNode pTemp,pTemp1;
  264. while(!IsQueueEmpty(queue))
  265. {
  266. Dequeue(&queue,&pTemp);
  267. if(pTemp->pFirstChild)
  268. {
  269. if(pTemp->pFirstChild->data == e)
  270. {
  271. return pTemp->data;
  272. }
  273. pTemp1 = pTemp;
  274. Enqueue(&queue,pTemp->pFirstChild);//入队长子
  275. pTemp = pTemp->pFirstChild;
  276. while(pTemp->pNextSibling)
  277. {
  278. pTemp = pTemp->pNextSibling;
  279. if(pTemp->data == e)
  280. {
  281. return pTemp1->data;
  282. }
  283. Enqueue(&queue,pTemp);//入队兄弟
  284. }
  285. }
  286. }
  287. }
  288. return -1;
  289. }
  290. void LevelTravel(pTreeNode pTreeNodeTemp)//层次遍历
  291. {
  292. Queue queue;
  293. InitQueue(&queue);
  294. if(pTreeNodeTemp != NULL)
  295. {
  296. cout<<pTreeNodeTemp->data<<" ";
  297. Enqueue(&queue,pTreeNodeTemp);
  298. pTreeNode pTemp;
  299. while(!IsQueueEmpty(queue))
  300. {
  301. Dequeue(&queue,&pTemp);
  302. if(pTemp->pFirstChild != NULL)
  303. {
  304. pTemp = pTemp->pFirstChild;
  305. cout<<pTemp->data<<" ";
  306. Enqueue(&queue,pTemp);//入队长子
  307. while(pTemp->pNextSibling != NULL)
  308. {
  309. pTemp = pTemp->pNextSibling;
  310. cout<<pTemp->data<<" ";
  311. Enqueue(&queue,pTemp);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. void InsertTree(ppTreeNode ppTreeNodeTemp,ElemType data,int i,pTreeNode pSub)
  318. {
  319. if(*ppTreeNodeTemp == NULL)
  320. {
  321. return;
  322. }
  323. pTreeNode pTreeNodeTemp = Point(*ppTreeNodeTemp,data);
  324. if(pTreeNodeTemp != NULL)
  325. {
  326. if(i==1)
  327. {
  328. pSub->pNextSibling = pTreeNodeTemp->pFirstChild;
  329. pTreeNodeTemp->pFirstChild = pSub;
  330. }else
  331. {
  332. int j = 2;
  333. pTreeNodeTemp = pTreeNodeTemp->pFirstChild;
  334. while (j < i && pTreeNodeTemp)
  335. {
  336. pTreeNodeTemp = pTreeNodeTemp->pNextSibling;
  337. j++;
  338. }
  339. if(j == i)
  340. {
  341. pSub->pNextSibling = pTreeNodeTemp->pNextSibling;
  342. pTreeNodeTemp->pNextSibling = pSub;
  343. }else
  344. {
  345. return;
  346. }
  347. }
  348. }
  349. }
  350. void DeleteTree(ppTreeNode ppTreeNodeTemp,ElemType data,int i)
  351. {
  352. if(*ppTreeNodeTemp == NULL)
  353. {
  354. return;
  355. }
  356. pTreeNode pTemp;
  357. pTreeNode pTreeNodeTemp = Point(*ppTreeNodeTemp,data);
  358. if(pTreeNodeTemp != NULL)
  359. {
  360. if(i == 1)//删除长子
  361. {
  362. pTemp = pTreeNodeTemp->pFirstChild;
  363. pTreeNodeTemp->pFirstChild = pTemp->pNextSibling;
  364. pTemp->pNextSibling = NULL;
  365. DestroyTree(&pTemp);
  366. }else
  367. {
  368. pTreeNodeTemp = pTreeNodeTemp->pFirstChild;
  369. int j = 2;
  370. while(j < i && pTreeNodeTemp)
  371. {
  372. pTreeNodeTemp = pTreeNodeTemp->pNextSibling;
  373. j++;
  374. }
  375. if(j == i)
  376. {
  377. pTemp = pTreeNodeTemp->pNextSibling;
  378. pTreeNodeTemp->pNextSibling = pTemp->pNextSibling;
  379. pTemp->pNextSibling = NULL;
  380. DestroyTree(&pTemp);
  381. }
  382. }
  383. }
  384. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/692686
推荐阅读
相关标签
  

闽ICP备14008679号