Experimental data structure REPORT

1. binary linked list structure for storage, achieve the creation of a binary tree traversal

1) Problem Description: A simple design in the main program menu, respectively calling the corresponding function feature:

       1 ... build tree

       2 ... preorder traversal tree

       3 ... in order (non-recursive) traversal of the tree

4 ... postorder tree

       0 ... end

2) Experimental requirements: The following function is defined in the program, and to realize the function of the functional requirements: 

CreateTree (): according to the preamble sequence entered from the keyboard, create tree

    PreOrderTree (): preorder traversal tree (recursive)

    InOrderTree (): the sequence (non-recursive) traversal of the tree

    LaOrderTree (): postorder tree (recursive)

3) attention to the problem:

   Note the steps to understand the recursive algorithm.

   Note that the character type of the input data processing.

   Important to understand how to use the stack structure to achieve a non-recursive algorithm

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct BiTNode{
	int data;
	BiTNode* lchild,*rchild;
};
typedef BiTNode* BiTree;

int STree(BiTree r,int a,int b){
	int flag;
	if(r == NULL) return 0;
    if(r->data == a){
    	if(r->lchild == NULL){
    		r->lchild = (BiTNode*)malloc(sizeof(BiTNode));
    		r->lchild->data = b;
			r->lchild->lchild = NULL;
			r->lchild->rchild = NULL;
			return 1; 
		}
		else if(r->rchild == NULL){
			r->rchild = (BiTNode*)malloc(sizeof(BiTNode));
			r->rchild->data = b;
			r->rchild->lchild = NULL;
			r->rchild->rchild = NULL;
			return 1; 
		}
	}
	else{
		flag = STree(r->lchild,a,b);
	    if(flag != 1){
		    flag = STree(r->rchild,a,b);			
		}
	}
	return flag;
} 

int count = 1;
void PreOrderTree(BiTree root,int m){
	if(root == NULL) return;
	if(m != count){
	    printf("%d ",root->data);
		count++;		
	}
    else{
    	printf("%d\n",root->data);
	}
	PreOrderTree(root->lchild,m);
	PreOrderTree(root->rchild,m);
} 

int count1 = 1;
void InOrderTree(BiTree root,int m){
	BiTree p,stack[100];
	int top = 0;
	if(root == NULL) return;
	p = root;
	while(!(p == NULL && top == 0)){
		while(p != NULL){
			if(top<99){
				stack[top] = p;
				top++;
			}
			else{
				printf("栈溢出");
				return; 
			}
			p = p->lchild; 
		}
		if(top<=0) return;
		else{
			top--;
			p = stack[top];
	            if(m != count1){
	                printf("%d ",p->data);
		            count1++;		
	            }
                else{
    	            printf("%d\n",p->data);
	            }

			p = p->rchild;
		}
	} 
}

int count2 = 1;
void LaOrderTree(BiTree root,int m){	
	if(root == NULL) return;
	LaOrderTree(root->lchild,m);
	LaOrderTree(root->rchild,m);
	if(m != count2){
	    printf("%d ",root->data);
		count2++;		
	}
    else{
    	printf("%d\n",root->data);
	}
} 

int main(){
	int n,count=0,k,m;
	BiTree t;
	t = (BiTNode*)malloc(sizeof(BiTNode));
	cout<<"1...建立树"<<endl;
	cout<<"2...前序遍历树"<<endl;
	cout<<"3...中序(非递归)遍历树"<<endl;
	cout<<"4...后序遍历树"<<endl;
	cout<<"0...结束"<<endl; 
	while(1){
		scanf("%d",&k);
		if(k==1){
			printf("请建立树:\n"); 
			scanf("%d",&n);   //输入节点个数 
            m=n;
	        if(n!=0){
	        while(n-1){
	            int a,b;
	            scanf("%d%d",&a,&b);
	            if(count == 0){
	   	            t->data = a;
	   	            t->lchild = NULL;
	   	            t->rchild = NULL;
	   	            count++;
	            }
	            STree(t,a,b);
	            n--;
	        }
	        }
		}
		else if(k==2){
			printf("前序遍历树\n");
			PreOrderTree(t,m);	
			cout<<endl;
		} 
		else if(k==3){
			printf("中序遍历树\n");
			InOrderTree(t,m);
			cout<<endl;
		}
		else if(k==4){
			printf("后序遍历树\n");
			LaOrderTree(t,m);
			cout<<endl;
		}
		else if(k==0){
			break;
		}
	}
	return 0;
}

2. In the binary tree, a binary tree node P indicated in any given node, to write the algorithm for the path from the root to between the node P indicated.

   Experimental requirements: binary list in a storage structure

   Experimental Tip: non-recursive binary tree traversal order, when the order traversal to access node P indicated, when the stack P of all the nodes are ancestors of the node indicated by the ancestor from the root constitutes a junction path between the node P indicated.

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct BiTNode{
	int data;
	BiTNode *lchild,*rchild;
};
typedef BiTNode *BiTree;
typedef struct{
	BiTree link;
	int flag;
}StackType;

void LaOrderTree(BiTree root,int p){   //后序遍历非递归实现 
	StackType stack[100];
	BiTree q;
	int top,sign;
	if(root == NULL) return;
	top = -1;
	q = root;
	while(!(q==NULL && top==-1)){
		if(q!=NULL){
			top++;
			stack[top].link = q;
			stack[top].flag = 1;
			q = q->lchild;
		}
		else{
			q = stack[top].link;
			sign = stack[top].flag;
			top--;
			if(sign==1){
				top++;
				stack[top].link = q;
				stack[top].flag = 2;
				q = q->rchild;
			}
			else{
	            char tmp[100];
                int i=0,length;
                if(q->data == p){
                    length = top+1;
                	tmp[i] = p;
                	while(top != -1){
						q = stack[top].link;
                		tmp[++i] = q->data;
                		top--;
					}
					cout<<endl; 
					cout<<"路径为:"<<endl;
					while(length>0){
						printf("%d ",tmp[length]);
						length--;
					}	
					printf("%d",tmp[0]);
					break;
				}
				q = NULL;
			}
		}
	}
} 

int STree(BiTree r,int a,int b){
	int flag;
	if(r == NULL) return 0;
    if(r->data == a){
    	if(r->lchild == NULL){
    		r->lchild = (BiTNode*)malloc(sizeof(BiTNode));
    		r->lchild->data = b;
			r->lchild->lchild = NULL;
			r->lchild->rchild = NULL;
			return 1; 
		}
		else if(r->rchild == NULL){
			r->rchild = (BiTNode*)malloc(sizeof(BiTNode));
			r->rchild->data = b;
			r->rchild->lchild = NULL;
			r->rchild->rchild = NULL;
			return 1; 
		}
	}
	else{
		flag = STree(r->lchild,a,b);
	    if(flag != 1){
		    flag = STree(r->rchild,a,b);			
		}
	}
	return flag;
} 

int main(){
	int n,count=0;
	BiTree t;
	t = (BiTNode*)malloc(sizeof(BiTNode));
	cout<<"请建立树:"<<endl; 
	scanf("%d",&n);
	int m=n;
	if(n!=0){
	while(n-1){
	    int a,b;
	    scanf("%d%d",&a,&b);
	    if(count == 0){
	   	    t->data = a;
	   	    t->lchild = NULL;
	   	    t->rchild = NULL;
	   	    count++;
	    }
	    STree(t,a,b);
	    n--;
	}
    }
	int p;
	scanf("%d",&p);
	LaOrderTree(t,p);
	return 0;
}

3. Test the algorithm written in hierarchical order binary tree traversal

Problem Description: Write algorithm hierarchical order binary tree traversal

Experimental requirements: binary list in a storage structure

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct BiTNode{
	int data;
	BiTNode *lchild,*rchild;
};
typedef BiTNode* BiTree; 

int STree(BiTree r,int a,int b){
	int flag;
	if(r == NULL) return 0;
    if(r->data == a){
    	if(r->lchild == NULL){
    		r->lchild = (BiTNode*)malloc(sizeof(BiTNode));
    		r->lchild->data = b;
			r->lchild->lchild = NULL;
			r->lchild->rchild = NULL;
			return 1; 
		}
		else if(r->rchild == NULL){
			r->rchild = (BiTNode*)malloc(sizeof(BiTNode));
			r->rchild->data = b;
			r->rchild->lchild = NULL;
			r->rchild->rchild = NULL;
			return 1; 
		}
	}
	else{
		flag = STree(r->lchild,a,b);
	    if(flag != 1){
		    flag = STree(r->rchild,a,b);			
		}
	}
	return flag;
} 

