数据结构 (五) DLUT 树

数据结构 (五)DLUT 树

二叉树的创建,遍历和前中后序转换

#include<iostream>
#include<string>
#include<stack>
using namespace std;
#define MAXSIZE 20

typedef char ElemType;
typedef struct BiNode{
	ElemType data;
	BiNode *lchild;
	BiNode *rchild;
}BiTree;

BiTree* CreateBiTree(BiTree *root){
	char ch;
	cout<<"输入节点值:"<<endl;
	cin>>ch;	
	if(ch=='#'){
		root=NULL;
		return root;
	}else{
		root=new BiTree;
		root->data=ch;
		root->lchild=CreateBiTree(root->lchild);
		root->rchild=CreateBiTree(root->rchild);
	}
}//创建二叉树 (此创建对应先序序列)

BiTree *CopyTree(BiTree *root,BiTree *T){
	if(root==NULL){
		T=NULL;
		return T;
	}else{
		T=new BiTree;
		T->data=root->data;
		T->lchild=CopyTree(root->lchild,T->lchild);
		T->rchild=CopyTree(root->rchild,T->rchild);
	}
}//复制二叉树(对应先序序列)

int TreeDepth(BiTree *root){
	int left=0,right=0;
	if(root==NULL){
		return 0;
	}else{
		left=TreeDepth(root->lchild);
		right=TreeDepth(root->rchild);
		if(left>=right)return left+1;
		if(left<right)return right+1;
	}
}//二叉树深度 

void Levelorder(BiTree *root){
	BiNode *p,*Q[MAXSIZE];
	int front=-1,rear=-1;
	rear+=1;
	Q[rear]=root;
	while(front!=rear){
		front=(front+1)%MAXSIZE;
		p=Q[front];
		cout<<p->data;
		if(p->lchild!=NULL){
			rear=(rear+1)%MAXSIZE;
			Q[rear]=p->lchild;
		}
		if(p->rchild!=NULL){
			rear=(rear+1)%MAXSIZE;
			Q[rear]=p->rchild;
		}
	}
}//层次遍历 

void PreOrderTraverse(BiTree *root){
	if(root){
		cout<<root->data<<endl;
		PreOrderTraverse(root->lchild);
		PreOrderTraverse(root->rchild);
	}else{
		return;
	}
}//先序遍历(递归)

void InOrderTraverse(BiTree *root){
	if(root){
		InOrderTraverse(root->lchild);
		cout<<root->data<<endl;
		InOrderTraverse(root->rchild);
	}else{
		return;
	}
}//中序遍历(递归)

void PostOrderTraverse(BiTree *root){
	if(root){	
		PostOrderTraverse(root->lchild);
		PostOrderTraverse(root->rchild);
		cout<<root->data<<endl;
	}else{
		return;
	}
}//后序遍历(递归)


int GetNode(BiTree *T){
	if(T==NULL){
		return 0;
	}else{
		return GetNode(T->lchild)+GetNode(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);
	}
}//计算二叉树叶子结点数 

//void PreOrder(BiTree *root){
//	BiNode *St[MAXSIZE],*p;
//	int top=-1;
//	if(root!=NULL){
//		top++;
//		St[top]=root;
//		while(top>-1){
//			p=St[top];
//			top--;
//			cout<<p->data<<"\t";
//			if(p->rchild!=NULL){
//				top++;
//				St[top]=p->rchild;
//			}
//			if(p->lchild!=NULL){
//				top++;
//				St[top]=p->lchild;
//			}
//		}
//		cout<<endl;
//	}
//}//先序遍历(非递归)
void PreOrder(BiTree *root){
	stack<BiTree*>st;
	BiTree* pointer=root;
	while(!st.empty()||pointer){
		if(pointer){
			cout<<pointer->data<<" ";
			if(pointer->rchild){
				st.push(pointer->rchild);	
			}
			pointer=pointer->lchild;
		}else{
			pointer=st.top();
			st.pop();
		}
	}
} //先序遍历(非递归)

//void InOrder(BiTree *root){
//	BiNode *St[MAXSIZE],*p;
//	int top=-1;
//	if(root!=NULL){
//		p=root;
//		while(top>-1||p!=NULL){
//			while(p!=NULL){
//				top++;
//				St[top]=p;
//				p=p->lchild;
//			}
//			
//			if(top>-1){
//				p=St[top];
//				top--;
//				cout<<p->data<<"\t";
//				p=p->rchild; 
//			}
//		}
//		cout<<endl;
//	}
//}//中序遍历(非递归) 

void InOrder(BiTree* root){
	stack<BiTree*>st;
	BiTree* pointer=root;
	while(!st.empty()||pointer){
		if(pointer){
			st.push(pointer);
			pointer=pointer->lchild;
		}else{
			pointer=st.top();
			cout<<pointer->data<<" ";
			pointer=pointer->rchild;
			st.pop();
		}
	}
}//中序遍历(非递归)

