二叉树:顺序存储结构实现基本操作(C++)

代码实现如下:

1.输入二叉树(采用先根遍历)和输出二叉树(采用层次遍历)

2.找对应结点在数组中的存储位置

3.寻找父母结点和孩子结点

4.求树的高度(递归和非递归)和所有的叶子结点


#include <iostream>
using namespace std;
#include <queue>
#include <cstring>
#include <cmath>
const int MAX = 100;
#define Elemtype char
//二叉树基本知识
//顺序存储结构,使用下表之间的关系代表二叉树,一般为完全二叉树

//构建二叉树
class BTree {
public:
	BTree() {
		this->length = 0;
		for (int i = 0; i < MAX; i++) {
			this->data[i] = '\0';
		}
	}
	~BTree() {
		delete[] data;
	}
	void InitBtree(int n);//构建二叉树
	
	void printBtree();//输出二叉树
	int findNode(Elemtype e);//找对应结点
	void childNode(Elemtype e);//找孩子结点
	void parentNode(Elemtype e);//找父母结点
	int heightTree();//树的高度
	void leafNode();//叶子结点
	
	int length;
	Elemtype* data = new Elemtype[MAX];//在堆中分配内存
};

void BTree::InitBtree(int n) {
	char ch;
	
	cin >> ch;
	if (ch == '#') {
		return;
	} else {
		if (n < MAX) {
			this->data[n] = ch;
			this->length++;
			
			cout << ch << "左子树:" ;
			InitBtree(2 * n);
			cout << ch << "右子树:";
			InitBtree(2 * n + 1);
		} else {
			cout << "数组已满,无法插入新结点" << endl;
		}
	}
}

void BTree::printBtree() {
	if (this->length == 0) {
		cout << "二叉树为空" << endl;
		return;
	}
	queue<int> q;
	//层次遍历
	q.push(1);
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		cout << this->data[n] << " ";
		
		if (n * 2 <= this->length) {
			q.push(n * 2);
		}
		if (n * 2 + 1 <= this->length) {
			q.push(n * 2 + 1);
		}
	}
	cout << endl; //换行
}


void menu() {
	cout << "-------------" << endl;
	cout << "0.退出" << endl;
	cout << "1.求孩子结点" << endl;
	cout << "2.求父母结点" << endl;
	cout << "3.求树的高度" << endl;
	cout << "4.求叶子结点" << endl;
	cout << "--------------" << endl;
}

int BTree::findNode(Elemtype e) {
	int n = -1;
	for (int i = 1; i <= this->length; i++) {
		if (e == this->data[i]) {
			n = i;
		}
	}
	return n;
}

void BTree::childNode(Elemtype e) {
	if (this->length == 0) {
		cout << "二叉树为空" << endl;
		return;
	}
	int num = findNode(e);
	if (num == -1) {
		cout << "此结点在二叉树中不存在!" << endl;
	} else {
		if (num * 2 < this->length) {
			cout << "孩子结点:" << this->data[num * 2] << ' ' << this->data[num * 2 + 1] << endl;
		} else {
			cout << "无孩子结点" << endl;
		}
		
	}
}

void BTree::parentNode(Elemtype e) {
	if (this->length == 0) {
		cout << "二叉树为空" << endl;
		return;
	}
	int num = findNode(e);
	if (num == -1) {
		cout << "此结点在二叉树中不存在!" << endl;
	} else {
		if (num != 1) {
			cout << "父母结点:" << this->data[num / 2] << endl;
		} else {
			cout << "无父母结点" << endl;
		}
		
	}
}

int BTree::heightTree()
{
	if(this->length==0){
		return 0;//树为空时
	}else
	{
		return log(this->length+1)/log(2);
	}
}

void BTree::leafNode()
{
	cout << "叶子结点:" << endl;
	for(int i=this->heightTree();i<2*this->heightTree();i++){
		cout << this->data[i] << ' ';
	}
	cout << endl;
}

int main() {
	BTree* btr = new BTree();
	cout << "请输入根节点:" << endl;
	btr->InitBtree(1);
	cout << "输出二叉树:" << endl;
	btr->printBtree();
	
	int choice;
	while (1) {
		menu();
		cout << "请选择:" ;
		cin >> choice;
		
		Elemtype e;
		switch (choice) {
		case 0:
			exit(0);
		case 1:
			cout << "请输入结点:";
			cin >> e;
			btr->childNode(e);
			break;
		case 2:
			cout << "请输入结点:";
			cin >> e;
			btr->parentNode(e);
			break;
		case 3:
			cout << "Height: "<<btr->heightTree() << endl;
			break;
		case 4:
			btr->leafNode();
			break;
		default:
			cout << "输入错误!" << endl;
			delete btr; //释放内存
		}
		system("pause");
		system("cls");
	}
	return 0;
}

反思:

数据结构中的顺序存储结构均要在堆中分配空间,C语言-》malloc,C++-》new;

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Daxiuy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值