Shandong University Data Structure Experiment 6: Binary Tree Operation

Experimental content

 

① Input a level traversal string of a complete binary tree, create this binary tree, and output the pre-order traversal string, in-order traversal string, post-order traversal string, number of nodes, binary tree height of this binary tree (each of the above results is a separate row show).

② Input the preorder sequence and inorder sequence of the binary tree (each element is different), create this binary tree, and output the postorder sequence and level traversal of the binary tree.

Experimental procedure

①Refer to the ppt review and write the BinaryTreeNode node class. For the convenience of operation, I set the data in it, the left child and the right child, to public. Initializing the node class also constructs an array of pointers, instantiates each class object, and then uses the method of left child 2*i and right child 2*i+1. Since there are not many elements, the height of the tree is also calculated by traversing the power of 2 minus 1, and the comparison is realized.

②Write a recursive program -> pre-order traversal, middle-order traversal, post-order traversal. Note that there should be no extra commas when outputting the last character, which needs to be compared with the last character of the input string to judge.

③ Write the build method that returns the node pointer, here is also the idea of ​​recursion. Take the first element from the preorder traversal, which is the "root node" of the current tree, and then find the element in the inorder traversal. The left tree of the element is the left child of the inorder 0~k-1 starting from pre+1 The k elements of the sequence are recursive, and the right tree is similar.

④Writing the hierarchical traversal method requires the use of queues. Take a closer look at the sequence traversal process. In fact, it is to put each number into the queue from top to bottom and from left to right, and then print in order is the desired result.

Implementation process

1. First push the root node of the binary tree to the queue, and if the queue is not NULL, output the element at the head of the queue.

2. If it is judged that the node has children, push the children into the queue.

3. The traversed nodes are dequeued,

4. Repeat the above operations until Tree == NULL.

 

本次实验主要代码:
#include<bits/stdc++.h>
using namespace std;

string s;
string pree,inn;
const char*preorder;
const char* inorder;
class BinaryTreeNode{
	public:
		BinaryTreeNode *left;
		BinaryTreeNode *right;
		char data;
};
void Pre(BinaryTreeNode *t){
	if(t!=NULL){
		if(s[s.size()]!=t->data)
		cout<<t->data<<",";
		else
		cout<<t->data;
		Pre(t->left);
		Pre(t->right);
	}
}
void in(BinaryTreeNode *t){
	if(t!=NULL){
		in(t->left);
		if(s[s.size()]!=t->data)
		cout<<t->data<<",";
		else
		cout<<t->data;
		in(t->right);
	}
}
void post(BinaryTreeNode *t,char s){
	if(t!=NULL){
		
		post(t->left,s);
		post(t->right,s);
		if(s!=t->data)
		cout<<t->data<<",";
		else
		cout<<t->data;
	}
}
void level(BinaryTreeNode *t){
		queue<BinaryTreeNode*> q;
		q.push(t);
		bool flag = true;
		if(pree.size()==1){
			flag = false;
		} 
		while(!q.empty()&&(q.size()!=1||flag)){
		flag = false;
			 cout << q.front()->data << ","; 
			 
			if (q.front()->left != NULL)   
        {
            q.push(q.front()->left);   
        }

        if (q.front()->right != NULL)   
        {
            q.push(q.front()->right);
        }
        q.pop();  
		}
	    cout << q.front()->data;
}

BinaryTreeNode * build(const char* preorder,const char* inorder,int n){
	if(n==0)return NULL;
	int k=0;
	while(preorder[0]!=inorder[k])k++;
	BinaryTreeNode *t=new BinaryTreeNode;
	t->data = preorder[0];
	t->left=build(preorder+1,inorder,k);
	t->right = build(preorder+k+1,inorder+k+1,n-k-1);
	return t;
}

int main(){
	cout<<"Input1"<<endl;
	int num = 0;
	cin>>s;
	for(int i=s.size()+1;i>0;i--){
		s[i]=s[i-1];
	}
	BinaryTreeNode **b=new BinaryTreeNode*[20];
	for(int i=0;i<20;i++){
		b[i] = new BinaryTreeNode;
	}
	for(int i=1;i<=s.size();i++){
		b[i]->data=s[i];
	}
	for(int i=1;i<=s.size();i++){
	if(2*i<=s.size()){
			b[i]->left=b[2*i];
		}
		else{
			b[i]->left=NULL;
		}
	if(2*i+1<=s.size()){
			b[i]->right=b[2*i+1];
		}
		else{
			b[i]->right=NULL;
		}	
	}
	cout<<"Output1"<<endl;
	Pre(b[1]);
	cout<<endl;
	in(b[1]);
	cout<<endl;
	post(b[1],s[1]);
	cout<<endl<<s.size()<<endl;
	for(int i=1;i<=6;i++){
		if(pow(2,i)-1==s.size()){
			cout<<i<<endl;
			break;
		}
	}
	cout<<"Input2"<<endl;
	
	cin>>pree>>inn;
	 preorder = pree.data();
	 inorder = inn.data();
	cout<<"Output2"<<endl;
	post(build(preorder,inorder,pree.size()),pree[0]);
	cout<<endl;
	level(build(preorder,inorder,pree.size()));
	cout<<endl<<"End0"<<endl;
	return 0;
} 

Guess you like

Origin blog.csdn.net/Sunnyztg/article/details/128280605