1. #include<iostream>
  2. using namespace std;
  3. struct BinaryTreeNode{
  4. int ltag,rtag;
  5. char data;
  6. BinaryTreeNode* leftChild;
  7. BinaryTreeNode* rightChild;
  8. BinaryTreeNode(char newdata,int newltag=0,int newrtag=0){
  9. data=newdata;
  10. ltag=newltag;
  11. rtag=newrtag;
  12. }
  13. };
  14. BinaryTreeNode* treeRoot;//声明树根节点指针
  15. //前序和中序遍历重构二叉树
  16. BinaryTreeNode* createBinaryTree(char* VLR,char* LVR,int n){
  17. if(n==0){
  18. return NULL;
  19. }
  20. int k=0;
  21. while(VLR[0]!=LVR[k]){
  22. k++;
  23. }
  24. BinaryTreeNode* p=new BinaryTreeNode(VLR[0]);
  25. p->leftChild=createBinaryTree(VLR+1,LVR,k);
  26. p->rightChild=createBinaryTree(VLR+k+1,LVR+k+1,n-k-1);
  27. return p;
  28. }
  29. void createInThread(BinaryTreeNode* current,BinaryTreeNode* &pre){
  30. if(current==NULL){
  31. return;
  32. }
  33. createInThread(current->leftChild,pre);
  34. if(current->leftChild==NULL){
  35. current->leftChild=pre;
  36. current->ltag=1;
  37. }
  38. if(pre!=NULL&&pre->rightChild==NULL){
  39. pre->rightChild=current;
  40. pre->rtag=1;
  41. }
  42. pre=current;
  43. createInThread(current->rightChild,pre);
  44. }
  45. //中序线索化二叉树
  46. void createInThread(BinaryTreeNode* root){
  47. BinaryTreeNode* pre=NULL;
  48. if(root!=NULL){
  49. createInThread(root,pre);
  50. pre->rightChild=NULL;
  51. pre->rtag=1;
  52. }
  53. }
  54. //First
  55. BinaryTreeNode* First(BinaryTreeNode* current){
  56. BinaryTreeNode* p=current;
  57. while(p->ltag==0){
  58. p=p->leftChild;
  59. }
  60. return p;
  61. }
  62. //Last
  63. BinaryTreeNode* Last(BinaryTreeNode* current){
  64. BinaryTreeNode* p=current;
  65. while(p->rtag==0){
  66. p=p->rightChild;
  67. }
  68. return p;
  69. }
  70. //Next
  71. BinaryTreeNode* Next(BinaryTreeNode* current){
  72. BinaryTreeNode* p=current->rightChild;
  73. if(current->rtag==0){
  74. return First(p);
  75. }else{
  76. return p;
  77. }
  78. }
  79. //Prior
  80. BinaryTreeNode* Prior(BinaryTreeNode* current){
  81. BinaryTreeNode* p=current->leftChild;
  82. if(current->ltag==0){
  83. return Last(p);
  84. }else{
  85. return p;
  86. }
  87. }
  88. //中序遍历
  89. void inOrder(BinaryTreeNode* root){
  90. BinaryTreeNode* p;
  91. for(p=First(root);p!=NULL;p=Next(p)){
  92. cout<<p->data;
  93. }
  94. }
  95. //前序遍历
  96. void preOrder(BinaryTreeNode* root){
  97. BinaryTreeNode* p=root;
  98. while(p!=NULL){
  99. cout<<p->data;
  100. if(p->ltag==0){
  101. p=p->leftChild;
  102. }
  103. else if(p->rtag==0){
  104. p=p->rightChild;
  105. }
  106. else{
  107. while(p!=NULL&&p->rtag==1){
  108. p=p->rightChild;
  109. }
  110. if(p!=NULL) p=p->rightChild;
  111. }
  112. }
  113. }
  114. //寻找父节点
  115. BinaryTreeNode* parent(BinaryTreeNode* t){
  116. BinaryTreeNode* p;
  117. if(t==treeRoot){
  118. return NULL;
  119. }
  120. for(p=t;p->ltag==0;p=p->leftChild);
  121. if(p->leftChild!=NULL){
  122. for(p=p->leftChild;p!=NULL&&p->leftChild!=t&&p->rightChild!=t;p=p->rightChild);
  123. }
  124. if(p==NULL||p->leftChild==NULL){
  125. for(p=t;p->rtag==0;p=p->rightChild);
  126. for(p=p->rightChild;p!=NULL&&p->leftChild!=t&&p->rightChild!=t;p=p->leftChild);
  127. }
  128. return p;
  129. }
  130. //后序遍历
  131. void postOrder(BinaryTreeNode* root){
  132. cout<<"暂时没写";
  133. }
  134. int main(){
  135. char* vlr = "ABCDEFGH";//前序
  136. char* lvr = "CBEDFAGH";//中序
  137. //后序应为:CEFDBHGA
  138. treeRoot=createBinaryTree(vlr,lvr,8);//根据前序和中序序列创建二叉树,并返回根节点指针
  139. createInThread(treeRoot);
  140. cout<<"中序线索二叉树的中序遍历:";
  141. inOrder(treeRoot);
  142. cout<<endl;
  143. cout<<"前序线索二叉树的前序遍历:";
  144. preOrder(treeRoot);
  145. cout<<endl;
  146. cout<<"后序线索二叉树的后序遍历:";
  147. postOrder(treeRoot);
  148. cout<<endl;
  149. cout<<"查找父节点:";
  150. cout<<parent(treeRoot->leftChild)->data;//应该输出根节点data
  151. cout<<endl;
  152. return 0;
  153. }

 

版权声明:本文为zzyf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zzyf/p/13392667.html