//void PostOrder(BiTree *root){
//	BiNode *St[MAXSIZE],*q=root;
//	BiNode *p;
//	int flag,top=-1;
//	if(q!=NULL){
//		do{
//			while(q!=NULL){
//				top++;
//				St[top]=q;
//				q=q->lchild;
//			}
//			p=NULL;
//			flag=1;
//			while(top!=-1&&flag){
//				q=St[top];
//				if(q->rchild==p){
//					cout<<q->data<<"\t";
//					top--;
//					p=q;
//				}else{
//					q=q->rchild;
//					flag=0;
//				}
//			}
//		}while(top!=-1); 
//		cout<<endl;
//	}
//}//后序遍历(非递归)

void PostOrder(BiTree* root){
	BiTree* temp;
	stack<BiTree*>st1;
	stack<BiTree*>st2;
	st1.push(root);
	while(!st1.empty()){
		temp=st1.top();
		st1.pop();
		st2.push(temp);
		if(temp->lchild){
			st1.push(temp->lchild);
		}
		if(temp->rchild){
			st1.push(temp->rchild);
		}
	}
	while(!st2.empty()){
		cout<<st2.top()->data<<" ";
		st2.pop();
	}
} //后序遍历(非递归)

//后序
//void PostOrder(BiTree* root){
//	stack<BiTree*>st;
//	BiTree* p=root;
//	BiTree* q=root;
//	while(p){
//		while(p->lchild){
//			st.push(p);
//			p=p->lchild;
//		} 
//		while(!p->rchild||p->rchild==q){
//			cout<<p->data<<" ";
//			q=p;
//			if(st.empty()){
//				return ;
//			}
//			p=st.top();
//			st.pop();
//		}
//		st.push(p);
//		p=p->rchild;
//	}
//} 

BiTree *PreInBuild(const char *pre,const char *in,int size){
	if(size<=0)return NULL;
	int root_value=0;
	for(root_value=0;root_value<size;root_value++){
		if(in[root_value]==pre[0]){
			break;
		}
	}
	if(root_value==size){
		cout<<"先序遍历和中序遍历不匹配"<<endl;
		return NULL;
	}
	BiTree *root=new BiTree;
	root->data=pre[0];
	root->lchild=PreInBuild(pre+1,in,root_value);
	root->rchild=PreInBuild(pre+root_value+1,in+root_value+1,size-root_value-1);
	return root;
}//先序中序求树

BiTree *InPostBuild(const char *in,const char *post,int size){
	if(size<=0)return NULL;
	int root_value=0;
	for(root_value=0;root_value<size;root_value++){
		if(in[root_value]==post[size-1]){
			break;
		} 
	}
	BiTree *root=new BiTree;
	root->data=post[size-1];
	root->lchild=InPostBuild(in,post,root_value);
	root->rchild=InPostBuild(in+root_value+1,post+root_value,size-1-root_value);
	return root; 
}//中序后序求树

bool searchBNode(BiTree *root,char c){
	if(root==NULL){
		return false;
	}
	if(root->data==c){
		return true;
	}
	return searchBNode(root->lchild,c)||searchBNode(root->rchild,c);
}//查找

void ChangePos(BiTree *root){
	if(root==NULL){
		return;
	}
	BiTree *temp;
	temp=root->lchild;
	root->lchild=root->rchild;
	root->rchild=temp;
	ChangePos(root->lchild);
	ChangePos(root->rchild);
}//交换左右子树 

void Postchange(BiTree *root){
	if(root!=NULL){
		return;
	}
	cout<<root->data<<" ";
	Postchange(root->rchild);
	Postchange(root->lchild);
}//反序输出后序 

int main(){
	BiTree *root,*T;
	root=CreateBiTree(root);
	//PreOrderTraverse(root);
	//InOrderTraverse(root);
	//PostOrderTraverse(root);
	//Levelorder(root);
	//cout<<GetNode(root)<<endl;
	//cout<<LeadCount(root)<<endl;
	//PreOrder(root);
	//InOrder(root);
	//PostOrder(root);
	//T=CopyTree(root,T);
	/*T=PreInBuild("ABDECFG","DBEAFCG",7);
	PostOrder(T);*/
	/*T=InPostBuild("DBEAFCG","DEBFGCA",7);
	PreOrder(T);*/
	//cout<<searchBNode(root,'D')<<endl;
	/*Levelorder(root);
	ChangePos(root);
	cout<<endl;
	Levelorder(root);*/
	//Postchange(root); 
} 

二叉树的一些递归算法

#include<iostream>
#include<string>
using namespace std;
#define MAXSIZE 20

typedef char ElemType;
typedef struct BiNode{
	ElemType data;
	BiNode *lchild;
	BiNode *rchild;
}BiTree;

