二叉查找树的实现(using C)

本程序主要实现二叉树的建树,排序输入的数据,查找数据的功能。



建树:1、输入采用数组方式,方便扩展;

          2、反复使用tree_insert来建树,tree_insert主要是解决在哪里加结点和新加结点和老结点的关系;



排序:直接使用中序遍历,得到从小到大的输出



查找:二叉查找树存在的最大价值在于快速的查找,实现见代码

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

typedef struct BiTreeNode{
    int date;
    struct BiTreeNode *lchild, *rchild, *parent;
}BiTreeNode,*BiTree;

BiTree root;
int a[100]={0};

void tree_insert(BiTree bt, int key)
{
    BiTree x,y,z;//y is father, x is son, z is temp
    z=(BiTreeNode*)malloc(sizeof(BiTreeNode));//ask room
    x=bt;
    while(x!=NULL)//find the location for inserting node
    {
        y=x;//record the father of inserting location
        if(key>x->date)
            x=x->rchild;
        else
            x=x->lchild;
    }
    x=z;//put the real room in the inserting location
    x->date=key;
    x->parent=y;
    if(y==NULL)root=x;//if it is a empty tree,
                        //transfer the real room location to root(global varible)

    if(y==NULL)//if it is a empty tree, the root has no parent, do nothing
           {
               return;//x->date=key;
           }
    else {
        //tell the father that whether the child is
        //his right child or left child
           if(key < y->date)
                y->lchild=x;
           else
                y->rchild=x;
           }

}

int tree_search(BiTree x,int key)
{

    if (x==NULL) return 0;//can't find key
    if (x->date==key) return 1;//find key
    if (key < (x->date))//
        tree_search(x->lchild,key);
    else
        tree_search(x->rchild,key);
}

void tree_inorder_walk(BiTree x)
{
    if (x!=NULL)
    {
        tree_inorder_walk(x->lchild);
        printf("%d ",x->date);
        tree_inorder_walk(x->rchild);
    }
}

int main()
{
    int i,n;

    root=NULL;

    //input the information of the BST
    printf("how many nodes in this tree:");
    scanf("%d",&n);
    printf("input the key of nodes:");
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);//12,5,8,18


    //insert
    for(i=1;i<=n;i++)
       tree_insert(root,a[i]);


    //check if there is a established tree
    printf("\ndescribe the tree:\n");
    printf("root is %d\n",root->date);

    //sort the nums
    printf("\nsort:");
    tree_inorder_walk(root);

    //search node
    printf("\n\nsearch node:\n");
    printf("can we find 5 in the BST: %d \n",tree_search(root,5));
    printf("can we find 6 in the BST: %d \n",tree_search(root,6));

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C++程序,实现随机产生一组关键字,利用二叉排序树的插入算法建立二叉排序树,然后删除某一指定关键字元素。 ```cpp #include <iostream> #include <cstdlib> using namespace std; // 二叉排序树的节点结构体 struct BSTNode { int key; BSTNode* left; BSTNode* right; }; // 插入节点到二叉排序树中 void BSTInsert(BSTNode*& root, int key) { if (!root) { root = new BSTNode{key, NULL, NULL}; return; } if (key < root->key) { BSTInsert(root->left, key); } else { BSTInsert(root->right, key); } } // 在二叉排序树中查找指定关键字的节点 BSTNode* BSTSearch(BSTNode* root, int key) { if (!root) { return NULL; } if (key == root->key) { return root; } else if (key < root->key) { return BSTSearch(root->left, key); } else { return BSTSearch(root->right, key); } } // 从二叉排序树中删除指定关键字的节点 void BSTDelete(BSTNode*& root, int key) { if (!root) { return; } if (key == root->key) { if (!root->left && !root->right) { // 删除叶子节点 delete root; root = NULL; } else if (root->left && !root->right) { // 删除只有左子树的节点 BSTNode* temp = root; root = root->left; delete temp; } else if (!root->left && root->right) { // 删除只有右子树的节点 BSTNode* temp = root; root = root->right; delete temp; } else { // 删除有两个子树的节点 BSTNode* temp = root->right; while (temp->left) { temp = temp->left; } root->key = temp->key; BSTDelete(root->right, temp->key); } } else if (key < root->key) { BSTDelete(root->left, key); } else { BSTDelete(root->right, key); } } int main() { // 随机生成一组关键字 int keys[10]; for (int i = 0; i < 10; i++) { keys[i] = rand() % 100; } // 建立二叉排序树 BSTNode* root = NULL; for (int i = 0; i < 10; i++) { BSTInsert(root, keys[i]); } // 输出原始的二叉排序树 cout << "Original BST: "; for (int i = 0; i < 10; i++) { cout << keys[i] << " "; } cout << endl; // 删除指定关键字的节点 int key; cout << "Enter the key to be deleted: "; cin >> key; BSTDelete(root, key); // 输出删除指定关键字后的二叉排序树 cout << "BST after deleting " << key << ": "; for (int i = 0; i < 10; i++) { if (keys[i] != key) { cout << keys[i] << " "; } } cout << endl; return 0; } ``` 注意,这个程序中的删除操作只能删除第一个匹配的关键字,如果有多个相同的关键字,只会删除第一个。如果需要删除所有匹配的关键字,需要修改删除函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值