对于二叉树的创建我是利用先序遍历的序列进行创建
可以对于树节点的内容我定义为char型变量 ‘0‘为空,即此处的节点不存在
头文件 Tree.h
//链式二叉树的头文件 #pragma once #include<iostream> #include<queue> using namespace std; class BinaryTreeNode { public: char data; BinaryTreeNode *leftChild,*rightChild; BinaryTreeNode(char _data,BinaryTreeNode *_leftChild=NULL,BinaryTreeNode *_rightChild=NULL) { data=_data; leftChild=_leftChild; rightChild=_rightChild; } };//BinaryTreeNode 树的根结点 class BinaryTree { public : BinaryTreeNode *root; int i; BinaryTree(){i=0;root=NULL;} bool IsEmpty(); bool Root(char _data); void CreateBinaryTree(BinaryTreeNode* &t); void CreateBinaryTree(BinaryTreeNode* &t,char data[]); void MakeTree(char _data,BinaryTree *left,BinaryTree *right); void BreakTree(char &_data,BinaryTree &left,BinaryTree &right); void PreOrder(void (Vist(BinaryTreeNode *&)),BinaryTreeNode * &t); void InOrder(void (Vist(BinaryTreeNode *&)),BinaryTreeNode * &t); void PostOrder(void (Vist(BinaryTreeNode *&)),BinaryTreeNode * &t); void LevelOrder(void (Vist(BinaryTreeNode *&))); void Delete(BinaryTreeNode *&t); int Size(BinaryTreeNode *&t); int Height(BinaryTreeNode *&t); }; bool BinaryTree::IsEmpty() { if(root){return false;} else{return true;} } bool BinaryTree::Root(char _data) { if(root){(*root).data=_data;return true;} else{return false;} } void BinaryTree::CreateBinaryTree(BinaryTreeNode *&t) { //data 不为0 char data; cin>>data; if(data!=‘0‘) { t=new BinaryTreeNode(data); CreateBinaryTree( (*t).leftChild); CreateBinaryTree((*t).rightChild); } else{t=NULL;} } void BinaryTree::CreateBinaryTree(BinaryTreeNode* &t,char data[]) { if(data[i]!=‘0‘) { t=new BinaryTreeNode(data[i]); ++i; CreateBinaryTree( (*t).leftChild,data); ++i; CreateBinaryTree((*t).rightChild,data); } else{t=NULL;} } void BinaryTree::MakeTree(char _data,BinaryTree *left,BinaryTree *right) { root=new BinaryTreeNode(_data,(*left).root,(*right).root); (*left).root=(*right).root=NULL; } void BinaryTree::PreOrder(void (Vist(BinaryTreeNode *&)),BinaryTreeNode * &t) { if(t) { Vist(t); PreOrder((Vist),(*t).leftChild); PreOrder((Vist),(*t).rightChild); } } void BinaryTree::InOrder(void (Vist(BinaryTreeNode *&)),BinaryTreeNode * &t) { if(t) { InOrder((Vist),(*t).leftChild); Vist(t); InOrder((Vist),(*t).rightChild); } } void BinaryTree::PostOrder(void (Vist(BinaryTreeNode *&)),BinaryTreeNode * &t) { if(t) { PostOrder((Vist),(*t).leftChild); PostOrder((Vist),(*t).rightChild); Vist(t); } } void BinaryTree::LevelOrder(void (Vist(BinaryTreeNode *&))) { //这边利用STL的queue用的很不习惯 queue<BinaryTreeNode> q; BinaryTreeNode *t; t=root; if(t){q.push(*t);} while(!q.empty()) { t=&q.front(); Vist(t); if(q.front().leftChild) { q.push(*(q.front().leftChild)); } if(q.front().rightChild) { q.push(*(q.front().rightChild)); } q.pop(); } } int BinaryTree::Size(BinaryTreeNode *&t) { if(!t){return 0;} else { return (Size((*t).leftChild)+Size((*t).rightChild)+1); } } int BinaryTree::Height(BinaryTreeNode *&t) { if(!t){return 0;} int h_left,h_right; h_right=Height((*t).rightChild); h_left=Height((*t).leftChild); int height=h_right>h_left?(h_right+1):(h_left+1); return height; }
主函数
//二叉树的创建 先序 中序 后序便利 高度 结点元素个数 和按层便利 2014-4-20 #include<iostream> #include"Tree.h" using namespace std; void Vist(BinaryTreeNode *&t) { if(t){cout<<" "<<(*t).data<<"";return ;} } int main() { BinaryTree binaryTree; char tree[]={‘A‘,‘B‘,‘C‘,‘0‘,‘0‘,‘0‘,‘D‘,‘0‘,‘0‘}; //binaryTree.Root(‘a‘); //binaryTree.CreateBinaryTree(binaryTree.root); binaryTree.CreateBinaryTree(binaryTree.root,tree); cout<<endl<<"PreOrder: "; binaryTree.PreOrder(Vist,binaryTree.root); cout<<endl<<"InOrder: "; binaryTree.InOrder(Vist,binaryTree.root); cout<<endl<<"PostOrder: "; binaryTree.PostOrder(Vist,binaryTree.root); cout<<endl<<"Size: "<<binaryTree.Size(binaryTree.root)<<endl; cout<<"Height: "<<binaryTree.Height(binaryTree.root)<<endl; //binaryTree.LevelOrder(Vist);; return 0; }
前序:ABEHGCF
中序:EBGHAFC
后序:EGHBFCA
层次:ABCEHFG
节点个数:7
树的深度:4
貌似大概就是这样了,唯一要提及的就是书上可能用‘*‘字符表示空,而非‘0‘ 关于这点只要改一下if的判断语句就行了,其他的一切照旧不变.
算法实验-二叉树的创建和前序-中序-后序-层次 遍历,布布扣,bubuko.com
原文:http://blog.csdn.net/u010416101/article/details/24531205