二叉树的链表表示 C++实现

/*
* File name  : LBtree.cpp
* Function   : 二叉树的链表表示 C++实现
* Created on : 2016年5月11日
* Author     : beijiwei@qq.com
* Copyright  : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。
任何单位和个人不经本人允许不得用于商业用途
* Input      :  ABCD##E##F##GHI##J##K##
*
*/
#include <cstdio>
#include <iostream>

using namespace std;

#define MAX 20
typedef char Elem_t;

typedef struct node{
	Elem_t data;
	//struct node * parent;
	struct node * Lchild;
	struct node * Rchild;
	int level;
	int node_count;
}Node,*pNode,*pBtree;


/****************************************************************************/
// for void Btree_traverse_by_level(Btree &T, int current_pos);  //按层遍历
template<typename T>
class Pqueue {
private:
	T array[MAX];

	int head;
	int tail;

public:
	int size;
	bool queue_in(T one) {
		if (tail == MAX - 1) {
			return false;
		}

		for (int i = 0; i <= tail; i++) {
			if (i == tail) {
				array[i] = one;
				break;
			}
			else
				if (one.level < array[i].level) {
					for (int j = tail + 1; j > i; j--) {
						array[j] = array[j - 1];
					}
					array[i] = one;
					break;
				}
		}
		tail++;
		size++;
		return true;
	}
	bool queue_out(T & A) {
		if (head == tail) {
			return false;
		}
		A = array[head++];
		size--;
		return true;
	}
	Pqueue() {
		for (int i = 0; i < 10; i++) {
			array[i].level = 0;
		}
		head = 0;
		tail = 0;
		size = 0;
	}
	~Pqueue() {
		head = 0;
		tail = 0;
		size = 0;
	}

};

/****************************************************************************/


void Btree_create(pBtree  & T, int level);
int  Btree_get_depth(pBtree &T);

void Btree_dlr(pBtree &T);//先序遍历
void Btree_ldr(pBtree &T);//中序遍历
void Btree_lrd(pBtree &T);//后序遍历
void Btree_traverse_by_level(pBtree &T);  //按层遍历
int  Btree_get_node_count(pBtree &T);
bool Btree_dlr_find_elem(pBtree &T, Elem_t elem);

pBtree  BT;
// ABCD##E##F##GHI##J##K##
int main(int argc, char** argv)
{
	int k = 0;
	Btree_create(BT,1);
	
	k = Btree_get_depth(BT);
	cout << "The depth of BT is : " << k << endl;
	k = Btree_get_node_count(BT);
	cout << "The total node count of BT is : " << k << endl;
	Btree_dlr(BT);//先序遍历
	cout << endl;

	Btree_ldr(BT);//中序遍历
	cout << endl;

	Btree_lrd(BT);//后序遍历
	cout << endl;
	
	cout << "Btree_traverse_by_level : " << endl;
	Btree_traverse_by_level(BT);

	
	cout << "C is in Btree ?" << Btree_dlr_find_elem(BT, 'C') << endl;
	cout << "M is in Btree ?" << Btree_dlr_find_elem(BT,  'M') << endl;
	
	return 0;
}


void Btree_create(pBtree  & T,int level)
{
	static int ncount = 0;
	Elem_t data;
	
	cin >> data;
	if (data == '#') {
		T = NULL;
		return;
	}
	else {
		ncount++;
	}
	T= new Node;

	T->data= data;
	T->level = level;
	
	

	Btree_create(T->Lchild,level+1);
	Btree_create(T->Rchild,level+1);
	T->node_count = ncount;
}

int Btree_get_depth(pBtree &T)
{
	static int depth = 0;;
	if (T == NULL)
		return 0;
	if (depth < T->level)
		depth=T->level;
	Btree_get_depth(T->Lchild);
	Btree_get_depth(T->Rchild);

	return depth;
}

int Btree_get_node_count(pBtree &T)
{
	return T->node_count;// T.node_count;
}
bool Btree_dlr_find_elem(pBtree &T, Elem_t elem) { //查找元素
	bool tmp = false;
	if (T) {
		if (T->data == elem)
			return true;
		tmp= Btree_dlr_find_elem(T->Lchild, elem);
		if (tmp)
			return tmp;
		tmp= Btree_dlr_find_elem(T->Rchild, elem);
		if (tmp)
			return tmp;
	}
	return false;
}


void Btree_dlr(pBtree &T) { //先序遍历
	if (T == NULL)
		return;
	cout << T->data << "\t";
	Btree_dlr(T->Lchild);
	Btree_dlr(T->Rchild);
}



void Btree_ldr(pBtree &T) { //中序遍历
	if (T == NULL)
		return;
	Btree_ldr(T->Lchild);
	cout << T->data << "\t";
	Btree_ldr(T->Rchild);
}


void Btree_lrd(pBtree &T) { //后序遍历
	if (T == NULL)
		return;
	Btree_lrd(T->Lchild);
	Btree_lrd(T->Rchild);
	cout << T->data << "\t";
}

Pqueue<Node> Q;
Node tmp;
void Btree_traverse_by_level(pBtree &T) { //按层遍历
	if (T == NULL)
		return;
	Q.queue_in(*T);
	Btree_traverse_by_level(T->Lchild);
	Btree_traverse_by_level(T->Rchild);

	if (T->level==1)
	{
		for (int i = 0; i < T->node_count; i++)
		{
			Q.queue_out(tmp);
			cout << tmp.data << "\t";
		}
		cout << endl;

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值