您的位置:首页 > 编程语言 > C语言/C++

【算法导论】C++参考源码之队列、二叉树

2013-08-17 16:32 423 查看
简单队列的实现

#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"为上面队列的头文件实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息