二叉树的C++实现
来源:互联网 发布:浮云网淘宝小号 编辑:程序博客网 时间:2024/05/16 01:07
关于二叉树的实现直接贴代码吧:
// BinaryTree_Class.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "stdafx.h"#include <iostream>#include<string.h>#include<stack> using namespace std;template<class T>struct _BinNode{T data;struct _BinNode *lchild,*rchild;};template<class T>class BinTree{public:typedef struct _BinNode<T> BinNode;//声明根结点成员BinNode *root;public:BinTree();~BinTree();void createTree( char *s);void printInorder(BinNode *pRoot) const;void _printInorder(BinNode *pRoot) const;void printPreOrder(BinNode *pRoot) const;void _printPreOrder(BinNode *pRoot) const;void printPostOrder(BinNode *pRoot) const;void _printPostOrder(BinNode *pRoot) const;int size(BinNode *T) const;int Height(BinNode *T) const;BinNode * _allocate();//产生一个新的结点};template<class T>typename BinTree<T>::BinNode * BinTree<T>::_allocate(){BinNode *newNode = new struct _BinNode<T>;newNode->lchild = NULL;newNode->rchild = NULL;newNode->data = NULL;return newNode;}template<class T>BinTree<T>::BinTree(){root = _allocate();}template<class T>BinTree<T>::~BinTree(){delete root;root = NULL; //记住在用delete释放root的内存后,root成为了悬空指针,需要让它指为空,否则很危险}template<class T> void BinTree<T>::createTree( char *s) //形如A(B,C(D,E)){stack<T> s1;stack<typename BinTree<T>::BinNode *> s2; //用于存放结点root->data = s[0];s2.push(root);bool isRight = false; //int i=1;while(i<strlen(s)){if(s[i] == '('){isRight = false;s1.push(s[i]);}else if(s[i] == ')'){s1.pop();s2.pop();}else if(s[i] == ',')isRight = true;else{//结点typename BinTree<T>::BinNode *newNode = _allocate();newNode->data = s[i];typename BinTree<T>::BinNode *temp = s2.top();if(isRight)temp->rchild = newNode;elsetemp->lchild = newNode;if(s[i+1] == '(')s2.push(newNode);}i++;}}//中序递归遍历template<class T>void BinTree<T>::printInorder(BinNode *pRoot) const{if(pRoot!=NULL){printInorder(pRoot->lchild);cout<<pRoot->data<<',';printInorder(pRoot->rchild);}}//中序非递归遍历template<class T>void BinTree<T>::_printInorder(BinNode *pRoot) const{stack<BinNode *> s;BinNode *p = pRoot;while(!s.empty() ||p!=NULL){while(p!=NULL){s.push(p);p = p->lchild;}if(!s.empty()){BinNode *temp = s.top();s.pop();cout<<temp->data<<',';p = temp->rchild;}}}template<class T>void BinTree<T>::printPreOrder(typename BinTree<T>::BinNode *pRoot) const{if(pRoot!=NULL){cout<<pRoot->data<<',';printPreOrder(pRoot->lchild);printPreOrder(pRoot->rchild);}}template<class T>void BinTree<T>::_printPreOrder(BinNode *pRoot) const{stack<BinNode *> s;BinNode *p = pRoot;while(!s.empty() || p!=NULL){while(p!=NULL){cout<<p->data<<',';s.push(p);p = p->lchild;}if(!s.empty()){BinNode *temp = s.top();s.pop();p = temp->rchild;}}}template<class T>void BinTree<T>::printPostOrder(typename BinTree<T>::BinNode *pRoot) const{if(pRoot!=NULL){printPostOrder(pRoot->lchild);printPostOrder(pRoot->rchild);cout<<pRoot->data<<',';}}int _tmain(int argc, _TCHAR* argv[]){ char *s = "A(B,C(D,E))";BinTree<char> *bTree = new BinTree<char>;bTree->createTree(s);cout<<"中序遍历:";bTree->printInorder(bTree->root);bTree->_printInorder(bTree->root);cout<<"前序遍历:";bTree->printPreOrder(bTree->root);bTree->_printPreOrder(bTree->root);cout<<"后序遍历:";bTree->printPostOrder(bTree->root);return 0;}
- 二叉树 c实现
- 二叉树 C实现
- 二叉树 c 实现
- c语言二叉树和二叉搜索树的实现
- c语言实现的简单二叉树
- 数据结构的C实现_二叉树
- 二叉树的基本功能实现(c++)
- c语言二叉树的实现
- C语言实现二叉树的遍历
- 二叉树的简单C实现
- 哈弗曼二叉树的C简单实现
- 二叉查找树的实现(using C)
- [数据结构]C语言二叉树的实现
- 数据结构 二叉树的实现 c语言版
- 二叉树的C语言实现
- C语言一个二叉树的实现
- 【C基础】二叉搜索树的实现
- 二叉查找树的C语言实现
- 大数阶乘
- image/pjpeg和image/jpeg问题
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
- 递归求奇数连乘的积
- 2013级C++第14周项目——一维数组、字符数组
- 二叉树的C++实现
- 获取图片宽高信息
- C++中 int i 与 int &i 注意事项
- 排序算法的稳定性
- 去掉Html textarea 标签前面显示空格
- Java并发编程实战~笔记~章三
- Linux(Ubuntu)系统各个目录的作用详解(整合)
- 单例模式(单子模式)
- ida静态分析/破解某软件的 时间限制