BiTree* CreateBiTree(BiTree *root){
	char ch;
	cout<<"输入节点值:"<<endl;
	cin>>ch;	
	if(ch=='#'){
		root=NULL;
		return root;
	}else{
		root=new BiTree;
		root->data=ch;
		root->lchild=CreateBiTree(root->lchild);
		root->rchild=CreateBiTree(root->rchild);
	}
}//创建二叉树 (此创建对应先序序列)

int LeadCount(BiTree *root){
	if(root==NULL){
		return 0;
	}
	if(root->lchild==NULL&&root->rchild==NULL){
		return 1;
	}else{
		return LeadCount(root->lchild)+LeadCount(root->rchild);
	}
}//计算二叉树叶子结点数 

int Count1(BiTree *root){
	if(!root){
		return 0;
	}
	if(root->lchild&&!root->rchild){
		return 1+Count1(root->lchild);
	}else if(root->rchild&&!root->lchild){
		return 1+Count1(root->rchild);
	}else{
		return Count1(root->lchild)+Count1(root->rchild);
	}
} //计算度为1的结点数

int Count2(BiTree *root){
	if(!root){
		return 0;
	}	
	if(root->lchild&&root->rchild){
		return Count2(root->lchild)+Count2(root->rchild)+1;
	}else{
		return Count2(root->lchild)+Count2(root->rchild);
	}
} //计算度为2的结点数

int TreeHeight(BiTree *root){
	int left=0,right=0;
	if(!root){
		return 0;
	}else{
		left=TreeHeight(root->lchild);
		right=TreeHeight(root->rchild);
		if(left>=right)return left+1;
		if(left<right)return right+1;
	}
}//二叉树高度 

char MaxBiNode(BiTree *root){
	if(!root){
		return '0';
	}else{
		char maxl=MaxBiNode(root->lchild);
		char maxr=MaxBiNode(root->rchild);
		char max=maxl>maxr?maxl:maxr;
		return max>root->data?max:root->data;
	}
}//结点最大值

int TreeLevelSize(BiTree *root,int n){
	if((!root)||n<1){
		return 0;
	}
	if(n==1){
		return 1;
	}else{
		return TreeLevelSize(root->lchild,n-1)+TreeLevelSize(root->rchild,n-1);
	}
} //二叉树某层宽度 

int TreeMaxSize(BiTree *root,int n){
	int max=0;
	for(int i=1;i<=n;i++){
		max=max>TreeLevelSize(root,i)?max:TreeLevelSize(root,i);
	}
	return max;
}//二叉树最大宽度 

void ChangePos(BiTree *root){
	if(!root){
		return;
	}
	BiTree* temp;
	temp=root->lchild;
	root->lchild=root->rchild;
	root->rchild=temp;
	ChangePos(root->lchild);
	ChangePos(root->rchild); 
}//交换每个节点的左右孩子 

void DelLeafNode(BiTree *root){
	if(!root){
		return;
	}
	if(root->lchild){
		BiTree *temp=root->lchild;
		if(temp->lchild==NULL&&temp->rchild==NULL){
			delete temp;
			root->lchild=NULL;
		}
	}
	if(root->rchild){
		BiTree *temp=root->rchild;
		if(temp->lchild==NULL&&temp->rchild==NULL){
			delete temp;
			root->rchild=NULL;
		}
	}
	DelLeafNode(root->lchild);
	DelLeafNode(root->rchild);
//	if(root->lchild==NULL&&root->rchild==NULL){
//		delete root;
//		return;
//	}else{
//		DelLeafNode(root->lchild);
//		DelLeafNode(root->rchild);
//	}
}//删除所有叶子结点 

void PreOrderTraverse(BiTree *root){
	if(root){
		cout<<root->data<<endl;
		PreOrderTraverse(root->lchild);
		PreOrderTraverse(root->rchild);
	}else{
		return;
	}
}//先序遍历(递归)

int main(){
	BiTree *root;
	root=CreateBiTree(root);
	PreOrderTraverse(root);
//	cout<<LeadCount(root)<<endl;
//	cout<<Count1(root)<<endl;
//	cout<<Count2(root)<<endl;
//	cout<<TreeHeight(root)<<endl;
//	ChangePos(root);
//	DelLeafNode(root);
//	PreOrderTraverse(root);
//	cout<<MaxBiNode(root)<<endl;
//	cout<<TreeLevelSize(root,3)<<endl;
	cout<<TreeMaxSize(root,3)<<endl;
} 

判断是否为完全二叉树

#include<iostream>
#include<queue> 
using namespace std;
#define MAXSIZE 20

typedef char ElemType;
typedef struct BiNode{
	ElemType data;
	BiNode *lchild;
	BiNode *rchild;
}BiTree;

BiTree* CreateBiTree(BiTree *root){
	char ch;
	cout<<"输入节点值:"<<endl;
	cin>>ch;	
	if(ch=='#'){
		root=NULL;
		return root;
	}else{
		root=new BiTree;
		root->data=ch;
		root->lchild=CreateBiTree(root->lchild);
		root->rchild=CreateBiTree(root->rchild);
	}
}//创建二叉树 (此创建对应先序序列)

