t1:tree遍历,递归,非递归,层次遍历
t2:tree 给出中序和先序或者中序和后序,建立该二叉树
t3:以括号法和凹入法表示二叉树,(凹入法没写)
t4:huffman树; 给定n个整数,求对应的哈夫曼树的高度
t5:tree路径遍历,求最长路径
t6:求茂密度:一个二叉树的高度乘所有层中节点数最多那层的节点数
t7:堆
头文件:
#ifndef TREE_H_INCLUDED #define TREE_H_INCLUDED #define MAX 100 //end flag struct BtNode{ int data; BtNode *lc; BtNode *rc; BtNode():data(0),lc(NULL),rc(NULL){ } }; struct HtNode{ int data; int parent; int lc; int rc; }; void creatbt(BtNode*& root); void midT(BtNode* root); void norecure_midt(BtNode* root); void norecure_pret(BtNode* root); void norecure_post(BtNode* root); void layert(BtNode* root); BtNode* t2_pre_mid(vector<int>::iterator pre,vector<int>::iterator mid,int n); BtNode* t2_pos_mid(vector<int>::iterator pos,vector<int>::iterator mid,int n); void t3_brac(BtNode* root); void t3_inde(BtNode* root); void huffman(vector<HtNode>& t,vector<int>& d); void huffman_code(vector<HtNode>& ht,int n); int t4_huffman_high(vector<int>& a); void t5_allpath(BtNode* r,vector<int>& s); void t5_lpath(BtNode* r,vector<int>& s,vector<int>& l); int t6(BtNode* r); //t7--heap template <class T> class MinHeap{ private: enum {DefaultSize=10}; vector<T> heap; int CurrentSize; int MaxHeapSize; void FilterDown(int m,int n); void FilterUp(int m); public: MinHeap(int maxSize); MinHeap(vector<T>& v,int maxSize); vector<T> GetHeap() const{ return heap; } const MinHeap<T> &operator=(const MinHeap &R); int Insert(const T &x); int RemoveMin(T &x); int isEmpty() const{ return CurrentSize==0;} int isFull() const{ return CurrentSize==MaxHeapSize;} void MakeEmpty(){ CurrentSize=0;} };//end template <class T> MinHeap<T>::MinHeap(int maxSize){ MaxHeapSize=DefaultSize<maxSize?maxSize:DefaultSize; heap.resize(MaxHeapSize); CurrentSize=0; } template <class T> void MinHeap<T>::FilterDown(int m,int n){ T tmp=heap[m]; int i=m,j=2*i-1; while(j<=n){ if(j<n&&(heap[j]>heap[j+1])) ++j; if(tmp<=heap[j]) break; else{ heap[i]=heap[j]; i=j; j=2*i-1; } } heap[i]=tmp; } template <class T> void MinHeap<T>::FilterUp(int m){ int j=m,i=(j-1)/2; T tmp=heap[m]; while(j>0){ if(heap[i]<=heap[j]) break; else{ heap[j]=heap[i]; j=i; i=(j-1)/2; } } heap[j]=tmp; } template <class T> MinHeap<T>::MinHeap(vector<T>& v,int maxSize){ MaxHeapSize=DefaultSize<maxSize?maxSize:DefaultSize; heap=v; CurrentSize=v.size(); int currentPos=(CurrentSize-2)/2; while(currentPos>=0){ FilterDown(currentPos,CurrentSize-1); --currentPos; } } template <class T> int MinHeap<T>::Insert(const T& x){ if(CurrentSize==MaxHeapSize){ cerr<<"Heap Full"<<endl; return 0; } heap[CurrentSize]=x; FilterUp(CurrentSize); ++CurrentSize; return 1; } template <class T> int MinHeap<T>::RemoveMin(T& x){ if(!CurrentSize){ cout<<"Heap Empty"<<endl; return 0; } x=heap[0]; heap[0]=heap[CurrentSize-1]; --CurrentSize; FilterDown(0,CurrentSize-1); return 1; } //end t7--heap #endif // TREE_H_INCLUDED
#include <iostream> #include <stack> #include <deque> #include <vector> #include <iterator> using namespace std; #include <tree.h> //t1 void creatbt(BtNode*& root){//creat tree int data; cin>>data; if(data!=MAX){ root=new BtNode(); root->data=data; creatbt(root->lc); creatbt(root->rc); } } void midT(BtNode* root){// recure mid traverse if(root!=NULL){ midT(root->lc); cout<<root->data; midT(root->rc); } } void norecure_midt(BtNode* root){//no recure mid traverse BtNode* p=root; stack<BtNode*> s; if(p==NULL){ cout<<"tree empty"<<endl; return; } while(!s.empty()||p!=NULL){ while(p!=NULL){ s.push(p); p=p->lc; } if(!s.empty()){ p=s.top(); s.pop(); cout<<p->data<<endl; p=p->rc; } } } void norecure_pret(BtNode* root){// no recure pre traverse BtNode* p=root; stack<BtNode*> s; if(p==NULL){ cout<<"tree empty"<<endl; return; } while(!s.empty()||p){ while(p!=NULL){ cout<<p->data<<endl; s.push(p); p=p->lc; } if(!s.empty()){ p=s.top(); s.pop(); p=p->rc; } } } void norecure_post(BtNode* root){//no recure pos traverse BtNode* p=root; BtNode* q; int flag; stack<BtNode*> s; if(p==NULL){ cout<<"tree empty"<<endl; return; } s.push(p); while(!s.empty()){ p=s.top(); p=p->lc; while(p){ s.push(p); p=p->lc; } q=NULL; flag=1; while(!s.empty()&&flag){ p=s.top(); if(p->rc==q||p->rc==NULL){ cout<<p->data<<endl; s.pop(); q=p; } else{ p=p->rc; s.push(p); flag=0; } } } } void layert(BtNode* root){ deque<BtNode*> d; BtNode* p; d.push_back(root); while(!d.empty()){ p=d.front(); cout<<p->data<<endl; d.pop_front(); if(p->lc!=NULL) d.push_back(p->lc); if(p->rc!=NULL) d.push_back(p->rc); } } //t2 BtNode* t2_pre_mid(vector<int>::iterator pre,vector<int>::iterator mid,int n){ //已知中序和先序 BtNode* s; vector<int>::iterator p; int k=0; if(n<=0) return NULL; s=new BtNode(); s->data=*pre; for(p=mid;p<mid+n;++p){ if(*p==*pre) break; ++k; } s->lc=t2_pre_mid(pre+1,mid,k); s->rc=t2_pre_mid(pre+k+1,p+1,n-k-1); return s; } BtNode* t2_pos_mid(vector<int>::iterator pos,vector<int>::iterator mid,int n){ //已知后序和中序 BtNode* s; vector<int>::iterator p; int k=0; if(n<=0) return NULL; s=new BtNode(); s->data=*(pos+n-1); for(p=mid;p<mid+n;++p){ if(*p==*(pos+n-1)) break; ++k; } s->lc=t2_pos_mid(pos,mid,k); s->rc=t2_pos_mid(pos+k,p+1,n-k-1); return s; } //t3 void t3_brac(BtNode* root){ if(root!=NULL){ cout<<root->data; if(root->lc!=NULL||root->rc!=NULL){ cout<<"("; t3_brac(root->lc); if(root->rc!=NULL) cout<<","; t3_brac(root->rc); cout<<")"; } } } void t3_inde(BtNode* root){ if(root!=NULL){ //cout<<root->data; if(root->lc!=NULL){ cout<<endl; cout<<" "<<root->lc->data<<"(L)"; t3_inde(root->lc); } if(root->rc!=NULL){ cout<<endl; cout<<" "<<root->rc->data<<"(R)"; t3_inde(root->rc); } } } //t4 void huffman(vector<HtNode>& t,vector<int>& d){ int max1,max2; int rcnode,lcnode; int num=d.size(); t.resize(2*num-1); for(int i=0;i<num;++i) t[i].data=d[i]; for(int i=num;i<2*num-1;++i) t[i].data=0; for(int i=0;i<t.size();++i) t[i].parent=t[i].lc=t[i].rc=-1; for(int k=num;k<2*num-1;++k){ max1=max2=0; lcnode=rcnode=-1; for(int i=0;i<k;++i)// find the biggest two elments if(t[i].parent==-1){ if(t[i].data>max1){ max2=max1; lcnode=rcnode; max1=t[i].data; rcnode=i; } else if(t[i].data>max2){ max2=t[i].data; lcnode=i; } } t[k].data=t[lcnode].data+t[rcnode].data; t[k].parent=-1; t[k].lc=lcnode; t[k].rc=rcnode; t[lcnode].parent=k; t[rcnode].parent=k; } } void huffman_code(vector<HtNode>& ht,int n){//n个叶子节点 int p,c; for(int i=0;i<n;++i){ c=i; p=ht[c].parent; deque<int> htcode; while(p!=-1){ if(ht[p].lc==c) htcode.push_front(0); else htcode.push_front(1); c=p; p=ht[c].parent; } cout<<ht[i].data<<":"; copy(htcode.begin(),htcode.end(),ostream_iterator<int>(cout)); cout<<endl; } } //huffman tree height struct Node{ int data; int layer; int flag; Node():data(0),layer(1),flag(0){ } }; int t4_huffman_high(vector<int>& a){ int num=a.size(); vector<Node> ht(2*num-1); int max1,max2; int lc,rc; int cur; for(int i=0;i<num;++i) ht[i].data=a[i]; for(int i=num;i<2*num-1;++i){ max1=max2=0; lc=rc=0; cur=0; for(int j=0;j<i;++j) if(ht[j].flag==0){ if(ht[j].data>max1){ max2=max1; lc=rc; max1=ht[j].data; rc=j; } else if(ht[j].data>max2){ max2=ht[j].data; lc=j; } } ht[lc].flag=ht[rc].flag=1; cur=(ht[lc].layer>ht[rc].layer)?ht[lc].layer:ht[rc].layer; ht[i].data=ht[lc].data+ht[rc].data; ht[i].layer=cur+1; ht[i].flag=0; } for(int i=0;i<2*num-1;++i) if(ht[i].flag==0) return ht[i].layer; } //t5 void t5_allpath(BtNode* r,vector<int>& s){ if(r!=NULL){ if(r->lc==NULL&&r->rc==NULL){ cout<<r->data<<":"; copy(s.rbegin(),s.rend(),ostream_iterator<int>(cout," ")); cout<<endl; } else{ s.push_back(r->data);//分治 t5_allpath(r->lc,s); t5_allpath(r->rc,s); s.pop_back(); } } } void t5_lpath(BtNode* r,vector<int>& s,vector<int>& l){ if(r!=NULL){ if(r->lc==NULL&&r->rc==NULL){ if(s.size()+1>l.size()){ l=s; l.push_back(r->data); } } else{ s.push_back(r->data);//分治 t5_lpath(r->lc,s,l); t5_lpath(r->rc,s,l); s.pop_back(); } } } int t6(BtNode* r){//子问题为本层节点数 int l,num;//高度和最大节点数 int cur1,cur2;//本层遍历中节点数和下一层次节点数 deque<BtNode*> d; BtNode* tmp; d.push_back(r); l=1; num=1; cur2=1; cur1=cur2; cur2=0; while(!d.empty()){ tmp=d.front(); d.pop_front(); if(tmp->lc!=NULL){ //下一层节点数增一 d.push_back(tmp->lc); ++cur2; } if(tmp->rc!=NULL){ d.push_back(tmp->rc); ++cur2; } --cur1;//当前层节点数减一 if(cur1==0&&!d.empty()){//如果队列未空且本层已遍历完,转向下一层 ++l; if(cur2>num) num=cur2; cur1=cur2; cur2=0; } } return num*l; } //t7--heap