二叉树的数组表示 C++实现(添加 按层遍历)

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

using namespace std;

#define SIZE 100
#define MAX 20

typedef char Elem_t;

typedef struct {
	Elem_t data;
	int parent;
	int Lchild;
	int Rchild;
	int level;
}Node;

typedef struct {
	Node depot[MAX];
	int depth;
	int node_count;
}Btree;

/****************************************************************************/
// 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(Btree & T, int current_pos, int parent_pos, int left, int father_level);
int  Btree_get_depth(Btree &T, int current_pos);

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


int main(int argc, char** argv)
{
	Btree  BT;
	int k = 0;
	Btree_create(BT,0,-1,2,0);
	k = Btree_get_depth(BT, 0);
	cout <<"The depth of BT is : "<<k  << endl;
	BT.depth = k;
	k = Btree_get_depth(BT, 0);
	cout << "The depth of BT is : " << k << endl;

	Btree_dlr(BT, 0);//先序遍历
	cout << endl;

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

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

	cout << "\nBtree node count is :"<<Btree_get_node_count(BT) << endl;
	cout << "C is in Btree ?" << Btree_dlr_find_elem(BT, 0, 'C')<<endl;
	cout << "M is in Btree ?" << Btree_dlr_find_elem(BT, 0, 'M') << endl;
	return 0; 
}

void Btree_create(Btree & T, int current_pos,int parent_pos,int left,int father_level)
{
	Elem_t tmp;
	static int  next_pos = 0;
	
	cin >> tmp;
	if (left == 2) {//root
		T.depth =0;
	}
	
	if (tmp == '#' ) {// node is null 
		if (left == 1) {
			T.depot[parent_pos].Lchild = -2;
		}
		else {
			T.depot[parent_pos].Rchild = -2;

		}
		return;
	}
	if(T.depth <= father_level)
		T.depth = father_level+1;
	
	T.depot[current_pos].data = tmp;
	T.depot[current_pos].parent = parent_pos;

	T.depot[current_pos].level = father_level +1;
	
	next_pos = current_pos + 1;
	T.depot[current_pos].Lchild = next_pos;
	
	parent_pos = current_pos;
	
	Btree_create(T, T.depot[current_pos].Lchild, parent_pos,1, father_level+1);
	T.depot[current_pos].Rchild = next_pos;
	Btree_create(T, T.depot[current_pos].Rchild, parent_pos,0, father_level+1);
	T.node_count = next_pos;
	
	
}

int Btree_get_depth(Btree &T,int current_pos)
{
	int left_depth;
	int right_depth;
	static int node_count = 0;

	if (T.depth != -1) {
		return T.depth;// in fact, only you,work
	}

	if (T.depot[current_pos].Lchild == -2 && T.depot[current_pos].Rchild == -2) {
		return 1;
	}
	else {
		if (T.depot[current_pos].Lchild != -2)
		{
			left_depth = Btree_get_depth(T,++node_count);
		}
		if (T.depot[current_pos].Rchild != -2) {
			right_depth = Btree_get_depth(T, ++node_count);
		}

		if (left_depth > right_depth) {
			return left_depth+1;
		}
		else {
			return right_depth + 1;
		}

	}
}

int Btree_get_node_count(Btree &T)
{
	return T.node_count;
}
bool Btree_dlr_find_elem(Btree &T, int current_pos, Elem_t elem) { //查找元素

	int left = T.depot[current_pos].Lchild;
	int right = T.depot[current_pos].Rchild;
	bool left_ret = false;
	bool right_ret = false;
	
	//cout << T.depot[current_pos].data << "\t";
	if (T.depot[current_pos].data == elem)
	{
		return true;
	}

	if (T.depot[current_pos].Lchild != -2)
	{
		left_ret= Btree_dlr_find_elem(T, left,elem);
	}
	if (T.depot[current_pos].Rchild != -2) {
		right_ret= Btree_dlr_find_elem(T, right,elem);
	}
	return (left_ret || right_ret);
}



void Btree_dlr(Btree &T, int current_pos) { //先序遍历

	int left = T.depot[current_pos].Lchild;
	int right = T.depot[current_pos].Rchild;

	cout << T.depot[current_pos].data << "\t";
	if (T.depot[current_pos].Lchild != -2)
	{
		Btree_dlr(T, left);
	}
	if (T.depot[current_pos].Rchild != -2) {
		Btree_dlr(T, right);
	}
}



