二叉树的基本操作

#include <stdio.h>
#include <stdlib.h>

typedef struct BiTNode {
	int data;
	struct BiTNode* lChild;
	struct BiTNode* rChild;
} BiTNode, *BiTTree;

// 创建二叉排序树
void CreateBiTTree(BiTTree* b, int data) {
	if (*b == NULL) {
		*b = (BiTNode*)malloc(sizeof(BiTNode));
		(*b)->data = data;
		(*b)->lChild = NULL;
		(*b)->rChild = NULL;
	} else {
		if (data < (*b)->data) {
			CreateBiTTree(&((*b)->lChild), data);
		} else {
			CreateBiTTree(&((*b)->rChild), data);
		}
	}
}

// 查找节点
BiTNode* FindNode(BiTNode* b, int x) {
	BiTNode* p;
	if (b == NULL) {
		return NULL;
	} else if (b->data == x) {
		return b;
	} else {
		p = FindNode(b->lChild, x);
		if (p != NULL) {
			return p;
		} else {
			return FindNode(b->rChild, x);
		}
	}
}

// 删除二叉树
void DestroyBiTTree(BiTNode* b) {
	if (b != NULL) {
		DestroyBiTTree(b->lChild);
		DestroyBiTTree(b->rChild);
		free(b);
	}
}

// 输出树高的算法
int BiTNodeDepth(BiTNode* b) {
	int lChildDep, rChildDep;
	if (b == NULL) {
		return 0;
	} else {
		lChildDep = BiTNodeDepth(b->lChild);
		rChildDep = BiTNodeDepth(b->rChild);
		return (lChildDep > rChildDep) ? (lChildDep + 1) : (rChildDep + 1);
	}
}

// 统计二叉树的结点数目
int CountNode(BiTNode* b) {
	if (b == NULL) {
		return 0;
	} else {
		return CountNode(b->lChild) + CountNode(b->rChild) + 1;
	}
}

int main() {
	BiTNode* root = NULL;
	
	// 创建二叉排序树
	int data[] = {8, 3, 10, 1, 6, 14, 4, 7, 13};
	int n = sizeof(data) / sizeof(data[0]);
	for (int i = 0; i < n; i++) {
		CreateBiTTree(&root, data[i]);
	}
	
	// 测试查找节点
	int x = 6;
	BiTNode* node = FindNode(root, x);
	if (node != NULL) {
		printf("Node with data %d found in the tree.\n", x);
	} else {
		printf("Node with data %d not found in the tree.\n", x);
	}
	
	// 测试输出树高
	int depth = BiTNodeDepth(root);
	printf("Tree depth: %d\n", depth);
	
	// 测试统计结点数目
	int nodeCount = CountNode(root);
	printf("Total nodes in the tree: %d\n", nodeCount);
	
	// 销毁二叉树
	DestroyBiTTree(root);
	root = NULL;
	
	return 0;
}
//        8
//      /   \
//     3    10
//    / \     \
//   1   6    14
//  / \   /
// 4   7 13

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
叉树是一种非常重要的数据结构,它的基本操作包括创建、销毁、遍历、查找等。下面是二叉树基本操作的实现方法: 1. 创建二叉树:通过前序遍历的数组构建二叉树,其中 '#' 表示空节点。具体实现方法可以参考引用中的 BinaryTreeCreate 函数。 2. 销毁二叉树:遍历二叉树,依次释放每个节点的内存空间。具体实现方法可以参考引用中的 BinaryTreeDestory 函数。 3. 遍历二叉树:二叉树的遍历包括前序遍历、中序遍历、后序遍历和层序遍历。具体实现方法可以参考引用中的 BinaryTreePrevOrder、BinaryTreeInOrder、BinaryTreePostOrder 和 BinaryTreeLevelOrder 函数。 4. 查找二叉树节点:在二叉树中查找值为 x 的节点,具体实现方法可以参考引用中的 BinaryTreeFind 函数。 5. 计算二叉树节点个数:计算二叉树中节点的个数,具体实现方法可以参考引用[2]中的 BinaryTreeSize 函数。 6. 计算二叉树叶子节点个数:计算二叉树中叶子节点的个数,具体实现方法可以参考引用中的 BinaryTreeLeafSize 函数。 7. 计算二叉树第 k 层节点个数:计算二叉树中第 k 层节点的个数,具体实现方法可以参考引用中的 BinaryTreeLevelKSize 函数。 8. 判断二叉树是否是完全二叉树:判断二叉树是否是完全二叉树,具体实现方法可以参考引用中的 BinaryTreeComplete 函数。 9. 计算二叉树的深度:计算二叉树的深度,具体实现方法可以参考引用中的 BinaryTreeDeep 函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ac果

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

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

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

打赏作者

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

抵扣说明:

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

余额充值