4.1链表二叉树
来源:互联网 发布:如何美化java做的界面 编辑:程序博客网 时间:2024/05/20 22:01
头文件 LinkQueue.h:
#include<iostream>using namespace std;typedef char DataType;typedef struct bnode{ DataType data; struct bnode *lchild,*rchild;} Bnode,*BTree;typedef struct node{BTree data;struct node *next;}QNode,*PQNode;typedef struct {PQNode front,rear;}LinkQueue,*PLinkQueue;//初始化队列PLinkQueue Init_LinkQueue(void){PLinkQueue Q;Q=(PLinkQueue)malloc(sizeof(LinkQueue));if(Q){Q->front=NULL;Q->rear=NULL;}return Q;}//判断队空(是否有元素) int Empty_LinkQueue(PLinkQueue Q){if(Q&&Q->front==NULL&&Q->rear==NULL)return 1;elsereturn 0;}//入队int In_LinkQueue(PLinkQueue Q,BTree x){PQNode p;p=(PQNode)malloc(sizeof(QNode));if(!p){cout<<"内存溢出";return 0;}p->data=x;p->next=NULL;if(Empty_LinkQueue(Q)){Q->rear=Q->front=p;}else{Q->rear->next=p; Q->rear=p;}return 1;}//出队int Out_LinkQueue(PLinkQueue Q,BTree *x){PQNode p; if(Empty_LinkQueue(Q)){cout<<"队空";return 0;}*x=Q->front->data;p=Q->front;Q->front=Q->front->next;free(p);if(!Q->front){Q->rear=NULL;} return 1; }//读队头元素int GetTop_LinkQueue(PLinkQueue Q,BTree *x){if(Empty_LinkQueue(Q)){cout<<"队空";return 0;}*x=Q->front->data;return 1;} //销毁队列void Destroy_LinkQueue(PLinkQueue *Q){PQNode p,s;if(*Q){s=(*Q)->front;while(s){p=s;s=s->next;free(p);}/*while((*Q)->front){p=(*Q)->front; (*Q)->front=(*Q)->front->next;free(p);}*/free(*Q);}*Q=NULL;return ;}
源文件:
#include <stdio.h>#include <stdlib.h>#include <iostream>#include "LinkQueue.h"#define MaxTreeNodeNum 100using namespace std;int count=0;void Visit(char data){ cout<<data<<" ";if(data=='H'){}}void PreOrder(BTree t)//先序遍历 { if(t) { Visit(t->data); PreOrder(t->lchild); PreOrder(t->rchild); } }void InOrder(BTree t)//中序遍历 { if(t) { InOrder(t->lchild); Visit(t->data); InOrder(t->rchild); } }void PostOrder(BTree t)//后序遍历 { if(t) { PostOrder(t->lchild); PostOrder(t->rchild); Visit(t->data); } }BTree CreatBinTree()//以加入空结点的先序序列输入,构建二叉链表 { BTree t;//ABD#G###CE##FH### char ch; ch=getchar(); if(ch=='#') t=NULL; else { t=(Bnode *)malloc(sizeof(Bnode));//生成节点空间 t->data=ch; t->lchild=CreatBinTree();//左子树 t->rchild=CreatBinTree();//右子树 } return t; }void Count_Tree(BTree t)//计数二叉树的结点个数{ //static int count=0;Dev不支持全局标量 if(t) { Count_Tree(t->lchild); //Visit(t->data); count++; Count_Tree(t->rchild); } } /*递归方法实现计数二叉树的结点个数 int Count(BTree t){ int lcount,rcount; if(t==NULL) return 0; lcount=Count(t->lchild); rcount=Count(t->rchild); return lcount+rcount+1;}*/int Height(BTree t)//计算二叉树的高度(深度){ int h1,h2; if(t==NULL) return 0; else { h1=Height(t->lchild); h2=Height(t->rchild); return (h1>h2)?h1+1:h2+1; } } BTree CopyTree(BTree t)//复制二叉树{ BTree p,q,s; if(t==NULL) return NULL; p=CopyTree(t->lchild); q=CopyTree(t->rchild); s=(Bnode *)malloc(sizeof(Bnode)); s->data=t->data; s->lchild=p; s->rchild=q; return s;}void Levcount(BTree t,int L,int num[])//求链式二叉树每层结点个数 (L为当前层数,初始值为1,t初始指向根节点,num[]初始值为0) { if(t) { Visit(t->data);num[L]++; Levcount(t->lchild,L+1,num); Levcount(t->rchild,L+1,num); } }void LayerOrder(PLinkQueue Q,BTree t)//层次遍历{ BTree u;u=(BTree)malloc(sizeof(Bnode)); In_LinkQueue(Q,t);//t进队列while(!Empty_LinkQueue(Q)){Out_LinkQueue(Q,&u);cout<<u->data<<" ";if(u->lchild) In_LinkQueue(Q,u->lchild);if(u->rchild) In_LinkQueue(Q,u->rchild);}Destroy_LinkQueue(&Q);}int main(){ BTree t=CreatBinTree();////ABD#G###CE##FH###//AB#D##CE##F##PostOrder(t);后序遍历 //PreOrder(t);//InOrder(t); Count_Tree(t);//结点数cout<<count<<endl;//cout<<Count(t)<<endl; PLinkQueue Q=Init_LinkQueue();LayerOrder(Q,t);//层次遍历cout<<Height(t)<<endl;//深度 system("pause"); return 0;}
0 0
- 二叉树、二叉链表
- 二叉树:二叉链表
- 4.1链表二叉树
- 二叉树,线索二叉树,二叉链表
- 二叉树(二叉链表实现)
- 二叉树的二叉链表创建
- 二叉链表生成二叉树
- 二叉树的二叉链表实现
- 二叉树的二叉链表实现
- 二叉链表实现二叉树
- 二叉树之二叉链表
- 二叉树的二叉链表存储
- 建立二叉树的二叉链表
- 二叉链表生成二叉树
- 二叉树之二叉链表
- 二叉树------二叉查找树转化为双向链表
- 二叉树22:二叉搜索树与双向链表
- 二叉链表存储的二叉树的复制
- 抽象类
- discuzX2.5 源码模板简介
- js实现图片上传预览
- LeetCode-Move Zeroes
- 关于自增自减运算符的一些问题
- 4.1链表二叉树
- ggplot2学习笔记之构建图层
- 设计模式——三工厂模式
- hdu2262 Where is the canteen
- ThinkPHP--RBAC为角色分配权限成功之收集表单信息
- 1425:sort
- 最小生成树--prim算法的c语言描述
- Python import机制 (十七)
- 细谈Struts2框架(二)Struts2框架之Scope属性