二叉树的遍历、深度、结点个数

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <iostream>
  3 #include <stack>
  4 #include <deque>
  5 using namespace std;
  6 
  7 //二叉树节点的描述
  8 typedef struct BiTNode
  9 {
 10     char data;
 11     struct BiTNode *lchild, *rchild;
 12 }BiTNode, *BiTree;
 13 
 14 //按照先序遍历创建二叉树
 15 void CreateBiTree(BiTNode **root)
 16 {
 17     char ch;//要插入的数据
 18     scanf("\n%c", &ch);
 19     //cin>>ch;
 20     if (ch == '#')
 21         *root = NULL;
 22     else
 23     {
 24         *root = (BiTNode*)malloc(sizeof(BiTNode));
 25         (*root)->data = ch;
 26         printf("请输入%c的左孩子:", ch);
 27         CreateBiTree(&((*root)->lchild));
 28         printf("请输入%c的右孩子:", ch);
 29         CreateBiTree(&((*root)->rchild));
 30     }
 31 }
 32 
 33 //前序递归遍历
 34 void PreOrder(BiTNode *root)
 35 {
 36     if (root == NULL)
 37         return;
 38     printf("%c", root->data); //输出数据
 39     PreOrder(root->lchild); //递归调用,前序遍历左子树
 40     PreOrder(root->rchild); //递归调用,前序遍历右子树
 41 }
 42 
 43 //中序递归遍历
 44 void InOrder(BiTNode *root)
 45 {
 46     if (root == NULL)
 47         return;
 48     InOrder(root->lchild);
 49     printf("%c", root->data);
 50     InOrder(root->rchild);
 51 }
 52 
 53 //后序递归遍历
 54 void PostOrder(BiTNode *root)
 55 {
 56     if (root == NULL)
 57         return;
 58     PostOrder(root->lchild);
 59     PostOrder(root->rchild);
 60     printf("%c", root->data);
 61 }
 62 
 63 //非递归前序遍历
 64 void PreOrderUnRecur(BiTree root)
 65 {
 66     if (root == NULL)
 67         return;
 68     stack<BiTree>s;
 69     s.push(root);
 70   
 71     while (!s.empty())
 72     {
 73         BiTree temp = s.top();
 74         cout << temp->data << "";
 75         s.pop();
 76         if (temp->rchild)
 77             s.push(temp->rchild);
 78         if (temp->lchild)
 79             s.push(temp->lchild);
 80     }
 81 }
 82 
 83 //非递归中序遍历
 84 void InOrderUnRecur(BiTree root)
 85 {
 86     if (root == NULL)
 87         return;
 88     stack<BiTree>s;
 89     BiTree curr = root;
 90     while (curr != NULL || !s.empty())
 91     {
 92         while (curr != NULL)
 93         {
 94             s.push(curr);
 95             curr = curr->lchild;
 96         }
 97         if (!s.empty())
 98         {
 99             curr = s.top();
100             s.pop();
101             cout << curr->data << " ";
102             curr = curr->rchild;
103         }
104     }
105 }
106 
107 //非递归后序
108 void PostOrderUnRecur(BiTree root)
109 {
110     if (root == NULL)
111         return;
112     stack<BiTree>s1, s2;
113     BiTree curr;
114     s1.push(root);
115     while (!s1.empty())
116     {
117         curr = s1.top();
118         s1.pop();
119         s2.push(curr);
120         if (curr->lchild)
121             s1.push(curr->lchild);
122         if (curr->rchild)
123             s1.push(curr->rchild);
124     }
125     while (!s2.empty())
126     {
127         cout << s2.top()->data << " ";
128         s2.pop();
129     }
130 
131 }
132 
133 //递归层次遍历二叉树
134 void PrintFromTopToButtom(BiTree root)
135 {
136     if(!root)
137         return;
138     deque<BiTree>d;
     d.push_back(root);
139 while(d.size()) 140 { 141 BiTree curr = d.front(); 142 d.pop_front(); 143 cout << curr->data << " "; 144 if (curr->lchild) 145 d.push_back(curr->lchild); 146 if (curr->rchild) 147 pushback(curr->rchild); 148 } 149 } 150 151 //递归求树的深度 152 int FindTreeDeep(BiTree root) 153 { 154 int deep = 0; 155 if (root) 156 { 157 int lchilddeep = FindTreeDeep(root->lchild); 158 int rchilddeep = FindTreeDeep(root->rchild); 159 deep = (lchilddeep >= rchilddeep ? lchilddeep : rchilddeep)+1; 160 } 161 return deep; 162 } 163 164 //树的节点个数 165 int CountNode(BiTree root) 166 { 167 if (root == NULL) 168 return 0; 169 return (CountNode(root->lchild) + CountNode(root->rchild)) + 1; 170 } 171 int main(void) 172 { 173 BiTNode *root = NULL; 174 int flag = 1; 175 int k; 176 printf("本程序实现二叉树的基本操作\n"); 177 178 while (flag) 179 { 180 printf("|--------------------------------------------------|\n"); 181 printf("| 二叉树基本操作 |\n"); 182 printf("| 0.创建二叉树 |\n"); 183 printf("| 1.递归前序遍历 |\n"); 184 printf("| 2.递归中序遍历 |\n"); 185 printf("| 3.递归后序遍历 |\n"); 186 printf("| 4.非递归前序遍历 |\n"); 187 printf("| 5.非递归中序遍历 |\n"); 188 printf("| 6.非递归后序遍历 |\n"); 189 printf("| 7.层次递归遍历 |\n"); 190 printf("| 8.二叉树深度 |\n"); 191 printf("|--------------------------------------------------|\n"); 192 193 printf("选择功能:\n"); 194 195 scanf("%d", &k); 196 switch (k) 197 { 198 case 0: 199 printf("建立二叉树并输入二叉树的根节点:\n"); 200 CreateBiTree(&root); 201 break; 202 case 1: 203 if (root) 204 { 205 printf("递归前序遍历二叉树:\n"); 206 PreOrder(root); 207 printf("\n"); 208 } 209 else 210 printf("二叉树为空:\n"); 211 break; 212 case 2: 213 if (root) 214 { 215 printf("递归中序遍历二叉树:\n"); 216 InOrder(root); 217 printf("\n"); 218 } 219 else 220 printf("二叉树为空:\n"); 221 break; 222 case 3: 223 if (root) 224 { 225 printf("递归后序遍历二叉树:\n"); 226 PreOrder(root); 227 printf("\n"); 228 } 229 else 230 printf("二叉树为空:\n"); 231 break; 232 case 4: 233 if (root) 234 { 235 printf("递归前序遍历二叉树:\n"); 236 PreOrderUnRecur(root); 237 printf("\n"); 238 } 239 else 240 printf("二叉树为空:\n"); 241 break; 242 case 5: 243 if (root) 244 { 245 printf("递归中序遍历二叉树:\n"); 246 InOrderUnRecur(root); 247 printf("\n"); 248 } 249 else 250 printf("二叉树为空:\n"); 251 break; 252 case 6: 253 if (root) 254 { 255 printf("递归后序遍历二叉树:\n"); 256 PreOrderUnRecur(root); 257 printf("\n"); 258 } 259 else 260 printf("二叉树为空:\n"); 261 break; 262 case 7: 263 if (root) 264 { 265 printf("层次递归遍历二叉树:\n"); 266 PrintFromTopToButtom(root); 267 printf("\n"); 268 } 269 else 270 printf("二叉树为空:\n"); 271 break; 272 case 8: 273 if (root) 274 printf("二叉树深度:%d\n",FindTreeDeep(root)); 275 else 276 printf("二叉树为空:\n"); 277 break; 278 case 9: 279 if (root) 280 printf("二叉树节点个数:%d\n",CountNode(root)); 281 else 282 printf("二叉树为空:\n"); 283 break; 284 default: 285 flag = 0; 286 printf("程序结束\n"); 287 288 } 289 } 290 system("pause"); 291 return 0; 292 }

 

posted @ 2017-03-29 15:42  ren_zhg1992  阅读(226)  评论(0)    收藏  举报