【算法导论】C++参考源码之队列、二叉树
2013-08-17 16:32
423 查看
简单队列的实现
简单的二叉树实现,包括:前、中、后序遍历、计算节点数、计算叶子节点数、计算度数为1的节点数、层次遍历
#include"queue.h"为上面队列的头文件实现
#include <iostream> using namespace std; //----------------------------------------------------------- template<typename T> struct Node { Node(T d) : data(d), next(NULL) {} T data; Node* next; }; //----------------------------------------------------------- template<typename T> class LinkQueue { public: LinkQueue(T d); void EnQueue(T d); bool DelQueue(T& d); void visitAllNode(); T getFront() { return front->next->data; } bool Empty(){ return length == 0; } private: Node<T>* front; Node<T>* rear; int length; }; //----------------------------------------------------------- template<typename T> LinkQueue<T>::LinkQueue(T d) : length(0), front(NULL), rear(NULL) { Node<T>* p = new Node<T>(d); if(!p) exit(1); front = rear = p; } //----------------------------------------------------------- template<typename T> void LinkQueue<T>::EnQueue( T d ) { Node<T>* p = new Node<T>(d); if(!p) exit(1); rear->next = p; rear = p; ++length; } //----------------------------------------------------------- template<typename T> bool LinkQueue<T>::DelQueue(T& d) { if (front == rear) { return false; } else { Node<T>* p = front->next; front->next = p->next; if (p == rear) { rear = front; } d = p->data; delete p; --length; return true; } } //----------------------------------------------------------- template<typename T> void LinkQueue<T>::visitAllNode() { Node<T>* p = front->next; while( p->next != NULL ) { cout << p->data << "-->"; p = p->next; } cout << p->data << endl; } //----------------------------------------------------------- int main() { LinkQueue<int> p(0); p.EnQueue(1); p.EnQueue(2); p.EnQueue(3); p.visitAllNode(); int temp; p.DelQueue(temp); p.visitAllNode(); p.EnQueue(4); p.visitAllNode(); return 0; }
简单的二叉树实现,包括:前、中、后序遍历、计算节点数、计算叶子节点数、计算度数为1的节点数、层次遍历
#include <iostream> #include "queue.h" using namespace std; //---------------------------------------------------------- struct tree { tree(int d) : data(d), left(NULL), right(NULL) {} int data; tree *left, *right; }; //---------------------------------------------------------- class Btree { static int n; static int m; public: tree* root; Btree() : root(NULL) {} void create_Btree(int d); void preOrder(tree* p); void inOrder(tree* p); void postOrder(tree* p); void display1() { preOrder(root); cout << endl; } void display2() { inOrder(root); cout << endl; } void display3() { postOrder(root); cout << endl; } int count(tree* p); int findleaf(tree* p); int findnode(tree* p); void Hierarchy( tree* p ); }; //---------------------------------------------------------- int Btree::m = 0; int Btree::n = 0; //---------------------------------------------------------- void Btree::create_Btree( int d ) { tree* p = new tree(d); if (root == NULL) { root = p; return; } tree* current = root; tree* back; while (current != NULL) { back = current; if (d < current->data) current = current->left; else current = current->right; } if (d < back->data) back->left = p; else back->right = p; } //---------------------------------------------------------- void Btree::preOrder( tree* p ) { if (p != NULL) { cout << p->data << " "; preOrder(p->left); preOrder(p->right); } } //---------------------------------------------------------- void Btree::inOrder( tree* p ) { if (p != NULL) { inOrder(p->left); cout << p->data << " "; inOrder(p->right); } } //---------------------------------------------------------- void Btree::postOrder( tree* p ) { if (p != NULL) { postOrder(p->left); postOrder(p->right); cout << p->data << " "; } } //---------------------------------------------------------- int Btree::count( tree* p ) { if (p == NULL) { return 0; } return count(p->left) + count(p->right) + 1; } //---------------------------------------------------------- int Btree::findleaf( tree* p ) { if (p == NULL) { return 0; } else { if (p->left == NULL && p->right == NULL) { return m += 1; } else { findleaf(p->left); findleaf(p->right); } return m; } } //---------------------------------------------------------- int Btree::findnode( tree* p ) { if (p == NULL) { return 0; } else { if (p->left != NULL && p->right != NULL) { findnode(p->left); findnode(p->right); } if (p->left != NULL && p->right == NULL) { n += 1; findnode(p->left); } if (p->left == NULL && p->right != NULL) { n += 1; findnode(p->right); } return n; } } //层次遍历 //---------------------------------------------------------- void Btree::Hierarchy( tree* p ) { tree* pTree = new tree(0); LinkQueue<tree*> _queue(pTree); if(NULL == p) return; _queue.EnQueue(p); while(!_queue.Empty()) { tree* temp = _queue.getFront(); cout << temp->data << " "; _queue.DelQueue(p); if(p->left) _queue.EnQueue(p->left); if(p->right) _queue.EnQueue(p->right); } cout << endl; } //---------------------------------------------------------- int main() { Btree btree; int A[] = {7,4,2,3,15,35,6,45,55,20,1,14,56,57,58}; int size = sizeof(A) / sizeof(A[0]); for (int i = 0; i < size; ++i) { btree.create_Btree(A[i]); } btree.display1(); btree.display2(); btree.display3(); cout << btree.count(btree.root) << endl; cout << btree.findleaf(btree.root) << endl; cout << btree.findnode(btree.root) << endl; btree.Hierarchy(btree.root); return 0; }
#include"queue.h"为上面队列的头文件实现
相关文章推荐
- 【算法导论】C++参考源码之堆排序中的优先级队列
- 【算法导论】C++参考源码之堆排序
- 【算法导论】C++参考源码之基础排序
- 【算法导论】C++参考源码之线性时间排序
- 关于 优先队列(C语言) ——(参考算法导论)
- 基于算法导论6.5用最大堆实现的优先队列(C++)
- C++最大堆实现priority_queue优先级队列(算法导论)
- 关于 队列(C语言) ——(参考算法导论)
- 【算法导论】C++源码之快速排序
- 算法导论——lec 12 平摊分析与优先队列
- 栈与队列_第10章_基本数据结构_算法导论
- 【数据结构与算法】二叉树递归与非递归遍历(附完整源码)
- 队列--参考源码(转)
- 优先队列 算法导论 java语言
- 二叉排序树节点的删除(C++,算法导论),前中后序遍历(递归/非递归,栈实现),按层次遍历(队列实现)
- 算法导论10:栈链表的简化、队列的数组实现 2016.1.10
- 【算法导论】22.3 图的深度优先搜索-C++实现
- 【刘庆源码共享】稀疏线性系统求解算法MGMRES(m) 之 矩阵类(C++)
- 算法学习记录五(C++)--->两个栈实现队列
- 两个堆栈实现一个队列和一叠两个队列实现【算法导论课后题】