#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define maxSize 100
typedef int Status;
typedef int TElemType;
//二叉树顺序存储结构
#define MAXTSIZE 100
typedef TElemType SqBiTree[MAXTSIZE];
SqBiTree bt;
//二叉树链式存储结构
typedef struct BTNode{
TElemType data;
struct BTNode *lchild,*rchild;
}BTNode,*BiTree;
BiTree T;
//三叉链表
typedef struct TriTNode{
TElemType data;
struct TriTNode *lchild,*parent,*rchild;
}TriTNode,*TriTree;
//遍历二叉树
void visit(BiTree T){
cout<<T->data;
}
//先序遍历
Status PreOrderTraverse(BiTree T){
if(T==NULL) return OK;
else{
visit(T);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//中序遍历
Status InOrderTraverse(BiTree T){
if(T==NULL) return OK;
else{
PreOrderTraverse(T->lchild);
visit(T);
PreOrderTraverse(T->rchild);
}
}
//后序遍历
Status PreOrderTraverse(BiTree T){
if(T==NULL) return OK;
else{
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
visit(T);
}
}
//中序遍历非递归算法(栈)
Status nnOrderTraverse(BiTree T){
BiTree p,q; InitStack(S); p=T;
while(p||!StackEmpty(S)){
if(p){
Push(S,p);
p=p->lchild;
}
else{
Pop(S,q);
cout<<q->data;
p=q->rchild; //?
}
}
return OK;
}
//二叉树层次遍历(队列)
//队列定义类型
typedef struct{
BTNode data[MAXTSIZE];
int front,rear;
}SqQueue;
//遍历
void LevelOrder(BTNode *b){
BTNode *p; SqQueue *qu;
InitQueue(qu);
enQueue(qu,b);
while(!QueueEmpty(qu)){
deQueue(qu,p); //出队结点p
cout<<p->data;
if(p->lchild!=NULL)
enQueue(qu,p->lchild);
if(p->child!=NULL)
enQueue(qu,p->rchild);
}
}
//二叉树算法的应用
//二叉树的建立
//先序序列
Status CreateBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else{
if(!(T=(BTNode *)malloc(sizeof(BTNode))))
exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
//复制二叉树
int Copy(BiTree T,BiTree &NewT){
if(T==NULL){
NewT=NULL;
return 0;
}
else{
NewT=new BTNode;
NewT->data=T->data;
Copy(T->lchild,NewT->lchild);
Copy(T->rchild,NewT->rchild);
}
}
//计算二叉树的深度
int Depth(BiTree T){
if(T==NULL)
return 0;
else{
int m=Depth(T->lchild);
int n=Depth(T->rchild);
if(m>n) return(m+1);
else return(n+1);
}
}
//计算二叉树结点总数
int NodeCount(BiTree T){
if(T==NULL)
return 0;
else
return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
//计算二叉树叶子结点数
int LeadCount(BiTree T){
if(T==NULL)
return 0;
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
return LeadCount(T->lchild)+LeadCount(T->rchild);
}
//线索二叉树(Threaded Binary Tree)
typedef struct BiThrNode{
int data;
int ltag,rtag;
struct BiThrNode *lchild,*rchild;
}BiTHrNode,*BiThrTree;
//???
//树和森林
//双亲链表
//类型描述和树结构
typedef struct PTNode{
TElemType data;
int parent;
}PTNode;
#define MAX_TREE_SIZE 100
typedef struct{
PTNode nodes[MAX_TREE_SIZE];
int r,n;
}PTree;
//孩子链表
//孩子结点结构
typedef struct CTNode{
int child;
struct CTNode *next;
}*ChildPtr;
//双亲结点结构
typedef struct{
TElemType data;
ChildPtr firstchild;
}CTBox;
//孩子兄弟表示法
typedef struct CSNode{
TElemType data;
struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;
//树的遍历
//先根遍历
//哈夫曼树
//构造
//采用顺序存储结构---一维结构数组
//结点类型定义
typedef struct{
int weight;
int parent,lch,rch;
}HTNode,*HuffmanTree;
void CreatHuffmanTree(HuffmanTree HT,int n){
int i;
if(n<=1)return;
int m=2*n-1;
HT=new HTNode[m+1]; //0号单元不用,HT[m]表示根节点
for(i=1;i<=m;++i){
HT[i].lch=0;HT[i].rch=0;HT[i].parent=0;
}
for(i=1;i<=n;++i) cin>>HT[i].weight;
for(i=n+1;i<m;i++){
Select(HT,i-1,s1,s2);
HT[s1].preant=i;HT[s2].parent=i;
HT[i].lch=s1; HT[i].rch=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
}
//哈夫曼编码从叶子到根逆向求每个字符的哈夫曼编码
void CreatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n){
HC=new char *[n+1];
int i,c,f,start;
char *cd=new char [n];
cd[n-1]='\0';
for(i=1;i<=n;++i){
start=n-1;c=i;f=HT[i].parent;
while(f!=0){
--start;
if(HT[f].lch==c) cd[start]='0';
else cd[start]='1';
c=f;f=HT[f].parent;
}
HC[i]=new char [n-start];
strcpy(HC[i],&cd[start]);
}
delete cd;
}//CreatHuffmanCode
数据结构C++ 树和二叉树
最新推荐文章于 2024-10-31 13:56:46 发布