int count=1;
int print_level(BiTree T,int level,int k){
	if(!T || level < 0) return 0;
	if(level == 0){
		if(count!=k){
			cout<<T->data<<" ";
			count++;
		}
		else{
			cout<<T->data; 
		}
		return 1;
	}
	return print_level(T->lchild,level-1,k)+print_level(T->rchild,level-1,k);
}

void print_for_level(BiTree T,int m){
	int i=0;
	for(i=0; ;i++){
		if(!print_level(T,i,m)) break;
	}
}

int main(){
	int n,count=0;
	BiTree t;
	t = (BiTNode*)malloc(sizeof(BiTNode));
	cout<<"请建立树:"<<endl; 
	scanf("%d",&n);
	int m=n;
	if(n!=0){
	while(n-1){
	    int a,b;
	    scanf("%d%d",&a,&b);
	    if(count == 0){
	   	    t->data = a;
	   	    t->lchild = NULL;
	   	    t->rchild = NULL;
	   	    count++;
	    }
	    STree(t,a,b);
	    n--;
	}
	}
	cout<<endl;
	cout<<"按层次遍历结果为"<<endl; 
	print_for_level(t,m);
	return 0;
}

4. Preparation of a binary tree algorithm height and width.

1) Description of the problem: binary tree height is the maximum number of layers of all the nodes in the binary tree refers to the width of each layer of a binary tree having a maximum total number of nodes on the number of nodes in that layer.

2) Experimental requirements: binary list in a storage structure

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct BiTNode{
	char data;
	BiTNode *lchild,*rchild;
};
typedef BiTNode *BiTree;

int STree(BiTree r,int a,int b){
	int flag;
	if(r == NULL) return 0;
    if(r->data == a){
    	if(r->lchild == NULL){
    		r->lchild = (BiTNode*)malloc(sizeof(BiTNode));
    		r->lchild->data = b;
			r->lchild->lchild = NULL;
			r->lchild->rchild = NULL;
			return 1; 
		}
		else if(r->rchild == NULL){
			r->rchild = (BiTNode*)malloc(sizeof(BiTNode));
			r->rchild->data = b;
			r->rchild->lchild = NULL;
			r->rchild->rchild = NULL;
			return 1; 
		}
	}
	else{
		flag = STree(r->lchild,a,b);
	    if(flag != 1){
		    flag = STree(r->rchild,a,b);			
		}
	}
	return flag;
} 

int Depth(BiTree T){
	int dep1,dep2;
	if(T == NULL) return 0;
	else{
		dep1 = Depth(T->lchild);
		dep2 = Depth(T->rchild);
		if(dep1>dep2) return(dep1 + 1);
		else return(dep2+1);
	}
} 

int Width(BiTree T){
	BiTree q[100];
	int front = -1, rear = -1;
	int flag = 0,count = 0,p;
	if(T!=NULL){
		rear++;
		q[rear] = T;   //入栈 
		flag=1;
		p=rear;
	}
	while(front<p){
		front++;
		T=q[front];
		if(T->lchild!=NULL){
			rear++;
			q[rear]=T->lchild;
			count++;
		}
		if(T->rchild!=NULL){
			rear++;
			q[rear]=T->rchild;
			count++;
		}
		if(front==p){
			if(flag<count) flag=count;
			count=0;
			p=rear;
		}
	} 
	return flag;
}
int main(){
	int n,count=0;
	BiTree t;
	t = (BiTNode*)malloc(sizeof(BiTNode));
	cout<<"请建立树:"<<endl;
	 
	scanf("%d",&n);
	int m=n;
	if(n!=0){
	while(n-1){
	    int a,b;
	    scanf("%d%d",&a,&b);
	    if(count == 0){
	   	    t->data = a;
	   	    t->lchild = NULL;
	   	    t->rchild = NULL;
	   	    count++;
	    }
	    STree(t,a,b);
	    n--;
	}
	}
	printf("深度为:%d 宽度为:%d",Depth(t),Width(t));
	return 0;
}

5. To achieve a Huffman encoding / decoding system

1) Description of the problem: the use of Huffman coding information can greatly improve the communication channel utilization information to shorten the transmission time, lower transmission costs. However, this requires the data to be transmitted in advance through a coding system at the transmitting end, the receiving end decodes the transmitted data. For duplex channels, each side needs a complete coding / decoding system. A test writing Huffman encoding / decoding system for such information transceiver station.

 

