C ++ヒープと検索ツリー-山東大学のデータ構造実験6

実験内容
1。一連のゼロ以外の正の整数(20以下)を入力します。0は入力の終わりを表します(0を含まない)。
2.上記のデータシーケンス入力に従って、初期化方法を使用して最大ヒープを作成し(ノードを順番に挿入して最大ヒープを作成しないでください)、最大ヒープの階層シーケンスを出力します。
3.ヒープソート後にソート結果を出力します。
4.上記で入力したデータに基づいて、二分探索木を作成し(キーワードを繰り返すことはできません。繰り返しの場合は、キーワードを繰り返し挿入しないでください)、バイナリの前順および中順のシーケンスを出力します。検索ツリー(ブランチ出力)。

これの記述は少し面倒です、コードの一部は他の人からも借りています、私は数日でそれを変更してコメントを追加します

#include<iostream>
#include<string>
using namespace std;
 
template<class T>
class BTNode {
    
    
             public:
             	        T data;
                        BTNode<T> *Leftchild;    
                        BTNode<T> *Rightchild;  
                        
                        BTNode(const T&element)
                          {
    
    
                            data=element;
                            Leftchild=Rightchild=NULL;
                          }
                        BTNode() {
    
    Leftchild=Rightchild=NULL;}
                        BTNode(const T&element, BTNode *lchild,
                                            BTNode *rchild)
                          {
    
    
                             data=element;
                             Leftchild=lchild;
                             Rightchild=rchild;
                          }


};
template<class T>
class BTree{
    
    
 public:
          BTree()
		  {
    
    
		  	root=NULL;
		  }
        void InOrder(void(*Visit)(BTNode<T> *u))
        {
    
    
		InOrder(Visit, root);
		}
		  int Root(T& m)const;
          int IsEmpty()const
            {
    
    
            	if (root==NULL)
            		return 1;
            	else
            		return 0;
			}

          void PreOrder(void(*Visit)(BTNode<T> *u))
                {
    
    
					PreOrder(Visit, root);
				}
          BTNode<T>* root;
          void PreOrder(void(*Visit)
              (BTNode<T> *u), BTNode<T> *t);
        void InOrder(void(*Visit)
              (BTNode<T> *n), BTNode<T> *t);
		void InOrder(void(*Visit)(BTNode<T> *n,int j),BTNode<T> *n,int j); 
		void PreOrder(void(*Visit)(BTNode<T> *n,int j),BTNode<T> *n,int j);

			
          static void Out(BTNode<T> *t)
                 {
    
    
                  cout << t->data << " ";
                 }
          static void Out1(BTNode<T> *t,int i)
		  		 {
    
    
				  if(i != 0)
				  cout<<",";
			      cout<<t->data;
		  		 }

          void Preoutput()
            {
    
    
              static int i = 0;
			  PreOrder(Out1,root,i);
              cout << endl;
            }

          void Inoutput()
             {
    
    
              static int i = 0;
			  InOrder(Out1,root,i);
              cout << endl;
             }
         };
template<class E, class K>
class BSTree : public BTree<E>{
    
    
         public:
                    int search(const K& key, E& element)const;
                    BSTree<E,K>& insert(const E& element);
         };
template<class T>
int BTree<T>::Root(T& m) const
  {
    
     
    if(root!=NULL)
      {
    
    
        m=root->data;
        return 1;
      }
    else return 0; 
  }

template<class T>
void BTree<T>::PreOrder(void(*Visit)(BTNode<T> *n,int j),BTNode<T> *t,int j){
    
    
	if(t!=NULL){
    
    
		Visit(t,j);
		PreOrder(Visit,t->Leftchild,++j);
		PreOrder(Visit,t->Rightchild,++j);
	}
} 
template<class T>
void BTree<T>::PreOrder(void(*Visit)
                        (BTNode<T>* n), BTNode<T>* t)
  {
    
    
 
     if(t!=NULL)
      {
    
    
        Visit(t);
        PreOrder(Visit, t->LeftChild);
        PreOrder(Visit, t->RightChild);
      }
  }
 
template<class T>
void BTree<T>::InOrder(void(*Visit)(BTNode<T>* n),
                                     BTNode<T>* t)
  {
    
    
 
      if(t!=NULL)
         {
    
    
          InOrder(Visit, t->LeftChild);
          Vvisit(t);
          InOrder(Visit, t->RightChild);
         }
  }
   
template<class T>
void BTree<T>::InOrder(void(*Visit)(BTNode<T> *n,int j),BTNode<T> *t,int j)
{
    
    
	if(t!=NULL)
	{
    
    
		InOrder(Visit,t->Leftchild,j);
		if(t->Leftchild)
			j++;
		Visit(t,j++);
		InOrder(Visit,t->Rightchild,j);
	}
} 

template<class E, class K>
int BSTree<E,K>::search(const K& key, E& element) const
 {
    
    
 
    BTNode<E> *p=this->root;
 
     while(p) 
      {
    
    
        if(key<p->data)
          p=p->Leftchild;
        else if(key>p->data)
          p=p->Rightchild;
        else{
    
    
              element=p->data;
              return 1;
             }
      }
     return 0;
  }
 
template<class E, class K>
BSTree<E,K>& BSTree<E,K>::insert(const E& element)
 {
    
     
  BTNode<E> *pf=NULL; 
  BTNode<E> *p=NULL;  

  p=this->root; 
  while(p!=NULL)
    {
    
    
     pf=p;

	if(element>p->data)
      p=p->Rightchild; 
    else if(element<p->data)
      p=p->Leftchild;
    else
      return *this;  
     }
     
    BTNode<E> *newnode=new BTNode<E>(element);
     if(this->root != NULL)
      {
    
    
	    if(element>pf->data)
         	pf->Rightchild=newnode; 
        else if(element<pf->data)
          	pf->Leftchild=newnode;
      }
     else 
        this->root=newnode;
    return *this;
  }
 
struct BinaryTreeNode
{
    
    
	char data;
	struct BinaryTreeNode* lchild;
	struct BinaryTreeNode* rchild;
};

void initialize(int* theheap, int thesize)  
{
    
    
	for (int root = thesize / 2; root >= 1; root--)
	{
    
    
		int rootelement = theheap[root];
		int child = 2 * root;
		while (child <= thesize)
		{
    
    
			if (child < thesize && theheap[child] < theheap[child + 1])
				child++;
			if (rootelement >= theheap[child])
				break;

			theheap[child / 2] = theheap[child];
			child = child * 2;
		}
		theheap[child / 2] = rootelement;
	}
}

void pop(int* heap, int size)  
{
    
    
	if (size == 0)
		return;
	int lastelement = heap[size];

	int node = 1, child = 2;
	while (child <= size)
	{
    
    
		if (child < size && heap[child] < heap[child + 1])
			child++;
		if (lastelement >= heap[child])
			break;
		heap[node] = heap[child];
		node = child;
		child *= 2;
	}
	heap[node] = lastelement;
}
int * sort(int* heap, int size, int *a)  
{
    
    
	int j = 1;
	for (int i = size; i >= 1; i--)
	{
    
    
		int x = heap[1];
		pop(heap, size);
		size--;
		a[j] = x;
		j++;
	}
	return a;
}

int main(){
    
    

	BSTree<int,int> in;
	int heap [22];	
	int input [22];
	int a [22];
	int len = 0;
	cout<<"Input"<<endl;
	for(int i = 0;i <= 20;i++){
    
    
		cin>>input[i+1];
		if(input[i+1] != 0)
			in.insert(input[i+1]);
		if(input[i+1] == 0)
			break;
		len++;
	}
	for(int j = 0;j <= 20;j++)
	{
    
    
		heap[j] = input[j];
	}
	int size = len;
	
	cout<<"Output"<<endl;
	initialize(heap, size); 

	for (int j = 1; j < size+1; j++)    
	{
    
    
		if (j != size)
			cout << heap[j] << ',';
		else
			cout << heap[j] << endl;
	}

	sort(heap, size ,a);   
	for (int j = size; j >=1 ; j--) 
	{
    
    
		if (j != 1)
			cout << a[j] << ',';
		else
			cout << a[j] << endl;
	}

 	in.Preoutput();
 	in.Inoutput();
 	cout<<"End0"<<endl;
	return 0; 
} 


おすすめ

転載: blog.csdn.net/m0_47470899/article/details/109727557
おすすめ