bool IsCompleteTree1(BiTree *root){
	if(!root){
		return true;
	}
	queue<BiNode*> temp;
	temp.push(root);
	bool flag = false;
	while(!temp.empty()){
		BiNode* node=temp.front();
		temp.pop();
		if(node==NULL){
			flag=true;
			continue;
		}
		if(flag){
			return false;
		}
		temp.push(node->lchild);
		temp.push(node->rchild);
	}
	return true;
}

bool IsCompleteTree2(BiTree *root){
	if(!root){
		return true;
	}
	queue<BiTree*>q;
	bool flag=true;
	q.push(root);
	while(!q.empty()){
		BiTree* temp=q.front();
		if(flag){
			if(temp->lchild&&temp->rchild){
				q.push(temp->lchild);
				q.push(temp->rchild);
			}else if(temp->lchild&&!temp->rchild){
				flag=false;
				q.push(temp->lchild);
			}else if(!temp->lchild&&temp->rchild){
				return false;
			}else{
				flag=false;
			}
		}else{
			if(!(!temp->lchild&&!temp->rchild)){
				return false;
			}
		}
		q.pop();
	}
	return true;
}

int main()
{
	BiTree *root;
	root=CreateBiTree(root);
	cout<<IsCompleteTree1(root)<<endl;
	cout<<IsCompleteTree2(root)<<endl;
}

二叉搜索树

#include<iostream>
using namespace std;

class BinarySearchTree{
	private:
		int data;
		BinarySearchTree *lchild;
		BinarySearchTree *rchild;
	public:
		//插入函数 
		BinarySearchTree* Insert(BinarySearchTree* BST,int data){
			if(!BST){
				BST=new BinarySearchTree;
				BST->data=data;
				BST->lchild=BST->rchild=NULL;
			}else{
				if(data<BST->data){
					BST->lchild=BST->Insert(BST->lchild,data);
				}else if(data>BST->data){
					BST->rchild=BST->Insert(BST->rchild,data);
				}
			}
			return BST;
		}
		//创建二叉搜索树 
		BinarySearchTree* Create(int* data,int size){
			BinarySearchTree* bst=NULL;
			for(int i=0;i<size;i++){
				bst=bst->Insert(bst,data[i]);
			}
			return bst;
		}
		//按值查找
		BinarySearchTree* Find(BinarySearchTree* BST,int data){
			BinarySearchTree* root=BST;
			if(!root){
				return NULL;
			}
			while(root){
				if(root->data==data){
					return root;
				}else if(root->data>data){
					root=root->lchild;
				}else{
					root=root->rchild;
				}
			}
			return NULL;
		}
		//删除最小值
		BinarySearchTree* DeleteMin(BinarySearchTree* BST){
			BinarySearchTree* root=BST,*parent=BST;
			if(!root){
				return NULL;
			}
			if(!root->lchild&&!root->rchild){
				delete root;
				return NULL;
			}
			while(root->lchild){
				parent=root;
				root=root->lchild;
			}
			if(!root->lchild){
				parent->lchild=NULL;
				delete root;
			}
			return BST;
		} 
		
		//删除最大值
		BinarySearchTree* DeleteMAX(BinarySearchTree* BST){
			BinarySearchTree* root=BST,*parent=BST;
			if(!root){
				return NULL;
			}
			if(!root->lchild&&!root->rchild){
				delete root;
				return NULL;
			}
			while(root->rchild){
				parent=root;
				root=root->rchild;
			}
			if(!root->rchild){
				parent->rchild=NULL;
				delete root;
			}
			return BST;
		} 
		
		//中序遍历
		void InorderTraversal(BinarySearchTree* BST){
			if(!BST){
				return;
			}
			BST->InorderTraversal(BST->lchild);
			cout<<BST->data<<" ";
			BST->InorderTraversal(BST->rchild);
		}
		