2) Experimental requirements: A complete system should have the following features:

(1) I: Initialization (Initialization). Read from the character set of the terminal size n, and n and n number of characters weights, the Huffman tree established, and it is stored in the document hfmTree.

(2) E: encoding (Encoding). Has been built using the Huffman tree in the file ToBeTran encoding the bodies, then the result is stored in the file CodeFile.

(3) D: decoding (Decoding). Using a Huffman code tree in the file has been built CodeFile decoding result is stored in the file TextFile.

(4) P: code file print (Print). CodeFile files displayed on a terminal in a compact format, each line of code 50. At the same time writes this file CodePrin in the form of characters encoded file.

(5) T: Print Huffman tree (Tree printing). The Huffman tree is in memory in an intuitive way is displayed on the terminal, and write this file TreePrint Huffman tree in the form of characters.

3) to achieve prompt:

(1) The base type CodeFile file can be set byte.

(2) The user interface can be designed as a "menu" mode: displaying the function symbols, together with "Q", out of operation represents Quit. Please select the user types a character. This feature is finished and then display the menu until the user selects a particular date "E".

(3) After the execution of the program during a first execution of I, D, or C command, the Huffman tree has a memory, not necessarily read into. Every time I perform not necessarily execute the command because the file hfmTree may have already built.

#include<iostream>
using namespace std;
#include<cstdlib>
#include<cstring>
#include<cmath>
#define MaxWeight 1000
#define MaxLeaf 300
#define MaxNode MaxLeaf*2-1
#define MaxBit 100
typedef struct HNodeType{
	int weight;
	int parent;
	int lchild;
	int rchild;
	char ch;
}; 
typedef struct HCodeType{
	int bit[MaxBit];
	int start;
};
typedef struct{
	HNodeType link;
	int flag;
}StackType;
int N;
void HuffmanTree(HNodeType HuTree[],int n){     //哈弗曼树的构造 
	FILE *fp;
	int i,j,m1,m2,x1,x2;
//	scanf("%d",&n);        //输入字符集大小
	for(i=0;i<2*n-1;i++){
		HuTree[i].weight = 0;
		HuTree[i].parent = -1;
		HuTree[i].lchild = -1;
		HuTree[i].rchild = -1;
		HuTree[i].ch = '$'; 
	}
	getchar();	
	for(i=0;i<n;i++){
		scanf("%c",&HuTree[i].ch); 
		getchar(); 
	}
	for(i=0;i<n;i++){
		scanf("%d",&HuTree[i].weight);
	}
	for(i=0;i<n-1;i++){
		m1 = m2 = MaxWeight;
		x1 = x2 = 0;
		for(j=0;j<n+i;j++){
			if(HuTree[j].weight<m1 && HuTree[j].parent == -1){
				m2 = m1;
				x2 = x1;
				m1 = HuTree[j].weight;
				x1 = j;
			}
			else if(HuTree[j].weight<m2 && HuTree[j].parent == -1){
				m2 = HuTree[j].weight;
				x2 = j;
			}
		}
		HuTree[x1].parent = n+i;
		HuTree[x2].parent = n+i;
		HuTree[n+i].weight = HuTree[x1].weight + HuTree[x2].weight;
		HuTree[n+i].lchild = x1;
		HuTree[n+i].rchild = x2;
	}
	fp=fopen("hfmTree.dat","wb");
	for(i=0;i<2*n-1;++i)
		fwrite(&HuTree[i],sizeof(HuTree),1,fp);  
		//在hfmTree中存入树的各节点数据域信息 
	fclose(fp);
}
//编码
void HaffmanCode(HNodeType HuTree[],int n){
	
	HCodeType HuffCode[MaxNode],cd;
	int i,j,c,p;
//	HuffmanTree(HuTree,n);
	for(i=0;i<n;i++){
		cd.start = n-1;
		c = i;
		p = HuTree[c].parent;
		while(p!=-1){
			if(HuTree[p].lchild==c) cd.bit[cd.start]=0;
			else cd.bit[cd.start]=1;
			cd.start--;
			c = p;
			p = HuTree[c].parent;
		}
		for(j=cd.start+1;j<n;j++){
			HuffCode[i].bit[j] = cd.bit[j];
		}
		HuffCode[i].start = cd.start;
	}	
	//输出编码 
	FILE *fp1;
	fp1 = fopen("ToBeTran.txt","r");
	FILE *fp2;
    fp2=fopen("CodeFile.txt","w");
    char b[100];
    int f=0;
	while(fscanf(fp1,"%c",&b[f]) && !feof(fp1)){
		i=0;
		while(i<n){
			if(b[f] == HuTree[i].ch){
				for(j=HuffCode[i].start+1;j<n;j++){
			        fprintf(fp2,"%ld",HuffCode[i].bit[j]);
    		    }
    		    break;
			}
			else{
				i++;
			}
		}
		f++;
	}
	fclose(fp1);
	fclose(fp2);
} 

