【算法導論】C++參考源碼之隊列、二叉樹

簡單隊列的實現

#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"爲上面隊列的頭文件實現


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章