二叉树
来源:互联网 发布:北明软件 编辑:程序博客网 时间:2024/05/29 09:09
#include<iostream.h>typedef struct node{node *leftchild,*rightchild,*next;char ch;}*ptrn;class bintree{private:int count,i;int leafnodenumber;int leftdep,rightdep;ptrn newnode;ptrn leftchildnode;ptrn rightchildnode;ptrn *stack;ptrn popnode;int maxsize,nodenumber,treeheight;char *array;ptrn *widthtree;ptrn *b;public:ptrn root; //根结点 bintree(int size)//析构函数{count=0;//创建树是所用的栈的计数i=0;//以树的高度为容量创建数组leftdep=0;rightdep=0;leafnodenumber=0;//叶子结点数nodenumber=0;//结点数maxsize=size;//栈的最大容量stack=new ptrn[maxsize];//为栈分配空间root=NULL;//初始根为空array="a(b(d,e(h(j,k(l,m(,n))))),c(f,g(,i)))";//树的表示}bool empty()//计算栈是否为空{return count==0;}void push(ptrn &e)//入栈{if(count!=maxsize){stack[count]=e;count++;}}void pop()//出栈{if(!empty()){count--;}}int length()//求栈的大小{return count;}void creattree()//创建树{for(int i=0;i<maxsize;i++){switch(array[i]){case '(':push(newnode);break;case ')':pop();newnode=stack[count];break;case ',': if(array[i-1]==')') { pop();newnode=stack[count]; rightchildnode=new node; rightchildnode->leftchild=NULL; rightchildnode->rightchild=NULL; rightchildnode->ch=array[i+1];i++; newnode->rightchild=rightchildnode; if(array[i+1]=='(') {newnode=newnode->rightchild;}break; } else { rightchildnode=new node; rightchildnode->leftchild=NULL; rightchildnode->rightchild=NULL; rightchildnode->ch=array[i+1];i++; newnode->rightchild=rightchildnode; if(array[i+1]=='(') {newnode=newnode->rightchild;}break; }default:if(root==NULL){root=new node;root->ch=array[i]; newnode=root;root->leftchild=NULL;root->rightchild=NULL;}else{leftchildnode=new node; leftchildnode->leftchild=NULL;leftchildnode->rightchild=NULL;leftchildnode->ch=array[i];newnode->leftchild=leftchildnode;if(array[i+1]=='(')newnode=newnode->leftchild;}}}}void widthtreesize()//为数组widthtree分配空间{widthtree=new ptrn[height(root)];b=new ptrn[height(root)];widthtree[-1]=NULL;for(int i=0;i<height(root);i++){widthtree[i]=NULL;}}int height(ptrn &e)//求树的高度{if(e==NULL){return 0;}else{int leftheight,rightheight;leftheight=height(e->leftchild);rightheight=height(e->rightchild);return (leftheight>rightheight?leftheight:rightheight)+1;}} void width(ptrn &e)//树的每一层为一个链,为每一个链赋值{if(widthtree[i-1]==NULL){widthtree[i]=e;b[i]=e;b[i]->next=NULL; i++;width(widthtree[i-1]); }else{if(e->leftchild!=NULL){ if(widthtree[i]==NULL){widthtree[i]=e->leftchild; b[i]=e->leftchild;}else{ b[i]->next=e->leftchild; b[i]=e->leftchild;}if(e->rightchild!=NULL){b[i]->next=e->rightchild; b[i]=e->rightchild;}else{}if(e->next==NULL){ b[i]->next=NULL; i++; if(i<height(root)){e=widthtree[i-1]; width(e);}}else{e=e->next;width(e);}}else{if(e->rightchild!=NULL){if(widthtree[i]==NULL){widthtree[i]=e->rightchild;b[i]=e->rightchild;}else{b[i]->next=e->rightchild;b[i]=e->rightchild;}if(e->next==NULL){b[i]->next=NULL;i++; if(i<height(root)){e=widthtree[i-1];width(e);}}else{e=e->next;width(e);}}else{ e=e->next; if(e!=NULL){width(e);}else{b[i]->next=NULL;i++; if(i<height(root)){e=widthtree[i-1];width(e);}}}}}}int leafnodecount(ptrn &e)//求叶子结点个数{if(e!=NULL&&e->leftchild==NULL&&e->rightchild==NULL){leafnodenumber++;return leafnodenumber;}else if(e!=NULL){ leafnodecount(e->leftchild);leafnodecount(e->rightchild);} }void findnode(ptrn &e,char &ch1)//查找结点内容为ch1的子节点{if(e!=NULL&&e->ch==ch1){ if(e->leftchild!=NULL&&e->rightchild!=NULL){cout<<"随求结点的左孩子为"<<e->leftchild->ch<<"右孩子为"<<e->rightchild->ch<<endl;}else if(e->leftchild!=NULL&&e->rightchild==NULL){cout<<"随求结点只有左孩子,内容为"<<e->leftchild->ch<<endl;}else if(e->leftchild==NULL&&e->rightchild!=NULL){ cout<<"随求结点只有右孩子,内容为"<<e->rightchild->ch<<endl;}else{cout<<"所求结点无孩子"<<endl;}}else if(e!=NULL&&e->ch!=ch1){ findnode(e->leftchild,ch1); findnode(e->rightchild,ch1);}}int linklength(ptrn &e)//求上述链以e为头结点的长度{ptrn temp;temp=e;int linknodecount=0;while(temp!=NULL){linknodecount++;temp=temp->next;}return linknodecount;}void outputwidth()//输出树宽{int n=0;int j=0;while(j<height(root)){n=n>linklength(widthtree[j])?n:linklength(widthtree[j]);j++;} cout<<"树的宽度为"<<n<<endl;}int nodecount()//求结点个数{ return nodenumber;}void trailnode(ptrn &e)//以先序序列遍历树{ if(e!=NULL) { nodenumber++; cout<<e->ch<<endl; trailnode(e->leftchild); trailnode(e->rightchild); }}};void main(){char ch2='h';bintree bt(36);bt.creattree();bt.widthtreesize();bt.width(bt.root);bt.trailnode(bt.root);bt.findnode(bt.root,ch2); bt.outputwidth();cout<<"树高为"<<bt.height(bt.root)<<endl;cout<<"叶子数为"<<bt.leafnodecount(bt.root)<<endl;cout<<"结点总数为"<<bt.nodecount()<<endl;}
0 0
- 二叉树、二叉堆
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 【笔记】 《js权威指南》- 第8章 函数 - 8.2 - 8.3 函数调用 实参 形参
- Poj 3614 Sunscreen【贪心+优先队列】
- 立体匹配十大概念综述---立体匹配算法介绍
- Eclipse创建的项目出现感叹号的解决方法
- 乐观锁和悲观锁
- 二叉树
- iOS开发中Pch文件的使用与技巧
- IOS开发 使用XIB自定义一个UIView
- HDU 5655 CA Loves Stick
- 图的邻接表存储
- coreboot学习10:coreboot第一阶段学习小结
- CSU 1718(脑洞)
- Git的.gitignore文件配置格式。。。
- 『C++ Primer学习笔记』Chapter 3——string对象、vector对象以及数组的不同处理方法