실험 "데이터 구조"산동 대학 식스 : 힙 및 트리 검색

Liutui 실험 및 검색 트리

첫째, 실험의 목적

힙 마스터 검색 트리, 삽입, 삭제 방법의 기본 개념.

둘째, 실험 내용

1 0 (0은 제외) 만남의 입력단이고, 영이 아닌 (최대 20까지) 양수 일련의 입력.
최대 힙 초기화 방법을 이용하여 생성 2. 상기 입력 데이터 시퀀스는, 최대 스택의 다음 출력 레벨 (최대 힙을 만드는 방법 노드로 연속적으로 삽입하지 않음).
3 힙 정렬 정렬 결과 출력.
(4) 상기 데이터 입력 (키 반복 키의 삽입을 반복하지 경우 고유해야) 이진 검색 트리를 작성 순서 시퀀스에서 이진 검색 트리의 프리앰블 시퀀스의 출력 (출력 분기) .

다음과 같이 코드입니다 :

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<string.h> 
#include<queue>
using namespace std;
int a[25],b[25],n;
int &size=n;
int count=1;
//定义最大堆类
class maxheap{
public:
	int heap[25];//需要初始化数组空间大小,否则容易出错
	int heapsize;
	void init();
	int top(){return heap[1];}//返回最大元素的值
	void pop();
};
void maxheap::init(){//根据输入的数组元素初始化最大堆
	for(int i=1;i<=n;i++) heap[i]=a[i];
	heapsize=n;
	for(int root=heapsize/2;root>=1;root--){
		int rootElement=heap[root];
		int child=2*root;
		while(child<=heapsize){
			if(child<heapsize&&heap[child]<heap[child+1])child++;
			if(rootElement>=heap[child])break;
			heap[child/2]=heap[child];
			child*=2;
		}
		heap[child/2]=rootElement;
	}
}
//删除最大的元素,重新排列最大堆
void maxheap::pop(){
	int lastElement=heap[heapsize--];
	int cur=1,child=2;//从根开始
	while(child<=heapsize){
		if(child<heapsize&&heap[child]<heap[child+1])child++;
		if(lastElement>=heap[child])break; 
		heap[cur]=heap[child];
		cur=child;
		child*=2;
	} 
	heap[cur]=lastElement;
} 
//输出最大堆的层次序列
void print(int a[],int n){	for(int i=1;i<n;i++)
		cout<<a[i]<<",";
	cout<<a[n]<<endl;
}
//堆排序
void heapsort(const int a[],int b[],int n){	maxheap mh;
	mh.init();
	for(int i=n;i>=1;i--){
		b[i]=mh.top();
		mh.pop();
	}
	maxheap mmh;
	mh=mmh;
}
//定义二叉搜索树类
class BStreeNode{public:
	int element;
	BStreeNode *leftchild,*rightchild;
	BStreeNode(){leftchild=rightchild=NULL;}
	BStreeNode(int& theE,BStreeNode* theleft=NULL,BStreeNode*theright=NULL){
		element=theE;
		leftchild=theleft;
		rightchild=theright;
	}	
};
//按节点依次插入的方法创建二叉搜索树
void insert(BStreeNode *root,int num){
	BStreeNode *p=root;
	BStreeNode *prep;
	while(p!=NULL){
		if(p->element==num){
			size--;return;
		}
		prep=p;
		if(num>p->element){p=p->rightchild;}
		else if(num<p->element){p=p->leftchild;}
	}
	BStreeNode *np=new BStreeNode(num);
	if(root!=NULL){		
		if(num<prep->element){prep->leftchild=np;} 
		else if(num>prep->element){prep->rightchild=np;} 
	}
	else root=np;
	return;
}
void pre(BStreeNode* t){//前序序列输出
	if(t!=NULL){
		if(count!=size) cout<<t->element<<",";	
		else cout<<t->element<<endl;
		count++;
		pre(t->leftchild);
		pre(t->rightchild);
	}
}
void in(BStreeNode* t){//中序序列输出
	if(t!=NULL){
		in(t->leftchild);
		if(count!=size) cout<<t->element<<",";	
		else cout<<t->element<<endl;
		count++;
		in(t->rightchild);
	}
}
int main(){
	cout<<"Input"<<endl;
	for(n=1;cin>>a[n]&&a[n]!=0;n++);
	n--;
	cout<<"Output"<<endl;
	maxheap mh;
	mh.init();
	print(mh.heap,mh.heapsize);
	heapsort(a,b,n);
	print(b,n);
	BStreeNode *bst=new BStreeNode(a[1]);
	for(int i=2;i<=n;i++) {
		insert(bst,a[i]);
	}
	pre(bst);count=1;in(bst);
	cout<<"End"<<endl;
	return 0;
} 

결론 분석 및 경험 :

复习了最大堆和二叉搜索树相关的知识点,并通过实验得出类内成员数组需要初始化数组的空间大小,否则容易出错的结论,解决了逻辑正确但输出错误的问题。

게시 11 개 원래 기사 · 원의 칭찬 0 · 조회수 33

추천

출처blog.csdn.net/weixin_43959421/article/details/103974395