BST(C语言)

/****************************************************

            BST.h

 ***************************************************/

// 定义节点

typedef struct node

{ struct node * parent;

struct node *left;

struct node *right;

int key;

}Node;

void Tree_Insert(Node ** p_node,Node *z);

void  Inorder_Tree_Walk(Node * root);

Node * Tree_Search(Node *root,int k);

Node * Tree_Minimum(Node * root);

Node * Tree_Maximum(Node * root);

Node * Tree_Successor(Node * x);

Node * Tree_Predecessor(Node * x);

void Transplant(Node ** p_node,Node * u,Node *v);

void Tree_Delet(Node ** p_node,Node * u);





/*************************************************

            BST.c

包括插入节点、删除节点、中序遍历(排序)、查找节点、前继、后继、最小节点、最大节点

 ************************************************/



#include "BST.h"

#include <stdlib.h>

#include <stdio.h>

//function--插入元素

//p_node-- 指向root的指针的指针

//z--指向插入元素的指针

 /******************************

  iterative version

  *****************************/

void Tree_Insert(Node ** p_node,Node *z)

{ Node *y=NULL;// 初始化辅助节点指针

Node *x=*p_node;// 节点x指向头节点

//迭代查找待插入节点z的父节点

    while(x!=NULL)

{ y=x;

if(x->key<z->key)

x=x->right;

else

x=x->left;

}

    z->parent=y;// 节点

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是我重新设计的C语言版本的BST树模块: ``` // bst.h // 定义二叉搜索树节点类型 typedef struct bst_node { int key; struct bst_node* left; struct bst_node* right; } BSTNode; // 创建一个新的节点 BSTNode* create_node(int key); // 插入一个节点,返回根节点 BSTNode* insert_node(BSTNode* root, int key); // 删除一个节点,返回根节点 BSTNode* delete_node(BSTNode* root, int key); // 查找一个节点,返回指向该节点的指针 BSTNode** search_node(BSTNode** root, int key); // 遍历二叉搜索树,并将节点的key值存储到数组中 void traverse_bst(BSTNode* root, int* arr, int* index); // 释放二叉搜索树的内存 void free_bst(BSTNode* root); ``` ```c // bst.c #include "bst.h" #include <stdlib.h> // 创建一个新的节点 BSTNode* create_node(int key) { BSTNode* node = (BSTNode*)malloc(sizeof(BSTNode)); node->key = key; node->left = NULL; node->right = NULL; return node; } // 插入一个节点,返回根节点 BSTNode* insert_node(BSTNode* root, int key) { if (root == NULL) { return create_node(key); } if (key < root->key) { root->left = insert_node(root->left, key); } else if (key > root->key) { root->right = insert_node(root->right, key); } return root; } // 删除一个节点,返回根节点 BSTNode* delete_node(BSTNode* root, int key) { if (root == NULL) { return NULL; } if (key < root->key) { root->left = delete_node(root->left, key); } else if (key > root->key) { root->right = delete_node(root->right, key); } else { if (root->left == NULL) { BSTNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { BSTNode* temp = root->left; free(root); return temp; } BSTNode* temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->key = temp->key; root->right = delete_node(root->right, temp->key); } return root; } // 查找一个节点,返回指向该节点的指针 BSTNode** search_node(BSTNode** root, int key) { if (*root == NULL || (*root)->key == key) { return root; } if (key < (*root)->key) { return search_node(&((*root)->left), key); } else { return search_node(&((*root)->right), key); } } // 遍历二叉搜索树,并将节点的key值存储到数组中 void traverse_bst(BSTNode* root, int* arr, int* index) { if (root == NULL) { return; } traverse_bst(root->left, arr, index); arr[*index] = root->key; (*index)++; traverse_bst(root->right, arr, index); } // 释放二叉搜索树的内存 void free_bst(BSTNode* root) { if (root == NULL) { return; } free_bst(root->left); free_bst(root->right); free(root); } ``` 这个BST树模块包含了创建节点、插入节点、删除节点、查找节点、遍历和释放内存等基本操作。在使用时,可以通过调用这些函数来实现对BST树的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值