二叉树基本操作代码(C语言)

前言

C语言实现二叉树,在某些基本操作中需要借助队列结构实现,C++可以不借助其他结构。

构造二叉树结点

BTNode* BinaryTreeCreate (BTDataType* str, int* pi)
{
   
	if (str[*pi] == '#')
	{
   
		(*pi)++;
		return NULL;
	}
	BTNode* root = (BTNode*)malloc(sizeof(BTNode));

	if (root == NULL)
	{
   
		perror("malloc fail");
		exit(-1);
	}
	root->data = str[(*pi)++];

	root->left = BinaryTreeCreate(str, pi);

	root->right = BinaryTreeCreate(str, pi);

	return root;

}

二叉树销毁

void BinaryTreeDestory(BTNode** root)
{
   
	if (root == NULL)
	{
   
		return;
	}
	BinaryTreeDestory(&((*root)->left));
	BinaryTreeDestory(&((*root)->right));
	free(*root);
	*root = NULL;
}

二叉树节点个数

int BinaryTreeSize(BTNode* root)
{
   
	return root == NULL ? 0 : BinaryTreeSize(root->right) + BinaryTreeSize(root->left)+1;
}

二叉树叶子节点个数

int BinaryTreeLeafSize(BTNode* root)
{
   
	if (root == NULL)
	{
   
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
   
		return 1;
	}
	return BinaryTreeLeafSize(root->right) + BinaryTreeLeafSize(root->left);
}

二叉树第k层节点个数

int BinaryTreeLevelKSize(BTNode* root, int k)
{
   
	if (root == NULL)
	{
   
		return 0;
	}
	if (k == 1)
	{
   
		return 1;
	}
	return BinaryTreeLevelKSize(root->right,k-1) + BinaryTreeLevelKSize(root->left,k-1);
}

二叉树查找值为x的节点

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
   
	if (root == NULL)
	{
   
		return NULL;
	}
	if (root->data == x)
	{
   
		return root;
	}

	BTNode* left = BinaryTreeFind(root->left,x);
	if (left)
	{
   
		return left;
	}
	BTNode* right = BinaryTreeFind(root->right, x);
	
	if (right)
	{
   
		return right;
	}
	return NULL;
}

二叉树前序遍历

void BinaryTreePrevOrder(BTNode* obj)
{
   
	if (obj == NULL)
	{
   
		return;
	}
	printf("%c ", obj->data);
	BinaryTreePrevOrder(obj->left);
	
	BinaryTreePrevOrder(obj->right);
}

二叉树中序遍历

void inorder(BTNode* obj)
{
   
	if (obj == NULL)
	{
   
		return;
	}
	inorder(obj->left)
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是二叉树基本操作 C 语言代码,包括二叉树的创建、遍历(先序、中序、后序)、查找、插入、删除等操作: ```c #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 创建二叉树 struct TreeNode* createBinaryTree() { int val; scanf("%d", &val); if (val == -1) { return NULL; } struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = val; root->left = createBinaryTree(); root->right = createBinaryTree(); return root; } // 先序遍历 void preorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preorderTraversal(root->left); preorderTraversal(root->right); } // 中序遍历 void inorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } // 后序遍历 void postorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } // 查找节点 struct TreeNode* search(struct TreeNode* root, int val) { if (root == NULL || root->val == val) { return root; } if (val < root->val) { return search(root->left, val); } else { return search(root->right, val); } } // 插入节点 struct TreeNode* insert(struct TreeNode* root, int val) { if (root == NULL) { struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } if (val < root->val) { root->left = insert(root->left, val); } else if (val > root->val) { root->right = insert(root->right, val); } return root; } // 删除节点 struct TreeNode* delete(struct TreeNode* root, int val) { if (root == NULL) { return root; } if (val < root->val) { root->left = delete(root->left, val); } else if (val > root->val) { root->right = delete(root->right, val); } else { if (root->left == NULL && root->right == NULL) { free(root); root = NULL; } else if (root->left == NULL) { struct TreeNode* temp = root; root = root->right; free(temp); } else if (root->right == NULL) { struct TreeNode* temp = root; root = root->left; free(temp); } else { struct TreeNode* temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = delete(root->right, temp->val); } } return root; } // 主函数 int main() { struct TreeNode* root = createBinaryTree(); printf("Preorder Traversal: "); preorderTraversal(root); printf("\n"); printf("Inorder Traversal: "); inorderTraversal(root); printf("\n"); printf("Postorder Traversal: "); postorderTraversal(root); printf("\n"); struct TreeNode* node = search(root, 3); if (node != NULL) { printf("Node %d is found.\n", node->val); } else { printf("Node is not found.\n"); } root = insert(root, 3); printf("Inorder Traversal after insertion: "); inorderTraversal(root); printf("\n"); root = delete(root, 3); printf("Inorder Traversal after deletion: "); inorderTraversal(root); printf("\n"); return 0; } ``` 其中,`struct TreeNode` 是二叉树的节点结构体,包括节点值 `val` 和左右子节点指针 `left` 和 `right`。`createBinaryTree` 函数用于创建二叉树,遇到值为 -1 的节点表示该节点为空。`preorderTraversal`、`inorderTraversal`、`postorderTraversal` 分别实现了先序、中序、后序遍历。`search` 函数用于查找二叉树中是否存在指定值的节点,`insert` 函数用于插入一个新节点,`delete` 函数用于删除指定节点。最后,主函数展示了创建二叉树、遍历二叉树、查找、插入和删除节点等基本操作

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值