		//删除结点
		//合并删除
		BinarySearchTree* DeleteByMerging(BinarySearchTree *BST,int data){
			if(!BST){//树空时,直接返回NULL 
				return BST;
			}else if(data < BST->data){
				//data小于根节点时,到左子树去删除data 
				BST->lchild = this->DeleteByMerging(BST->lchild,data);
			}else if(data > BST->data){
				//data大于根节点时,到右子树去删除data 
				BST->rchild = this->DeleteByMerging(BST->rchild,data); 
			}else{
				BinarySearchTree* temp=BST;
				if(BST){
					if(!BST->lchild){
						BST=BST->rchild;
					}else if(!BST->rchild){
						BST=BST->lchild;
					}else{
						temp=BST->lchild;
						while(temp->rchild){
							temp=temp->rchild;
						}
						temp->rchild=BST->rchild;
						temp=BST;
						BST=BST->lchild;
					}
					delete temp;
				}
				return BST;
			}
			
		}
	 	//复制删除 
		BinarySearchTree* DeleteByCopying(BinarySearchTree* BST,int data){
			if(!BST){//树空时,直接返回NULL 
				return BST;
			}else if(data < BST->data){
				//data小于根节点时,到左子树去删除data 
				BST->lchild = this->DeleteByCopying(BST->lchild,data);
			}else if(data > BST->data){
				//data大于根节点时,到右子树去删除data 
				BST->rchild = this->DeleteByCopying(BST->rchild,data); 
			}else{//data等于根节点时 
				if(BST->lchild && BST->rchild){
					//左右子树都不空时,用右子树的最小来代替根节点
					BinarySearchTree* tmp = BST->rchild,*pre=BST;
					while(tmp->lchild){
						pre=tmp;
						tmp=tmp->lchild;
					}
					BST->data=tmp->data;
					if(pre==BST){
						pre->rchild=tmp->rchild;
					}else{
						pre->lchild=tmp->rchild;
					}
					delete tmp;
				}else{//当左右子树都为空或者有一个空时 
					BinarySearchTree* tmp = BST;
					if(!BST->lchild){//左子树为空时 
						BST = BST->rchild;
					}else if(!BST->rchild){//右子树为空时 
						BST = BST->lchild; 
					}
					delete tmp; 
				}
			}
			return BST;
		} 
		
};

int main(){
	int size;
	cin>>size;
	int *data=new int[size];
	for(int i=0;i<size;i++){
		cin>>data[i];
	}
	BinarySearchTree* BST=new BinarySearchTree;
	BST=BST->Create(data,size);
//	BST=BST->DeleteMAX(BST); 
	BST=BST->DeleteByMerging(BST,6);
	BST=BST->DeleteByCopying(BST,6);
	BST->InorderTraversal(BST);
}

最大堆

#include<iostream>
using namespace std;

class MaxHeap{
	private:
		int *data;
		int size;//当前规模
		int capacity;//最大容量
	public:
		MaxHeap(int MaxSize){
			this->data=new int[MaxSize];
			this->size=0;
			this->capacity=MaxSize;
		} 
		//将下标为n的元素调节成最大堆 ,辅助功能 
		void Predown(int n){
			int x=data[n];
			int parent,child;
			for(parent=n;parent*2<=this->size;parent=child){
				child=parent*2;
				if((child!=this->size)&&this->data[child]<this->data[child+1]){
					child++;
				}
				if(x>this->data[child]){
					break;
				}else{
					this->data[parent]=this->data[child];
				}
			}
			this->data[parent]=x;
		}
		//创建最大堆
		void Create(int *data,int n){
			for(int i=0;i<n;i++){
				this->data[++size]=data[i];
			}
			for(int i=size/2;i>0;i--){
				this->Predown(i);
			}
		} 
		//判满
		bool IsFull(){
			return this->size==this->capacity;
		}
		//判空
		bool IsEmpty(){
			return this->size==0;
		}
		//插入操作
		bool Insert(int num){
			if(this->IsFull()){
				cout<<"堆已满,插入失败"<<endl;
				return false;
			} 
			int i=++size;
			for(;this->data[i/2]<num&&i>0;i/=2){
				this->data[i]=this->data[i/2];
			}
			this->data[i]=num;
			return true;
		}
		//删除最大值操作
		int Delete(){
			if(this->IsEmpty()){
				cout<<"堆已空,删除失败"<<endl;
				return 10000;
			} 
			int max=this->data[1];
			this->data[1]=this->data[size];
			this->size--;
			Predown(1);
			return max;
		} 
		//打印最大堆
		void Print(){
			for(int i=1;i<=size;i++){
				cout<<this->data[i]<<" ";
			}
			cout<<endl;
		} 
};

int main(){
	int capacity,size,num;
	int *data;
	cout<<"输入最大堆容量:"<<endl;
	cin>>capacity;
	MaxHeap maxheap(capacity);
	cout<<"输入初始化最大堆元素个数:"<<endl;
	cin>>size;
	data=new int[size];
	cout<<"初始化元素:"<<endl;
	for(int i=0;i<size;i++){
		cin>>data[i];
	}
	maxheap.Create(data,size);
	cout<<"最大堆为:"<<endl;
	maxheap.Print();
	cout<<"输入要插入的数:"<<endl;
	cin>>num;
	maxheap.Insert(num);
	cout<<"进行删除操作"<<endl;
	num=maxheap.Delete();
	cout<<"删除的元素为:"<<num<<endl;
	cout<<"最大堆为:"<<endl;
	maxheap.Print();
	
	return 0;
}

哈夫曼编码解码树

