#include "stdafx.h" #include <string> #include <vector> #include <queue> #include <iostream> using namespace std; //定义树的节点 typedef struct BiTNode{ char data; BiTNode *lchild,*rchild; //左右孩子的指针 }*BiTree; //先序创建一个二叉树 int CreateBiTree(BiTree &T,string &str,int *index) { //index为字符串的序数的指针 //#代表结束 //string str="AB#D##C##"; //测试树 if ((*index)<str.size()) { char t_nodeData=str[(*index)++]; if(t_nodeData=='#') T=NULL; else { if(!(T=new (BiTNode))) throw runtime_error("OVERFLOW"); T->data = t_nodeData; // 生成根节点 CreateBiTree(T->lchild,str,index); // 创建左子树 CreateBiTree(T->rchild,str,index); // 创建右子树 } } return 0; } //后序递归遍历,交换左右节点 void PostOrderTraverse(const BiTree T){ if (T == NULL) return; PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); if (T->lchild!=NULL || T->rchild!=NULL) //交换左右子树 { BiTree p=T->lchild; T->lchild=T->rchild; T->rchild = p; } } //先序遍历输出这个树 void PreOrderTraverse1(BiTree T) { if (!T) return; cout<<T->data<<endl; PreOrderTraverse1(T->lchild); PreOrderTraverse1(T->rchild); } //计算树的最大深度 int TreeDepth(BiTree BT) { int leftDep,rightDep; if(BT==NULL) return 0; else { leftDep=TreeDepth(BT->lchild); rightDep=TreeDepth(BT->rchild); if(leftDep>rightDep) return(leftDep+1); else return (rightDep+1); } } int NodeCount(BiTree BT)//统计所有节点的个数 { if(BT==NULL) return 0; else return (NodeCount(BT->lchild)+NodeCount(BT->rchild)+1); } int LeafCount(BiTree BT)//统计叶子节点的个数 { if(BT==NULL) return 0; else if(BT->lchild==NULL&&BT->rchild==NULL) return 1; else return (LeafCount(BT->lchild)+LeafCount(BT->rchild)); } void LevelTraverse(BiTree T)//层次遍历 { queue<BiTree> t_vBiTree; //保存每个节点的指针的队列 if (T) t_vBiTree.push(T); while (!t_vBiTree.empty()) { //如果左右子不为空,则压栈 if (t_vBiTree.front()->lchild != NULL) t_vBiTree.push(t_vBiTree.front()->lchild); if (t_vBiTree.front()->rchild != NULL) t_vBiTree.push(t_vBiTree.front()->rchild); cout<<t_vBiTree.front()->data<<endl; t_vBiTree.pop(); } //while } //求最宽层次的点的个数 int WideLevelNum(BiTree T) { queue<BiTree> t_vBiTree; //保存每个节点的指针的队列 //保存每层的最后一个节点 BiTree t_pflag=T; int maxnum=0; //记录最大层次的节点的个数 if (T){ t_vBiTree.push(T); maxnum++; } while (!t_vBiTree.empty()) { //如果左右子不为空,则压栈 if (t_vBiTree.front()->lchild != NULL) t_vBiTree.push(t_vBiTree.front()->lchild); if (t_vBiTree.front()->rchild != NULL) t_vBiTree.push(t_vBiTree.front()->rchild); //如果出栈的是标记的点,也就是上层的最后一个点,则将现在队列的 //最后一个点赋值为当前层的最后一个点 if (t_vBiTree.front()==t_pflag) t_pflag=t_vBiTree.back(); cout<<t_vBiTree.front()->data<<endl; t_vBiTree.pop(); maxnum=maxnum<t_vBiTree.size()?t_vBiTree.size():maxnum; } //while return maxnum; } //判断是否为完全二叉树,返回1代表否,返回0代表是二叉树 int JudgeTree(BiTree T) { if( T == NULL ) return 1; queue<BiTree> t_vBiTree; //保存每个节点的指针的队列 if (T) t_vBiTree.push(T); while (!t_vBiTree.empty()) { //如果左右子不为空,则压队列,循环结束时, //队列中存的是完全二叉树的最下面一层 if (t_vBiTree.front()->lchild == NULL) break; else t_vBiTree.push(t_vBiTree.front()->lchild); if (t_vBiTree.front()->rchild == NULL) break; else t_vBiTree.push(t_vBiTree.front()->rchild); t_vBiTree.pop(); } //while //没有左子树,只有右子树 if (t_vBiTree.front()->rchild != NULL && t_vBiTree.front()->lchild==NULL) return 1; //左子树还有左子树 if ( t_vBiTree.front()->lchild != NULL && t_vBiTree.front()->lchild->lchild != NULL ) return 1; //如果后续的节点仍然有子树,那么也是非完全二叉树 while (!t_vBiTree.empty()) { if (t_vBiTree.front()->lchild != NULL || t_vBiTree.front()->rchild != NULL ) return 1; t_vBiTree.pop(); } if (t_vBiTree.empty()) return 0; } int main() { string str="AB#D##C##"; //测试树 int index=0; BiTNode *T; CreateBiTree(T,str,&index); cout<< JudgeTree(T); return 0; }
二叉树的层次遍历,叶子节点的个数,是否为完全二叉树等等
最新推荐文章于 2024-03-08 10:21:13 发布