void Btree_ldr(Btree &T, int current_pos) { //中序遍历

	int left = T.depot[current_pos].Lchild;
	int right = T.depot[current_pos].Rchild;

	
	if (T.depot[current_pos].Lchild != -2)
	{
		Btree_ldr(T, left);
	}
	cout << T.depot[current_pos].data << "\t";

	if (T.depot[current_pos].Rchild != -2) {
		Btree_ldr(T, right);
	}
}


void Btree_lrd(Btree &T, int current_pos) { //后序遍历

	int left = T.depot[current_pos].Lchild;
	int right = T.depot[current_pos].Rchild;

	if (T.depot[current_pos].Lchild != -2)
	{
		Btree_lrd(T, left);
	}


	if (T.depot[current_pos].Rchild != -2) {
		Btree_lrd(T, right);
	}

	cout << T.depot[current_pos].data << "\t";
}

Pqueue<Node> Q;
Node tmp;
void Btree_traverse_by_level(Btree &T, int current_pos) { //按层遍历

	int left = T.depot[current_pos].Lchild;
	int right = T.depot[current_pos].Rchild;
	Q.queue_in(T.depot[current_pos]);

	if (T.depot[current_pos].Lchild != -2)
	{
		Btree_traverse_by_level(T, left);
	}

	if (T.depot[current_pos].Rchild != -2) {
		Btree_traverse_by_level(T, right);
	}
	if (Q.size == T.node_count)
	{
		for (int i = 0; i <T.node_count; i++)
		{
			Q.queue_out(tmp);
			cout << tmp.data << "\t";
		}
		cout << endl;
		
	}
}


void Btree_add_node(Btree &T, int father_pos, Elem_t elem, int left) //only left can insert
{
	if (left == 1) { // add left node
		T.depot[father_pos].Lchild = ++T.node_count;
		T.depot[father_pos].Lchild = -2;
		T.depot[T.node_count].data = elem;
		T.depot[T.node_count].level = T.depot[father_pos].level + 1;
		T.depot[T.node_count].parent = father_pos;
		T.depot[T.node_count].Lchild = -2;
		T.depot[T.node_count].Rchild = -2;
		if(T.depth < T.depot[T.node_count].level)
		T.depth = T.depot[T.node_count].level;
	}
	else {
		T.depot[father_pos].Rchild = ++T.node_count;
		T.depot[T.node_count].data = elem;
		T.depot[T.node_count].level = T.depot[father_pos].level + 1;
		T.depot[T.node_count].parent = father_pos;
		T.depot[T.node_count].Lchild = -2;
		T.depot[T.node_count].Rchild = -2;
	}

}


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二叉树是一种常见的数据结构,遍历二叉树有多种方法,包括先序遍历、中序遍历和后序遍历。以下是在C语言中用数组表示二叉树并进行遍历的示例代码: ```c #include <stdio.h> // 定义二叉树节点结构 struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; }; // 构建二叉树 struct TreeNode nodes[10]; // 使用数组表示二叉树 void buildTree() { for (int i = 0; i < 10; i++) { nodes[i].data = i; nodes[i].left = NULL; nodes[i].right = NULL; } nodes[0].left = &nodes[1]; nodes[0].right = &nodes[2]; nodes[1].left = &nodes[3]; nodes[1].right = &nodes[4]; nodes[2].left = &nodes[5]; nodes[2].right = &nodes[6]; } // 先序遍历 void preOrder(struct TreeNode *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } // 中序遍历 void inOrder(struct TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } } // 后序遍历 void postOrder(struct TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } } int main() { buildTree(); printf("先序遍历结果:"); preOrder(&nodes[0]); printf("\n中序遍历结果:"); inOrder(&nodes[0]); printf("\n后序遍历结果:"); postOrder(&nodes[0]); return 0; } ``` 以上代码定义了一个二叉树的结构,使用数组表示节点,并实现了先序、中序和后序遍历的方法。在主函数中构建了一个二叉树,并进行了遍历操作,打印出遍历的结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值