#include<bits/stdc++.h>
using namespace std;
//首先是一个树
struct TreeNode{
	int weight;
	int parent;
	int left;
	int right;
}; 
//编码表中元素
struct TreeCode{
	char data;
	string code;
};
class Huffman{
	private:
		int N;//叶子节点数量 
		TreeCode* CodeTable;//存储编码表
		TreeNode* HTree;//哈夫曼树 
	public:
		Huffman(char ch){
			InitHuffman(ch);
		}//有参构造函数
		void InitHuffman(char ch){//统计各字符数量,同时建立哈夫曼树和编码表 
			int n=0;
			int count[127]={0};//存储各字符在输入字符串中的数量
			int temp[127]={0};//将分散在各处的count对应的字符从0依次排列,便于使用,后续作为权重 
			char save[127];//保存输入的字符串中不同的字符
			while(ch!='\n'){
				count[int(ch)]++;
				ch=cin.get(); 
			} 
			for(int i=0;i<127;i++){
				if(count[i]!=0){
					save[n]=(char)i;
					temp[n]=count[i];
					n++;
				}
			}
			N=n;
			HTree=new TreeNode[2*N-1];
			CodeTable=new TreeCode[N];
			for(int i=0;i<N;i++){
				HTree[i].weight=temp[i];
				HTree[i].right=HTree[i].left=-1;
				HTree[i].parent=-1;
				CodeTable[i].data=save[i];
			}//初始化哈夫曼树和编码表的各字符
			int s1=0,s2=0;
			for(int i=N;i<2*N-1;i++){
				SelectMin(s1,s2,HTree,i);
				HTree[s1].parent=HTree[s2].parent=i;
				HTree[i].weight=HTree[s1].weight+HTree[s2].weight;
				HTree[i].left=s1;
				HTree[i].right=s2;
				HTree[i].parent=-1;
			}
			Code(2*N-2,"");//建立编码表 
		} 
		void SelectMin(int &s1,int &s2,TreeNode* HT,int n){
			int min=10000;
			for(int i=0;i<n;i++){
				if(HT[i].parent==-1&&HT[i].weight<min){//HT[i].parent==-1是为了筛选掉非叶节点 
					min=HT[i].weight;
					s1=i;
				}
			}
			min=10000;
			for(int i=0;i<n;i++){
				if(HT[i].parent==-1&&HT[i].weight<min&&i!=s1){
					min=HT[i].weight;
					s2=i;
				}
			}
		}
		void Code(int i,string str){//根据编码表对输入的字符串编码,并将编码后的字符串输出
			if(HTree[i].left==-1){
				CodeTable[i].code=str;
				cout<<CodeTable[i].data<<"的编码为:"<<CodeTable[i].code<<endl; 
			}else{
				Code(HTree[i].left,str+'0');
				Code(HTree[i].right,str+'1');
			}
		} 
		void CreateCodeTable(){//生成编码表 
			Code(2*N-2,"");
		}
		void EnCoding(char *s,string str){//根据编码表对输入的字符串进行编码,并将编码后的字符串输出 
			while(*s!='\0'){
				for(int i=0;i<N;i++){
					if(*s==CodeTable[i].data){
						str=CodeTable[i].code;
						cout<<str;
						break;
					}
				}
				s++;
			}
		}
		void DeCoding(char* s,char* str){//利用哈夫曼树对编码后的字符串进行译码,并输出译码结果 
			while(*s!='\0'){
				int parent=2*N-2;
				while(HTree[parent].left!=-1){
					if(*s=='0'){
						parent=HTree[parent].left;
					}else{
						parent=HTree[parent].right;
					}
					s++;//不断将要译码的二进制数向前推进,直到恰好读出一个字符,结束此层循环 
				}
				*str=CodeTable[parent].data;
				cout<<*str;//输出此时的字符并通过将str地址后移为下一个字符存储做铺垫 
				str++;
			} 
		}
		double Compare(){
			double newweight=0;
			double oldweight=0;
			for(int i=0;i<N;i++){
				newweight+=HTree[i].weight*(CodeTable[i].code.length());
				oldweight+=HTree[i].weight*8;
			}
			return oldweight/newweight;
		}
		~Huffman(){
			delete []HTree;
			delete []CodeTable;
		}
}; 

int main(){
	cout<<"输入字符串:"<<endl;
	char c=cin.get();
	Huffman ht(c);
	while(true){
		cout<<"*************菜单**************"<<endl; 
		cout<<"1.编码,2.解码,3.查看压缩效率,4.退出"<<endl;
		cin>>c;
		if(c=='1'){
			char ch[100];
			cin>>ch;
			string str;
			ht.EnCoding(ch,str);
			cout<<endl;
		}else if(c=='2'){
			char ch[200];
			cin>>ch;
			char str[200];
			ht.DeCoding(ch,str);
			cout<<endl;
		}else if(c=='3'){
			cout<<ht.Compare()<<endl;
		}else{
			return 0;
		}
	}
}

AVL

#include<iostream>
#include<stack>
using namespace std;

