树与二叉树的C++实现

最近复习数据结构,复习到了树和二叉树部分,将相关的一些操作,如树的定义、初始化、插入节点,和中序遍历、层序遍历,以及计算树的深度等操作依次用c++实现了一遍,后续可能还有其他操作的实现代码,在此记录。

#include <iostream>
#include <queue>
using namespace std;

typedef struct Treenode//定义树节点
{
	int data;
	struct Treenode* Leftnode;
	struct Treenode* Reftnode;
}Tnode,*Tp;

void InitTree(Tp &tree,int data)//初始化空指针
{
	tree = new Tnode;
	if (tree)
	{
		tree->Leftnode = nullptr;
		tree->Reftnode = nullptr;
		tree->data = data;
	}
}

void CreateTreeNode(Tp &tree,int data)//创建树节点并递归插入
{
	if (tree == nullptr)
		InitTree(tree, data);
	else
	{
		if (data < tree->data)
			CreateTreeNode(tree->Leftnode,data);
		else
			CreateTreeNode(tree->Reftnode,data);
	}
}

void MiddleVisit(Tp tree)//递归方式的中序遍历
{
	if (tree != nullptr)
	{
		MiddleVisit(tree->Leftnode);
		cout << tree->data << " ";
		MiddleVisit(tree->Reftnode);
	}
}

int CulDepth(Tp tree)//利用递归计算树的深度
{
	if (tree == nullptr)
		return 0;
	else
	{
		int l = CulDepth(tree->Leftnode);
		int r = CulDepth(tree->Reftnode);
		return l > r ? l + 1 : r + 1;
	}
}

void LayerVisit(Tp tree)//利用队列打印树的层序遍历
{
	if (tree == nullptr)
		cout << "This is a empty tree" << endl;
	queue<Tp> q;
	q.push(tree);
	while(!q.empty())
	{
		cout << q.front()->data << " ";
		if (q.front()->Leftnode != nullptr)
			q.push(q.front()->Leftnode);
		if(q.front()->Reftnode != nullptr)
			q.push(q.front()->Reftnode);
		q.pop();
	}
}

int main()
{
	Tp tree=nullptr;
	int data;
	cout << "Please enter the value of the  rootnode of the tree:" << endl;
	while (cin>>data)
	{
		cout << "Please enter the value of the current node of the tree:" << endl;
		CreateTreeNode(tree,data);
	}

	cout << "The middle order traversal of this tree results in the following" << endl;
	MiddleVisit(tree);
	cout << endl;

	cout << "The layer traversal of this tree results in the following" << endl;
	LayerVisit(tree);
	cout << endl;

	int deepth = CulDepth(tree);
	cout <<"The depth of the tree is:"<< deepth << endl;

	delete tree;
	tree = nullptr;
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡二叉树是一种自平衡的二叉搜索,它的左右子的高度差不超过1。下面是平衡二叉树C++实现: ```cpp #include <iostream> using namespace std; // 平衡二叉树结点的定义 struct AVLNode { int data; // 数据 int height; // 高度 AVLNode* left; // 左子 AVLNode* right; // 右子 AVLNode(int val) : data(val), height(1), left(nullptr), right(nullptr) {} }; // 获取结点高度 int getHeight(AVLNode* node) { if (node == nullptr) { return 0; } return node->height; } // 获取平衡因子 int getBalanceFactor(AVLNode* node) { if (node == nullptr) { return 0; } return getHeight(node->left) - getHeight(node->right); } // 右旋 AVLNode* rightRotate(AVLNode* node) { AVLNode* leftChild = node->left; AVLNode* rightGrandChild = leftChild->right; leftChild->right = node; node->left = rightGrandChild; node->height = max(getHeight(node->left), getHeight(node->right)) + 1; leftChild->height = max(getHeight(leftChild->left), getHeight(leftChild->right)) + 1; return leftChild; } // 左旋 AVLNode* leftRotate(AVLNode* node) { AVLNode* rightChild = node->right; AVLNode* leftGrandChild = rightChild->left; rightChild->left = node; node->right = leftGrandChild; node->height = max(getHeight(node->left), getHeight(node->right)) + 1; rightChild->height = max(getHeight(rightChild->left), getHeight(rightChild->right)) + 1; return rightChild; } // 插入结点 AVLNode* insert(AVLNode* node, int val) { if (node == nullptr) { return new AVLNode(val); } if (val < node->data) { node->left = insert(node->left, val); } else if (val > node->data) { node->right = insert(node->right, val); } else { return node; } node->height = max(getHeight(node->left), getHeight(node->right)) + 1; int balanceFactor = getBalanceFactor(node); if (balanceFactor > 1 && val < node->left->data) { return rightRotate(node); } if (balanceFactor < -1 && val > node->right->data) { return leftRotate(node); } if (balanceFactor > 1 && val > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } if (balanceFactor < -1 && val < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } return node; } // 中序遍历 void inOrder(AVLNode* node) { if (node == nullptr) { return; } inOrder(node->left); cout << node->data << " "; inOrder(node->right); } // 测试 int main() { AVLNode* root = nullptr; int arr[] = { 9, 5, 10, 0, 6, 11, -1, 1, 2 }; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) { root = insert(root, arr[i]); } inOrder(root); // 输出:-1 0 1 2 5 6 9 10 11 return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值