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

代码实现如下:

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

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

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

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


#include 
using namespace std;
#include 
#include 
#include 
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 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: "<heightTree() << endl;
			break;
		case 4:
			btr->leafNode();
			break;
		default:
			cout << "输入错误!" << endl;
			delete btr; //释放内存
		}
		system("pause");
		system("cls");
	}
	return 0;
}

反思:

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

你可能感兴趣的:(数据结构和算法,数据结构,c++)