主要有二叉树,线索二叉树,AVL树,哈夫曼二叉树,二叉查找树(查找那一篇已发,这里不再重复)
二叉树:
//头文件 : template<class T> class TreeNode //采用左孩子,右兄弟的链式方式存储 { private: T data; TreeNode<T> *firstChild, *nextBrother; public: TreeNode (T value = 0, TreeNode<T> *L = NULL, TreeNode<T> *R = NULL):data(value), firstChild(L), nextBrother(R){} TreeNode<T> *GetFirstChild()const {return firstChild;} void SetFirstChild(TreeNode<T> *t) {firstChild = t;} TreeNode<T> *GetNextBrother() const{return nextBrother;} void SetNextBrother(TreeNode<T> *t) {nextBrother = t;} T &GetData() {return data;} void SetData(const T &item){data = item;} }; template<class T> class Tree { private: TreeNode<T> *root; T stop; public: Tree() {root = NULL;} TreeNode<T> *FindTarget(TreeNode<T> *t, T target); TreeNode<T> *FindFather(TreeNode<T> *t, TreeNode<T> *p); TreeNode<T> *FirstChild(TreeNode<T> *p); TreeNode<T> *NextBrother(TreeNode<T> *p); void DelSubTree(TreeNode<T> *t, TreeNode<T> *p); void Del(TreeNode<T> *p); void PreOder(TreeNode<T> *t); void NorecPreOrder(TreeNode<T> *t); void LevelOrder(TreeNode<T> *t); T GetStop() {return stop;} void SetStop(T tostop) {stop = tostop;} TreeNode<T> *GetRoot() {return root;} void SetRoot(TreeNode<T> *t) {root = t;} TreeNode<T> *Create(); void CreateTree(T stop); }; //源文件: #include "TreeNode.h" template<class T> TreeNode<T> * Tree<T>::FindTarget(TreeNode<T> *t, T target) { if (t == NULL) return NULL; TreeNode<T> *result = NULL; if ((t->GetData()) == target) result t; else { TreeNode<T> *p = t->GetFirstChild(); int i = 0; while(p != NULL && (result = FindTarget()) == NULL) p = p->GetNextBrother(); return result; } }; template<class T> TreeNode<T> *Tree<T>::FindFather(TreeNode<T> *t, TreeNode<T> *p) { if (t == NULL) return NULL; TreeNode<T> *result = NULL; TreeNode<T> *q = t->GetFirstChild(); while(q != NULL && q != p) { result = FindFather(q, p); if (!result) q = q->GetNextBrother(); else return result; } if (q == p) result t; else return NULL; }; template<class T> TreeNode<T> *Tree<T>::FirstChild(TreeNode<T> *p) { if (p != NULL && (p = p->GetFirstChild()) != NULL) return p->GetFirstChild(); return NULL; }; template<class T> TreeNode<T> *Tree<T>::NextBrother(TreeNode<T> *p) { if (p != NULL && (p = p->GetNextBrother()) != NULL) return p->GetNextBrother(); return NULL; }; template<class T> void Tree<T>::DelSubTree(TreeNode<T> *t, TreeNode<T> *p) { if ( t != NULL && p != NULL) { TreeNode<T> *q = NULL, *result = NULL; result = FindFather(t, p); if (result) { if ((result->GetFirstChild()) == p) { result->SetFirstChild(p->GetNextBrother()); Del(p); return; } else { q = result->GetFirstChild(); while((q->GetNextBrother()) != p) q = q->GetNextBrother(); q->SetNextBrother(p->GetNextBrother()); Del(p); return; } } else { Del(p); root = NULL; } } }; template<class T> void Tree<T>::Del(TreeNode<T> *p) { if ( p != NULL) { TreeNode<T> *q = p->GetFirstChild(), *next; while(q != NULL) { next = q->GetNextBrother(); Del(q); q = next; } delete p; } }; template<class T> void Tree<T>::PreOder(TreeNode<T> *t) { if (t != NULL) { cout<<t->GetData()<<endl; TreeNode<T> *child = NULL; child = FirstChild(t); while(child) { PreOder(child); child = NextBrother(child); } } }; template<class T> void Tree<T>::NorecPreOrder(TreeNode<T> *t) { if (t == NULL) return; AStack<TreeNode<T>*> s; int i; TreeNode<T> *p = t; do { while(p != NULL) { cout<<p->GetData()<<endl; s.Push(p); p = FirstChild(p); } while(p == NULL && s.IsEmpty()) { s.Pop(p); p = NextBrother(p); } } while (!s.IsEmpty()); } template<class T> void Tree<T>::LevelOrder(TreeNode<T> *t) { AQueue<TreeNode<T> *> q; if (t != NULL) { TreeNode<T> *p; q.QInsert(t); while(!q.isEmpty()) { q.QDelete(p); cout<<p->GetData()<<endl; p = FirstChild(p); while(p != NULL) { q.QInsert(p); p = NextBrother(p); } } } }; template<class T> TreeNode<T> *Tree<T>::Create() { }; template<class T> void Tree<T>::CreateTree(T stop) { }; //线索二叉树: //头文件: template<class T> class ThreadNode { private: int LThread, RThread; //1为线索 0为左孩子 ThreadNode<T> *left; ThreadNode<T> *right; T data; public: ThreadNode(const T item):data(item), left(NULL), right(NULL), LThread(0), RThread(0){} ThreadNode<T> *GetLeft(void) const{return left;} void SetLeft(ThreadNode<T> *t){left = t;} ThreadNode<T> *GetRight(void) const{return right;} void SetRight(ThreadNode<T> *t){right = t;} T &GetData(){return data;} void SetData(const T &item) {data = item;} void SetLThread(const int d){LThread = d;} int GetLThread() {return LThread;} void SetRThread(const int d){RThread = d;} int GetRThread() {return RThread;} }; template<class T> class ThreadInTree //中序线索二叉树 { private: ThreadNode<T> *root; T stop; ThreadNode<T> *pre; public: ThreadInTree(ThreadNode<T> *t){root = t;} ThreadNode<T> *FIO(ThreadNode<T> *t); //返回中序线索二叉树t的中根序列的第一个结点 ThreadNode<T> *LIO(ThreadNode<T> *t); //返回中序线索二叉树t的中根序列的最后一个结点 ThreadNode<T> *PIO(ThreadNode<T> *t, ThreadNode<T> *p); //在以t为根结点的中序线索二叉树中搜索结点p的中根前驱结点 ThreadNode<T> *NIO(ThreadNode<T> *t, ThreadNode<T> *p); //在以t为根结点的中序线索二叉树中搜索结点p的中根后驱结点 void InOrderOf(ThreadNode<T> *t); //中序遍历以结点t为根结点的中序线索二叉树 void InsertLeft(ThreadNode<T> *p, ThreadNode<T> *s); void InsertRight(ThreadNode<T> *p, ThreadNode<T> *s); //插入一个结点p,作为结点s的右结点 ThreadNode<T> *GetRoot() {return root;} void SetRoot(ThreadNode<T> *t) {root = t;} T GetStop() {return stop;} void SetStop(T tostop) {stop = tostop;} void CreatThreadingTree(T tostop); ThreadNode<T> *Create(); void ThreadingTree(); void InThreading(ThreadNode<T> *t); ThreadNode<T> *First() {return FIO(root);} ThreadNode<T> *Last() {return LIO(root);} void InOrder() {InOrderOf(root);} ThreadNode<T> *search(ThreadNode<T> *t, T item); void DeleteLeft(ThreadNode<T> *p, ThreadNode<T> *s); void DeleteRight(ThreadNode<T> *p, ThreadNode<T> *s); //删除结点s的左子结点p }; //源文件: #include "ThreadNode.h" template<class T> ThreadNode<T> *ThreadInTree<T>::FIO(ThreadNode<T> *t) { if (t == NULL) return NULL; ThreadNode<T> *q = t; while((q->GetLThread()) == 0) q = q->GetLeft(); return q; } template<class T> ThreadNode<T> *ThreadInTree<T>::LIO(ThreadNode<T> *t) { if (t == NULL) return NULL; ThreadNode<T> *q = t; while((q->GetRThread()) == 0) q = q->GetRight(); return q; } template<class T> ThreadNode<T> *ThreadInTree<T>::PIO(ThreadNode<T> *t, ThreadNode<T> *p) { if ( t == NULL || p == NULL) return NULL; ThreadNode<T> *q; if (p == FIO(t)) return NULL; ThreadNode<T> *rp = p->GetLeft(); if((p->GetLThread()) == 1) return rp; q = LIO(rp); return q; } template<class T> ThreadNode<T> *ThreadInTree<T>::NIO(ThreadNode<T> *t, ThreadNode<T> *p) { if ( t == NULL || p == NULL) return NULL; ThreadNode<T> *q; if (p == LIO(t)) return NULL; ThreadNode<T> *rp = p->GetRight(); if((p->GetRThread()) == 1) return rp; q = FIO(rp); return q; } template<class T> void ThreadInTree<T>::InOrderOf(ThreadNode<T> *t) { if (t == NULL) return; ThreadNode<T> *q; for (q = FIO(t); q != NULL; q = NIO(t, q)) cout<<q->GetData()<<endl; } template<class T> void ThreadInTree<T>::InsertRight(ThreadNode<T> *p, ThreadNode<T> *s) { if ( p == NULL || s == NULL) return NULL; p->SetRight(s->GetRight()); p->SetRThread(s->GetRThread()); p->SetLeft(s); p->SetLThread(1); s->SetRight(p); s->SetRThread(0); if ((s->GetRThread()) == 0) { ThreadNode<T> *q = p->GetRight(); q = FIO(q); q->SetLeft(p); } } //AVL树: //头文件: template<class T> class AVLNode { public: T key; AVLNode<T> *llink; AVLNode<T> *rlink; int balance; int RANK; AVLNode(const T &item, AVLNode<T> *lptr = NULL, AVLNode<T> *rptr = NULL):key(item), llink(lptr), rlink(rptr), balance(0){} }; template<class T> class AVLTree { private: AVLNode<T> *head; T stop; AVLNode<T> **link(int a, AVLNode<T> *p); public: AVLTree(); AVLNode<T> *Search_Insert(T k); AVLNode<T> *SearchByPos(int k); void InsertByPos(int k, AVLNode<T> *q); AVLNode<T> *GetRoot() {return head->rlink;} void InOrder(AVLNode<T> *t) const; T GetStop() {return stop;} void SetStop(T tostop) {stop = tostop;} }; //源文件: #include "AVLNode.h" template<class T> AVLNode<T> ** AVLTree<T>::link(int a, AVLNode<T> *p) { if (a == -1) return (&(p->llink)); if (a == 1) return (&(p->rlink)); return NULL; }; template<class T> AVLNode<T> *AVLTree<T>::Search_Insert(T k) { int a; AVLNode<T> *t = head; AVLNode<T> *s = head->rlink; AVLNode<T> *p = head->rlink; AVLNode<T> *q; if (s == NULL) { q = new AVLNode<T>(k); head->rlink = q; head->llink = 0; return NULL; } while(true) { if (k == p->key) return p; if(k < p->key) { q = p->llink; if (q == NULL) { q = new AVLNode<T>(k); p->llink = q; break; } else { if (q->balance != 0) { t = p; s = q; } p = q; } } else { q = p->rlink; if (q == NULL) { q = new AVLNode<T>(k); p->rlink = q; break; } else { if (q->balance != 0) { t = p; s = q; } p = q; } } } //插入完成,下面调整平衡系数 if (k < s->key) a = -1; else a = 1; AVLNode<T> *r = *link(a, s); p = r; while(p != q) { if (k < p->key) { p->balance = -1; p = p->llink; } if (k > p->key) { p->balance = 1; p = p->rlink; } } if (s->balance == 0) { s->balance = a; head->llink = head->llink+1; return NULL; } else if(s->balance == -a) { s->balance = 0; return NULL; } else if (s->balance == a) { if (r->balance == a) { p = r; *link(a, s) = *link(-a, r); *link(-a, r) = s; s->balance = 0; r->balance = 0; } else if (r->balance == -a) { p = *link(-a, r); *link(a, r) = *link(a, p); *link(a, p) = r; *link(a, s) = *link(-a, p); *link(-a, p) = s; if (p->balance == a) { s->balance = -a; r->balance = 0; } if (p->balance == 0) { s->balance = 0; r->balance = 0; } if (p->balance == -a) { s->balance = 0; r->balance = a; } p->balance = 0; } if (s == t->rlink) t->rlink = p; else t->llink = p; } return NULL; }; template<class T> AVLNode<T> *AVLTree<T>::SearchByPos(int k) { int m = k; AVLNode<T> *p = head->rlink; while() { if(m == p->RANK)return p; else if (m < p->RANK) p = p->llink; else { m -= p->RANK; p = p->rlink; } } return NULL; }; template<class T> void AVLTree<T>::InsertByPos(int k, AVLNode<T> *q) { AVLNode<T> *t = head; AVLNode<T> *s = head->rlink; AVLNode<T> *p = head->rlink; AVLNode<T> *r; int a; int u, m; u = k; m = k; while(true) { if (m <= p->RANK) { p->RANK ++; r = p->llink; if (r == NULL) { p->llink = q; break; } else { if (r->balance != 0) { t = p; s = r; u = m; } p = r; } } else { m -= p->RANK; r = p->rlink; if (r == NULL) { p->rlink = q; break; } else { if (r->balance != 0) { t = p; s = r; u = m; } p = r; } } } //插入 q->RANK = 1; q->llink = NULL; q->rlink = NULL; q->balance = 0; //调整平衡系数 m = u; if (m < s->RANK) { r = s->llink; p = r; a = -1; } else { r = s->rlink; p = r; a = 1; m -= s->RANK; } while(p != q) { if (m < p->RANK) { p->balance = -1; p = p->llink; } if(m > p->RANK) { p->balance = 1; m -= p->RANK; p = p->rlink; } } if (s->balance == 0) { s->balance = a; head->llink = head->llink + 1; return; } if(s->balance == -a) { s->balance = 0; return; } if (s->balance == a) { if(r->balance == a) { p = r; *link(a, s) = *link(-a, r); *link(-a, s) = s; s->balance = 0; r->balance = 0; //update value of RANk if(a == 1)r->RANK = r->RANK + s->RANK; if(a == -1) s->RANK = s->RANK - r->RANK; } else if (r->balance == -a) { p = *link(-a, p); *link(a, r) = *link(a, p); *link(a, p) = r; *link(a, s) = *link(-a, p); *link(-a, p) = s; if (p->balance == a) { s->balance = -a; r->balance = 0; } if (p->balance == 0) { s->balance = 0; r->balance = 0; } if (p->balance == a) { s->balance = 0; r->balance = -a; } p->balance = 0; if(a == 1) { r->RANK = r->RANK - p->RANK; p->RANK = p->RANK - s->RANK; } if(a == -1) { p->RANK = p->RANK + r->RANK; s->RANK = s->RANK - p->RANK; } } if(s == t->rlink)t->rlink = p; else t->llink = p; } }; //哈夫曼二叉树: //头文件: template<class T> class HuffmanNode { private: HuffmanNode<T> *left; HuffmanNode<T> *right; T data; int weight; public: HuffmanNode(const T &item, HuffmanNode<T> *lptr = NULL, HuffmanNode<T> *rptr = NULL):data(item),left(lptr),right(rptr),weight(0){} HuffmanNode<T> *GetLeft(void) const {return left;} void SetLeft(HuffmanNode<T> *L) {left = L;} HuffmanNode<T> *Getright(void) const{return right;} void SetRight(HuffmanNode<T> *R) {right = R;} T &GetData() {return data;} void SetData(const T &item) {data = item;} int GetWeight() {return weight;} void SetWeight(const int w){weight = w;} }; const int m = 20; template<class T> class HuffmanTree { private: HuffmanNode<T> *root; T data[m]; int weight[m]; public: HuffmanTree(HuffmanNode<T> *t = NULL):root(t){} void CreateHuffmanTree(); HuffmanNode<T> *GetRoot() {return root;} void SetRoot(HuffmanNode<T> *t) {root = t;} void InputNodes(); }; //源文件: #include "HuffmanTree.h" template<class T> void HuffmanTree<T>::CreateHuffmanTree() { InputNodes(); HuffmanNode<T> *H[m]; HuffmanNode<T> *p1 = NULL, *p2 = NULL, *p, *t; int i = 0, j = 0; for (; i < m ; i ++) { H[i] = new HuffmanNode<T>(0); H[i]->SetData(data[i]); H[i]->SetWeight(weight[i]); H[i]->SetLeft(NULL); H[i]->SetRight(NULL); } for (i = 0; i < m-1; i ++) { t = new HuffmanNode<T>(0); p1 = H[i]; p2 = H[i+1]; t->SetWeight(p1->GetWeight() + p2->GetWeight()); t->SetLeft(p1); t->SetRight(p2); p = t; j = i+2; while(j < m && (p->GetWeight()) > (H[i]->GetWeight())) { H[i-1] = H[j]; j ++; } H[j-1] = p; root = H[m-1]; } }