关于二叉树的若干代码, 非递归遍历,打印..
2012-12-14 19:50
465 查看
#ifndef BINARY_TREE #define BINARY_TREE #include <iostream> #include <stdio.h> #include <stack> using namespace std; class binaryTree { private: struct btNode { char data; btNode *left, *right; btNode(char c):data(c){} }; btNode *root; int size; //非递归后序遍历辅助数据的类型定义 struct nodeTag { btNode *p; int tag; nodeTag(btNode *_p, int _tag):p(_p), tag(_tag){} nodeTag(){} }; //线序遍历定义 void preorderWalk(btNode *p) { if(p) { cout << p->data <<endl; preorderWalk(p->left); preorderWalk(p->right); } } //中序遍历定义 void inorderWalk(btNode *p) { if(p) { inorderWalk(p->left); cout << p->data << endl; inorderWalk(p->right); } } //后序遍历定义 void postorderWalk(btNode *p) { if(p) { postorderWalk(p->left); postorderWalk(p->right); cout << p->data << endl; } } //根据深度打印节点信息 void printNode(char c, int h) { int i; for(i = 0; i < h; ++i) { cout << " "; } cout << c << endl; } //打印树节点信息 void printTree(btNode *t, int h) { if(!t) { printNode('*', h); return; } printTree(t->left, h + 1); printNode(t->data, h); printTree(t->right, h + 1); } //统计二叉树中节点个数 int nodes(btNode *t) { if(!t) { return 0; } return nodes(t->left) + nodes(t->right); } //求二叉树的高度 int height(btNode *t) { if(!t) { return 0; } int lh = height(t->left), rh = height(t->right); return 1 + (lh > rh ? lh : rh); } //求树中两节点的最长路径 int heightForLR(btNode *t, int &lroad) { if(!t) { return 0; } int lh = height(t->left), rh = height(t->right); if(lh + rh > lroad) { lroad = lh + rh; } return 1 + (lh > rh ? lh : rh); } //建树函数 btNode* buildBT() { char c; btNode *t = 0; c = getchar(); if(c != ' ') { t = new btNode(c); ++size; t->left = buildBT(); t->right = buildBT(); } return t; } void drop(btNode* t) { if(t) { drop(t->left); drop(t->right); delete t; } } public: binaryTree():root(0){} ~binaryTree() { drop(root); } //域外调用接口 void walk() { preorderWalk(root); } void show() { printTree(root, 0); } int getSize() { return size; } int getHeight() { return height(root); } int longestRoad() { int x = 0; heightForLR(root, x); return x; } void build() { size = 0; root = buildBT(); } //利用栈进行先序遍历 void preWalkWithStack() { if(!root) { return; } stack<btNode*> aidS; aidS.push(root); btNode *p; while(!aidS.empty()) { p = aidS.top(); aidS.pop(); cout << p->data <<endl; if(p->right) { aidS.push(p->right); } if(p->left) { aidS.push(p->left); } } } //利用栈实现中序遍历 void inWalkWithStack() { stack<btNode*> aidS; btNode *p = root; while(p || !aidS.empty()) { if(p) { aidS.push(p); p = p->left; } else { p = aidS.top(); aidS.pop(); cout << p->data <<endl; p = p->right; } } } //利用栈实现后序遍历 void postWalkWithStack() { nodeTag nt; stack<nodeTag> aidS; aidS.push(nodeTag(root, 1)); while(!aidS.empty()) { nt = aidS.top(); aidS.pop(); if(nt.tag) { nt.tag = 0; aidS.push(nt); if(nt.p->right) { aidS.push(nodeTag(nt.p->right, 1)); } if(nt.p->left) { aidS.push(nodeTag(nt.p->left, 1)); } } else { cout << nt.p->data << endl; } } } }; #endif
相关文章推荐
- 二叉树的遍历(代码递归和数据递归)
- 非递归前序遍历二叉树-代码实例讲解
- 非递归实现二叉树的前序,中序,后序遍历打印
- 非递归方式遍历二叉树(前序,中序,后序)代码
- 面试常考手写代码之--二叉树非递归遍历
- Java三种遍历方式打印二叉树(递归实现)
- 二叉树的递归 非递归 先序 中序 后序及层次遍历代码实现
- 算法:二叉树的先(根)序遍历、中(根)序遍历、后(根)序遍历(递归及压栈出栈实现的非递归方式)的java代码实现
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- 二叉树的递归、迭代遍历的模板代码
- 二叉树非递归遍历复习代码实现
- c语言数据结构中二叉树的先序、中序、后序三种递归遍历代码
- 二叉树的遍历以及将二叉树倒置90°打印输入(递归)
- 二叉树各种遍历-递归及非递归代码
- 递归、循环遍历二叉树代码实例
- 二叉树先序、中序、后序遍历的非递归实现,纯c代码
- 2013-03-17---二叉树递归,非递归实现(附代码)深度,叶子节点数量,逐行打印二叉树
- 节点遍历二叉树的遍历(递归、非递归)
- 非递归实现二叉树的各种遍历
- 非递归遍历二叉树