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

本文介绍了如何使用C++实现简单队列,包括队列的头部插入和尾部删除操作。同时,文章详细讲解了二叉树的各种遍历方法(前序、中序、后序)以及计算节点数、叶子节点数、度为1的节点数和层次遍历的方法。队列部分引用了‘queue.h’头文件进行实现。
摘要由CSDN通过智能技术生成

简单队列的实现

#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"为上面队列的头文件实现


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值