【数据结构】链式二叉树的创建

binaryTreeNode.h

#pragma once
#include<iostream>
template<class T>
struct binaryTreeNode
{
	T element;
	binaryTreeNode<T>* leftChild, * rightChild;
	binaryTreeNode() {}
	binaryTreeNode(const T& element, binaryTreeNode<T>* leftChild = NULL,
		binaryTreeNode<T>* rightChild = NULL) {
		this->element = element;
		this->leftChild = leftChild;
		this->rightChild = rightChild;
	}
};


linkedBinaryTree.h



#ifndef linkedBinaryTree_
#define linkedBinaryTree_
#include"binaryTreeNode.h"
#include<queue>
#include<iostream>
using namespace std;
template<class T>
class linkedBinaryTree
{
public:
	linkedBinaryTree();
	~linkedBinaryTree();
	bool empty()const;
	int size()const;
	binaryTreeNode<T>* createBinaryTree(binaryTreeNode<T>* t);
	void createBinaryTree();
	int height_recursion(binaryTreeNode<T>* t);//递归求高度
	int height_level();//层次遍历求高度
	int height();
	void preOrder(void (*theVisit)(binaryTreeNode<T>*));
	void inOrder(void (*theVisit)(binaryTreeNode<T>*));
	void postOrder(void (*theVisit)(binaryTreeNode<T>*));
	void levelOrder(void(*theVisit)(binaryTreeNode<T>*));
	void preOrderOutput();
	void inOrderOutput();
	void postOrderOutput();
	void erase();
private:
	binaryTreeNode<T>* root;
	int treesize;
	static void (*visit)(binaryTreeNode<T>*);
	static void preOrder(binaryTreeNode<T>*);
	static void inOrder(binaryTreeNode<T>*);
	static void postOrder(binaryTreeNode<T>*);
	static void dispose(binaryTreeNode<T>*);
	static void output(binaryTreeNode<T>*);
};
template<class T>
void(*linkedBinaryTree<T>::visit)(binaryTreeNode<T>*) = NULL;
#endif

linkedBinaryTree.cpp

#include "linkedBinaryTree.h"
#include<queue>

template<class T>
linkedBinaryTree<T>::linkedBinaryTree()
{
	root = NULL;
	treesize = 0;
}

template<class T>
linkedBinaryTree<T>::~linkedBinaryTree()
{
	erase();
}

template<class T>
bool linkedBinaryTree<T>::empty() const
{
	return treesize == 0;
}

template<class T>
int linkedBinaryTree<T>::size() const
{
	return treesize;
}

template<class T>
binaryTreeNode<T>* linkedBinaryTree<T>::createBinaryTree(binaryTreeNode<T>* t)
{
	T element;
	cin >> element;
	if (element == '#') {
		return NULL;
	}
	else {
		treesize++;
		t = new binaryTreeNode<T>(element);
		t->leftChild = createBinaryTree(t->leftChild);
		t->rightChild = createBinaryTree(t->rightChild);
	}
	return t;
}


template<class T>
void linkedBinaryTree<T>::createBinaryTree()
{
	root = createBinaryTree(root);
}

template<class T>
int linkedBinaryTree<T>::height_recursion(binaryTreeNode<T>* t)
{
	if (t == NULL) {
		return 0;
	}
	int h1 = height_recursion(t->leftChild);
	int h2 = height_recursion(t->rightChild);
	if (h1 > h2) {
		return h1 + 1;
	}
	else {
		return h2 + 1;
	}
}

template<class T>
int linkedBinaryTree<T>::height_level()
{
	queue<binaryTreeNode<T>*> q;
	q.push(root);
	int n = 0,len;
	binaryTreeNode<T>* cur;
	while (!q.empty()) {
		n++;
		len = q.size();
		for (int i = 0; i < len; i++) {
			cur = q.front();
			q.pop();
			if (cur->leftChild != NULL) {
				q.push(cur->leftChild);
			}
			if (cur->rightChild != NULL) {
				q.push(cur->rightChild);
			}
		}
	}
	return n;
}

template<class T>
int linkedBinaryTree<T>::height()
{
	return height_recursion(root);
}

template<class T>
void linkedBinaryTree<T>::preOrder(void(*theVisit)(binaryTreeNode<T>*))
{
	visit = theVisit;
	preOrder(root);
}

template<class T>
void linkedBinaryTree<T>::inOrder(void(*theVisit)(binaryTreeNode<T>*))
{
	visit = theVisit;
	inOrder(root);
}

template<class T>
void linkedBinaryTree<T>::postOrder(void(*theVisit)(binaryTreeNode<T>*))
{
	visit = theVisit;
	postOrder(root);
}

template<class T>
void linkedBinaryTree<T>::levelOrder(void(*theVisit)(binaryTreeNode<T>*))
{
	queue<binaryTreeNode<T>*>q;
	q.push(root);
	while (q != NULL) {
		binaryTreeNode<T>* cur = q.front();
		q.pop();
		theVisit(cur);

		if (cur->leftChild != NULL) {
			q.push(cur->leftChild);
		}
		if (cur->rightChild != NULL) {
			q.push(cur->rightChild);
		}
	}

}

template<class T>
void linkedBinaryTree<T>::preOrderOutput()
{
	preOrder(output);
	cout << "\n";
}

template<class T>
void linkedBinaryTree<T>::inOrderOutput()
{
	inOrder(output);
	cout << "\n";
}

template<class T>
void linkedBinaryTree<T>::postOrderOutput()
{
	postOrder(output);
	cout << "\n";
}

template<class T>
void linkedBinaryTree<T>::erase()
{
	postOrder(dispose);
	root = NULL;
	treesize = 0;
}

template<class T>
void linkedBinaryTree<T>::preOrder(binaryTreeNode<T>* t)
{
	if (t != NULL) {
		linkedBinaryTree<T>::visit(t);
		preOrder(t->leftChild);
		preOrder(t->rightChild);
	}
}

template<class T>
void linkedBinaryTree<T>::inOrder(binaryTreeNode<T>* t)
{
	if (t != NULL) {
		inOrder(t->leftChild);
		linkedBinaryTree<T>::visit(t);
		inOrder(t->rightChild);
	}
}

template<class T>
void linkedBinaryTree<T>::postOrder(binaryTreeNode<T>* t)
{
	if (t != NULL) {
		postOrder(t->leftChild);
		postOrder(t->rightChild);
		linkedBinaryTree<T>::visit(t);
	}

}

template<class T>
void linkedBinaryTree<T>::dispose(binaryTreeNode<T>* t)
{
	delete t;
}

template<class T>
void linkedBinaryTree<T>::output(binaryTreeNode<T>* t)
{
	cout << t->element << " ";
}




test.cpp

#include<iostream>
using namespace std;
#include"linkedBinaryTree.h"
#include"linkedBinaryTree.cpp"
int main() {
	linkedBinaryTree<char> binaryTree;
	binaryTree.createBinaryTree();
	cout << "前序遍历:";
	binaryTree.preOrderOutput();
	cout << "中序遍历:";
	binaryTree.inOrderOutput();
	cout << "后序遍历:";
	binaryTree.postOrderOutput();
	cout << "该二叉树的高度为:" << binaryTree.height()<<"\n";
	cout << "该二叉树的高度为:" << binaryTree.height_level()  << "\n";
	return 0;
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值