class AVL{
	private:
		int data;
		int height;
		AVL* lchild;
		AVL* rchild;
	public:
		//最小值查找
		AVL* FindMin(AVL* avl){
			AVL* root=avl;
			if(root==NULL){
				return NULL;
			}
			while(root){
				if(!root->lchild){
					return root;
				}else{
					root=root->lchild;
				}
			}
		} 
		//最大值查找
		AVL* FindMax(AVL* avl){
			AVL* root=avl;
			if(root==NULL){
				return NULL;
			}
			while(root){
				if(!root->rchild){
					return root;
				}else{
					root=root->rchild;
				}
			}
		} 
		//插入函数
		AVL* Insert(AVL* avl,int data){
			if(!avl){
				avl=new AVL;
				avl->data=data;
				avl->height=0;
				avl->lchild=avl->rchild=NULL;
			}else if(data<avl->data){
				avl->lchild=avl->Insert(avl->lchild,data);
				int rheight=this->getHeight(avl->rchild);
				int lheight=this->getHeight(avl->lchild);
				if(lheight-rheight==2){
					if(data<avl->lchild->data){
						avl=this->SingleLeftRotation(avl);
					}else{
						avl=this->DoubleLeftRightRotation(avl);
					}
				}
			}else if(data>avl->data){
				avl->rchild=avl->Insert(avl->rchild,data);
				int rheight=this->getHeight(avl->rchild);
				int lheight=this->getHeight(avl->lchild);
				if(rheight-lheight==2){
					if(data>avl->rchild->data){
						avl=this->SingleRightRotation(avl);
					}else{
						avl=this->DoubleRightLeftRotation(avl);
					}
				}
			}
			avl->height=this->getHeight(avl->lchild)>this->getHeight(avl->rchild)?this->getHeight(avl->lchild)+1:this->getHeight(avl->rchild)+1;
			return avl; 
		}
		//利用数组创建二叉平衡树
		AVL* CreateAVL(int *data,int size){
			AVL* avl=NULL;
			for(int i=0;i<size;i++){
				avl=this->Insert(avl,data[i]);
			} 
			return avl;
		} 
		//删除操作
		AVL* Delete(AVL* avl,int data){
			if(!avl){
				return NULL;
			}else if(data<avl->data){
				avl->lchild=this->Delete(avl->lchild,data);
				int rheight=this->getHeight(avl->rchild);
				int lheight=this->getHeight(avl->lchild);
				if(rheight-lheight==2){
					if(data>avl->rchild->data){
						avl=this->SingleRightRotation(avl);
					}else{
						avl=this->DoubleRightLeftRotation(avl);
					}
				}
			}else if(data>avl->data){
				avl->rchild=this->Delete(avl->rchild,data);
				int rheight=this->getHeight(avl->rchild);
				int lheight=this->getHeight(avl->lchild);
				if(lheight-rheight==2){
					if(data<avl->lchild->data){
						avl=this->SingleLeftRotation(avl);
					}else{
						avl=this->DoubleLeftRightRotation(avl);
					}
				}
			}else{
				if(avl->lchild&&avl->rchild){
					AVL* temp=this->FindMin(avl->rchild);
					avl->data=temp->data;
					avl->rchild=this->Delete(avl->rchild,temp->data);
				}else{
					AVL* temp=avl;
					if(!avl->lchild){
						avl=avl->rchild;
					}else if(!avl->rchild){
						avl=avl->lchild;
					}
					delete temp;
				}
			}
			return avl; 
		} 
		//左单旋转
		AVL* SingleLeftRotation(AVL* avl){
			AVL* temp=avl->lchild;
			avl->lchild=temp->rchild;
			temp->rchild=avl;
			avl->height=this->getHeight(avl->lchild)>this->getHeight(avl->rchild)?this->getHeight(avl->lchild)+1:this->getHeight(avl->rchild)+1;
			temp->height=this->getHeight(temp->lchild)>this->getHeight(temp->rchild)?this->getHeight(temp->lchild)+1:this->getHeight(temp->rchild)+1;
			return temp;
		} 
		//右单旋转
		AVL* SingleRightRotation(AVL* avl){
			AVL* temp=avl->rchild;
			avl->rchild=temp->lchild;
			temp->lchild=avl;
			avl->height=this->getHeight(avl->lchild)>this->getHeight(avl->rchild)?this->getHeight(avl->lchild)+1:this->getHeight(avl->rchild)+1;
			temp->height=this->getHeight(temp->lchild)>this->getHeight(temp->rchild)?this->getHeight(temp->lchild)+1:this->getHeight(temp->rchild)+1;
			return temp;
		}
		//左右旋转
		AVL* DoubleLeftRightRotation(AVL* avl){
			avl->lchild=this->SingleRightRotation(avl->lchild);
			return this->SingleLeftRotation(avl);
		}
		//右左旋转 
		AVL* DoubleRightLeftRotation(AVL* avl){
			avl->rchild=this->SingleLeftRotation(avl->rchild);
			return this->SingleRightRotation(avl);
		}
		//获取树的高度
		int getHeight(AVL* avl){
			if(!avl){
				return 0;
			}
			return avl->height;
		} 
		//前序遍历(非递归)
		void PreOrder(AVL* avl){
			if(!avl){
				return;
			}
			stack<AVL*>st;
			AVL* pointer=avl;
			while(!st.empty()||pointer){
				if(pointer){
					cout<<pointer->data<<" ";
					if(pointer->rchild){
						st.push(pointer->rchild);
					} 
					pointer=pointer->lchild;
				}else{
					pointer=st.top();
					st.pop();
				}
			}
		} 
		//中序遍历(非递归)
		void InOrder(AVL* avl){
			stack<AVL*>st;
			AVL* pointer=avl;
			while(!st.empty()||pointer){
				if(pointer){
					st.push(pointer);
					pointer=pointer->lchild;
				}else{
					pointer=st.top();
					cout<<pointer->data<<" ";
					st.pop();
					pointer=pointer->rchild;
				}
			}
		} 
		//后序遍历
		void PostOrder(AVL* avl){
			AVL* temp;
			stack<AVL*>st1;
			stack<AVL*>st2;
			st1.push(avl);
			while(!st1.empty()){
				temp=st1.top();
				st1.pop();
				st2.push(temp);
				if(temp->lchild){
					st1.push(temp->lchild);
				}
				if(temp->rchild){
					st1.push(temp->rchild);
				}
			}
			while(!st2.empty()){
				cout<<st2.top()->data<<" ";
				st2.pop();
			}
		} 
};

int main(){
	int size;
	cout<<"请输入结点个数:"<<endl; 
	cin>>size;
	int* data;
	data = new int[size];
	cout<<"请输入每个结点的值:"<<endl;
	for(int i = 0 ; i < size ; i++){
		cin>>data[i];
	}
	AVL* avl;
	avl = new AVL;
	avl = avl->CreateAVL(data,size);
	cout<<"前序遍历(非递归):"<<endl;
	avl->PreOrder(avl);
	cout<<endl;
	
	cout<<"中序遍历(非递归):"<<endl;
	avl->InOrder(avl);
	cout<<endl;
	
	cout<<"后序遍历(非递归):"<<endl;
	avl->PostOrder(avl);
	cout<<endl;
//	
//	int num;
//	cout<<"请输入要删除的结点:"<<endl;
//	cin>>num;
//	avl = avl->Delete(avl,num);
//	cout<<"删除之后:"<<endl;
//	cout<<"前序遍历(非递归):"<<endl;
//	avl->PreOrder(avl);
//	cout<<endl;
//	
//	cout<<"中序遍历(非递归):"<<endl;
//	avl->InOrder(avl);
//	cout<<endl;
//	
//	cout<<"请输入要删除的结点:"<<endl;
//	cin>>num;
//	avl = avl->Delete(avl,num);
//	cout<<"删除之后:"<<endl;
//	cout<<"前序遍历(非递归):"<<endl;
//	avl->PreOrder(avl);
//	cout<<endl;
//	
//	cout<<"中序遍历(非递归):"<<endl;
//	avl->InOrder(avl);
//	cout<<endl;
//	
//	cout<<"请输入要删除的结点:"<<endl;
//	cin>>num;
//	avl = avl->Delete(avl,num);
//	cout<<"删除之后:"<<endl;
//	cout<<"前序遍历(非递归):"<<endl;
//	avl->PreOrder(avl);
//	cout<<endl;
//	
//	cout<<"中序遍历(非递归):"<<endl;
//	avl->InOrder(avl);
//	cout<<endl;
//	
//	cout<<"请输入要删除的结点:"<<endl;
//	cin>>num;
//	avl = avl->Delete(avl,num);
//	cout<<"删除之后:"<<endl;
//	cout<<"前序遍历(非递归):"<<endl;
//	avl->PreOrder(avl);
//	cout<<endl;
//	
//	cout<<"中序遍历(非递归):"<<endl;
//	avl->InOrder(avl);
//	cout<<endl;
//	
//	cout<<"后序遍历(非递归):"<<endl;
//	avl->PostOrder(avl);
//	cout<<endl;
	
//	cout<<"请输入要插入的结点:"<<endl;
//	cin>>num;
//	avl = avl->Insert(avl,num);
//	cout<<"插入之后:"<<endl;
//	cout<<"前序遍历(非递归):"<<endl;
//	avl->PreOrder(avl);
//	cout<<endl;
//	
//	cout<<"中序遍历(非递归):"<<endl;
//	avl->InOrder(avl);
//	cout<<endl;
//	
//	cout<<"后序遍历(非递归):"<<endl;
//	avl->PostOrder(avl);
//	cout<<endl;
	return 0;
}

树这一章挺难的呜呜,不过下一章图更难,啊啊啊啊,不过我还是会努力学习,持续更新哒,加油!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

残夜.寒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值