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

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

2016-04-28 19:01 453 查看
<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;
}

}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: