这几日又把二叉树的递归写了一遍,原来是用C写的,自己写了一个栈,这一次直接用的C++,使用了自带的栈结构。代码如下:
1 /************************************************************************* 2 > Author: Yves 3 > E-mail: [email protected] 4 > File Name: BiTreeNew.cpp 5 > Description: ... 6 > Created Time: 2015-05-17. 15:51:24 7 > Modified Time: 2015-05-17. 15:51:24 8 ************************************************************************/ 9 10 #include<stack> 11 #include<cstdlib> 12 #include<stdio.h> 13 #include<iostream> 14 using namespace std; 15 16 typedef char ElemType; 17 18 typedef struct tagBiTreeNode 19 { 20 ElemType data; 21 struct tagBiTreeNode* leftChild; 22 struct tagBiTreeNode* rightChild; 23 }BiTreeNode, *pBiTreeNode;//指向BiTNode的指针 24 25 //创建二叉树 26 static void CreatBiTree(pBiTreeNode *root); 27 //访问树 28 static void VisitTree(pBiTreeNode p, const int level); 29 //前序递归遍历 30 static void PreOrderRecursiveTraversal(const pBiTreeNode root, int level, void (*VisitTree)(pBiTreeNode p, const int level)); 31 //前序非递归遍历 32 static void PreRecursiveTraversal(const pBiTreeNode root, void (*VisitTree)(pBiTreeNode p, const int level)); 33 //中序递归遍历 34 static void InOrderRecursiveTraversal(const pBiTreeNode root, int level, void (*VisitTree)(pBiTreeNode p, const int level)); 35 //中序非递归遍历 36 static void InOrdeTraversal(const pBiTreeNode root, void (*VisitTree)(pBiTreeNode p, const int level)); 37 //后序递归遍历 38 static void PostOrderRecursiveTraversal(const pBiTreeNode root, int level, void (*VisitTree)(pBiTreeNode p, const int level)); 39 //后序非递归遍历 40 static void PostOrderTraversal(const pBiTreeNode root, void (*VisitTree)(pBiTreeNode p, const int level)); 41 42 static void CreatBiTree(pBiTreeNode *root) 43 { 44 /*Here we creat the tree in preorder, so the input 45 sequences must comply with the preorder*/ 46 ElemType c; 47 cin>>c; 48 if(c != ‘#‘) 49 { 50 *root = (BiTreeNode*)malloc(sizeof(BiTreeNode)); 51 (*root)->data = c; 52 CreatBiTree(&(*root)->leftChild); 53 CreatBiTree(&(*root)->rightChild); 54 }else 55 { 56 *root = NULL; 57 return; 58 } 59 } 60 61 static void VisitTree(pBiTreeNode p, const int level) 62 { 63 if(level > 0) 64 { 65 cout<<p->data<<" is on the level "<<level<<"."<<endl; 66 } 67 else 68 { 69 cout<<p->data<<" "; 70 } 71 } 72 73 static void PreOrderRecursiveTraversal(const pBiTreeNode root, int level, void (*VisitTree)(pBiTreeNode p, const int level)) 74 { 75 if(root == NULL) 76 { 77 return; 78 } 79 VisitTree(root, level); 80 PreOrderRecursiveTraversal(root->leftChild, level + 1, VisitTree); 81 PreOrderRecursiveTraversal(root->rightChild, level + 1, VisitTree); 82 } 83 static void PreRecursiveTraversal(const pBiTreeNode root, void (*VisitTree)(pBiTreeNode p, const int level)) 84 { 85 if(root == NULL) 86 { 87 return; 88 } 89 stack<pBiTreeNode> s; 90 pBiTreeNode p; 91 p = root; 92 while(!s.empty() || p != NULL) 93 { 94 while(p != NULL) 95 { 96 VisitTree(p, 0); 97 s.push(p); 98 p = p->leftChild; 99 } 100 if(!s.empty()) 101 { 102 p = s.top(); 103 s.pop(); 104 p = p->rightChild; 105 } 106 } 107 } 108 static void InOrderRecursiveTraversal(const pBiTreeNode root,int level, void (*VisitTree)(pBiTreeNode p, const int level)) 109 { 110 if(root == NULL) 111 { 112 return; 113 } 114 InOrderRecursiveTraversal(root->leftChild, level + 1, VisitTree); 115 VisitTree(root, level); 116 InOrderRecursiveTraversal(root->rightChild, level + 1, VisitTree); 117 } 118 static void InOrderTraversal(const pBiTreeNode root, void (*VisitTree)(pBiTreeNode p, const int level)) 119 { 120 if(root == NULL) 121 { 122 return; 123 } 124 stack<pBiTreeNode> s; 125 pBiTreeNode p; 126 p = root; 127 while(!s.empty() || p != NULL) 128 { 129 while(p != NULL) 130 { 131 s.push(p); 132 p = p->leftChild; 133 } 134 if(!s.empty()) 135 { 136 p = s.top(); 137 VisitTree(p, 0); 138 s.pop(); 139 p = p->rightChild; 140 } 141 } 142 } 143 static void PostOrderRecursiveTraversal(const pBiTreeNode root,int level, void (*VisitTree)(pBiTreeNode p, const int level)) 144 { 145 if(root == NULL) 146 { 147 return; 148 } 149 PostOrderRecursiveTraversal(root->leftChild, level + 1, VisitTree); 150 PostOrderRecursiveTraversal(root->rightChild, level + 1, VisitTree); 151 VisitTree(root, level); 152 } 153 static void PostOrderTraversal(const pBiTreeNode root, void (*VisitTree)(pBiTreeNode p, const int level)) 154 { 155 stack<pBiTreeNode> s; 156 pBiTreeNode pCurrentNode; 157 pBiTreeNode pPrecedingNode = NULL; 158 s.push(root); 159 while(!s.empty()) 160 { 161 pCurrentNode = s.top(); 162 /*There is two cases: 163 * 1, the lefechild and the rightchild is all NULL, meaning it is a leaf node,so we can visit it directly. 164 * 2, the preceding node is not NULL,in addtion, it is the rightchild or the leftchild of the curretn node. 165 * Notice because of our order of pushing nodes into stack, it is sure that the two nodes(leftchild,rightchild) 166 * have accessed yet if one of conditions satisfies: 167 * pPrecedingNode == pCurrentNode->leftChild or 168 * pPrecedingNode == pCurrentNode->rightChild)) 169 * of course, pPrecedingNode != NULL must be satisfied. 170 */ 171 if((pCurrentNode->leftChild == NULL && pCurrentNode->rightChild == NULL) 172 || (pPrecedingNode != NULL && (pPrecedingNode == pCurrentNode->leftChild 173 || pPrecedingNode == pCurrentNode->rightChild))) 174 { 175 VisitTree(pCurrentNode, 0); 176 s.pop(); 177 pPrecedingNode = pCurrentNode; 178 }else 179 { 180 if(pCurrentNode->rightChild != NULL) 181 { 182 s.push(pCurrentNode->rightChild); 183 } 184 if(pCurrentNode->leftChild != NULL) 185 { 186 s.push(pCurrentNode->leftChild); 187 } 188 } 189 } 190 } 191 192 193 int main(void) 194 { 195 pBiTreeNode root; 196 197 cout<<"Please input the node of the tree(preorder sequence): "<<endl; 198 CreatBiTree(&root); 199 cout<<endl; 200 201 202 cout<<"Traverse the tree in preorder(non-recursive): "<<endl; 203 PreRecursiveTraversal(root, &VisitTree); 204 cout<<endl; 205 cout<<"Traverse the tree in preorder: "<<endl; 206 PreOrderRecursiveTraversal(root,1,&VisitTree); 207 cout<<endl; 208 209 210 cout<<"Traverse the tree in inorder(non-recursive): "<<endl; 211 InOrderTraversal(root, &VisitTree); 212 cout<<endl; 213 cout<<"Traverse the tree in inorder: "<<endl; 214 InOrderRecursiveTraversal(root, 1, &VisitTree); 215 cout<<endl; 216 217 cout<<"Traverse the tree in postorder(non-recursive): "<<endl; 218 PostOrderTraversal(root, &VisitTree); 219 cout<<endl; 220 cout<<"Traverse the tree in posteorder: "<<endl; 221 PostOrderRecursiveTraversal(root, 1, &VisitTree); 222 cout<<endl; 223 224 return 0; 225 }
时间: 2024-10-31 07:49:39