二叉树链式结构的实现

对于二叉树链式结构的实现,理解二叉树的概念至关重要。在二叉树的创建以及求二叉树叶子节点的个数等其他操作时,都是先从二叉树的概念入手,一步一步完成二叉树的创建及其他操作,接下来看一下二叉树的概念

  • 二叉树的概念
    • 空树
    • 根节点+根节点的左子树+根节点的右子树
  • 接下来是二叉树的具体实现
    BinaryTree.h实现
#pragma once
typedef char DataType;
typedef struct BNode{
struct BNode* left;
struct BNode* right;
DataType data;
}BNode;
BNode* BuyBinTreeNode(DataType data);
BNode* CreateBinTree(DataType* array, int size,DataType invalid);
BNode* _CreateBinTree(DataType* array, int size, int* index, int invalid);
void LevelOrder(BNode* root);
BNode* Find(BNode* root, DataType data);
int GetKLevelNodeCount(BNode* root, int K);
int GetLeafCount(BNode* root);
void PreOrder(BNode* root);
void DestroyBinTree(BNode** root);
void TestBinTree();

BinaryTree.c实现

#include"BinaryTree.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
BNode* BuyBinTreeNode(DataType data) {
 BNode* pNewNode = (BNode*)malloc(sizeof(BNode));
 if (pNewNode == NULL) {
  assert(0);
  return NULL;
 }
 pNewNode->left = NULL;
 pNewNode->right = NULL;
 pNewNode->data = data;
 return pNewNode;
}
BNode* CreateBinTree(DataType* array, int size,DataType invalid) {
 int index = 0;
 return _CreateBinTree(array, size, &index,invalid);
}
BNode* _CreateBinTree(DataType* array, int size,int* index,int invalid) {
 //用树的概念
 //根节点
 BNode* pRoot = NULL;
 if (*index < size&&'$'!=array[*index]) {
  pRoot = BuyBinTreeNode(array[*index]);
  ++(*index);
  //根的左子树
  pRoot->left = _CreateBinTree(array, size, index, invalid);
  //根的右子树
  ++(*index);
  pRoot->right = _CreateBinTree(array, size, index, invalid);
 }
 return pRoot;
}
int GetKLevelNodeCount(BNode* root, int K) {
 if (root == NULL || K <= 0) {
  return 0;
 }
 if (K == 1) {
  return 1;
 }
 return GetKLevelNodeCount(root->left, K - 1) + GetKLevelNodeCount(root->right, K - 1);
}
int GetLeafCount(BNode* root) {
 if (root == NULL) {
  return 0;
 } else if (root->left == NULL && root->right == NULL) {
  return 1;
 }
 return GetLeafCount(root->left) + GetLeafCount(root->right);
}
void PreOrder(BNode* root) {
 if (root) {
  printf("%c ", root->data);
  PreOrder(root->left);
  PreOrder(root->right);
 }
}
void InOrder(BNode* root) {
 if (root) {
  InOrder(root->left);
  printf("%c ", root->data);
  InOrder(root->right);
 }
}
void PostOrder(BNode* root) {
 if (root) {
  PostOrder(root->left);
  PostOrder(root->right);
  printf("%c ", root->data);
 }
}
BNode* Find(BNode* root, DataType data) {
 if (root == NULL) {
  return NULL;
 } else if (root->data == data) {
  return root;
 }
 BNode* pNode = NULL;
 if (pNode = Find(root->left, data)) {
  return pNode;
 }
 return Find(root->right, data);
}
void DestroyBinTree(BNode** root) {
 assert(root);
 if (*root) {
  DestroyBinTree(&(*root)->left);
  DestroyBinTree(&(*root)->right);
  free(*root);
  *root = NULL;
 }
}
void TestBinTree() {
 char* str = "ABD$$$CE$$F";
 BNode* pRoot = CreateBinTree(str,strlen(str),'$');
 printf("前序遍历结果为:");
 PreOrder(pRoot);
 printf("\n");
 printf("中序遍历结果为:");
 InOrder(pRoot);
 printf("\n");
 printf("后序遍历结果为:");
 PostOrder(pRoot);
 printf("\n");
 printf("K=3: %d\n", GetKLevelNodeCount(pRoot, 3));
 if (Find(pRoot, 'E')) {
  printf("找到了!!!\n");
 } else {
  printf("没找到!!!\n");
 }
 DestroyBinTree(&pRoot);
}
在C语言中,要实现二叉树链式结构,通常我们会使用结构体(struct)来定义节点,每个节点包含一个值、左子节点和右子节点的指针。对于“参照节点”这个概念,可能指的是在不复制整个节点的情况下,直接引用或操作其他节点,这是一种常见的内存管理策略,用于节省内存。 以下是一个简单的二叉搜索树(BST)节点及其链式结构实现示例: ```c // 定义二叉树节点 typedef struct TreeNode { int val; // 节点值 struct TreeNode* left; // 左子节点指针 struct TreeNode* right; // 右子节点指针 } TreeNode; // 插入新节点的方法,参照已存在节点进行插入 void insert(TreeNode** root, int val) { if (*root == NULL) { // 如果根节点为空,创建一个新的节点 *root = (TreeNode*)malloc(sizeof(TreeNode)); (*root)->val = val; (*root)->left = NULL; (*root)->right = NULL; } else { // 否则遍历树 if (val < (*root)->val) { insert(&((*root)->left), val); // 对左子树递归调用 } else { insert(&((*root)->right), val); // 对右子树递归调用 } } } // 示例如何使用 int main() { TreeNode* root = NULL; insert(&root, 50); insert(&root, 30); insert(&root, 70); // ... return 0; } ``` 在这个例子中,`insert`函数就是参照已有的节点进行操作,而不需要为每个新节点分配新的内存。当插入时,如果找到空节点,则在那里创建新节点;否则根据比较结果选择左或右子树继续递归查找。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值