int main(){
	HNodeType HuTree[100];
	int Q,n;
	char T;
	int p=0,c;
	char b;
    cout<<"I:初始化"<<endl;
    cout<<"E:编码"<<endl;
    cout<<"D:译码"<<endl;
    cout<<"P:打印代码文件"<<endl;
    cout<<"T:打印哈弗曼树"<<endl;
    cout<<"Q:退出"<<endl;	
    cout<<endl; 
	cout<<"请首先建立树:"<<endl;
	scanf("%c",&T);	

	while(T != 'Q'){
		if(T=='I'){
		
			cout<<"格式为:节点个数 各节点名称 各节点权值"<<endl; 
			scanf("%d",&n);
			HuffmanTree(HuTree,n);
			cout<<"建立哈弗曼树成功!"<<endl;
			cout<<endl;
		}
		else if(T=='E'){
			HaffmanCode(HuTree,n);
			cout<<"编码成功!"<<endl;
			cout<<endl;
		}
		else if(T=='D'){
			FILE *fp4;
	        fp4 = fopen("CodeFile.txt","r");
			FILE *fp5;
	        fp5 = fopen("TextFile.txt","w"); 
			getchar();
			while(p != -1){
				c=p;
				p = HuTree[c].parent;
			}
			int q=c;
            int count=0;
            int d=0;
			    string str;
            while(fscanf(fp4,"%c",&str[d]) && !feof(fp4)){
				if(str[d] == '0'){
					q = HuTree[q].lchild;
					if(HuTree[q].ch != '$'){
                        fprintf(fp5,"%c",HuTree[q].ch);
						q = c;
					}
				}
				else if(str[d] == '1'){
					q = HuTree[q].rchild;
					if(HuTree[q].ch != '$'){
						fprintf(fp5,"%c",HuTree[q].ch);
						q = c;	
					}
				}
				d++;
	    	}    
			cout<<"译码成功!"<<endl;
			cout<<endl;
		}
		
		else if(T == 'P'){
            FILE *fp8,*fp6;
            int num=100;
            char a[num];
            fp8=fopen("CodeFile.txt","r"); 
            fp6=fopen("CodePrin.txt","w"); 
            fgets(a,num,fp8);
            cout<<"打印CodeFile文件:"<<endl;
            int l= strlen(a);
            for(int j=0;j<l;j++)
            {
                cout<<a[j];
                fprintf(fp6,"%c",a[j]);
                if((j+1)%50==0)       //每行输出50个 
                    cout<<endl;
            }
            cout<<endl;
            fclose(fp8);
            cout<<"输出成功!"<<endl;
			cout<<endl;
		}
		else if(T == 'T'){
			FILE *fp7;
			fp7 = fopen("TreePrint.txt","w");
			int i=2*n-2,j=i+2,count=0,sum=0;
			int k=1;sum = pow(2,0);
			cout<<"哈弗曼树为:"<<endl; 
			while(j--){
		    	cout<<" ";
		    	fprintf(fp7,"%c",' ');
			}
		    while(i>=0){
		    	j=i;
		    	count++;
		    	cout<<HuTree[i].weight<<" "; 
		    	fprintf(fp7,"%d",HuTree[i].weight);
		    	fprintf(fp7,"%c",' ');
		    	if(count == sum){
		    		sum += pow(2,k);
		    		k++;
					cout<<endl;
					cout<<endl;
				    while(j--){
		    	    	cout<<" ";
		    	    	fprintf(fp7,"%c",' ');
				    }
				} 
				i--;
			}
			cout<<endl; 
			cout<<endl; 
			cout<<endl; 
		}
		scanf("%c",&T);
	} 
	cout<<"结束"<<endl; 
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41106517/article/details/94717909