C語言實現(xiàn)BST二叉排序樹的基本操作
更新時間:2021年09月22日 14:59:01 作者:似曾不相識
這篇文章主要為大家詳細介紹了C語言實現(xiàn)BST二叉排序樹的基本操作,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)BST二叉排序樹的基本操作代碼,供大家參考,具體內(nèi)容如下
BST-二叉排序樹的幾個基本操作。
頭文件聲明與函數(shù)定義
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
/**
* 定義節(jié)點
*/
typedef struct BSTNode{
ElemType data;//數(shù)據(jù)域
struct BSTNode *lchild,//左孩子
*rchild;//右孩子
}BSTNode;
/**
* 插入節(jié)點
*/
int BST_InsertNode(BSTNode** bstNode,ElemType e);
/**
* 創(chuàng)建BST樹
*/
void BST_Create(BSTNode** bstTree,ElemType* dataSet,int n);
/**
* 查找BST樹節(jié)點
*/
BSTNode* BST_SearchNode(BSTNode** bstNode,ElemType e);
/**
* 遍歷BST樹節(jié)點
*/
void BST_PrintNodes(BSTNode* bstNode);
函數(shù)編寫
#include "BSTree.h"
/**
* 插入節(jié)點
*/
int BST_InsertNode(BSTNode** bstNode,ElemType e){
//如果BST樹為空,直接創(chuàng)建根節(jié)點
if (*bstNode==NULL)
{
*bstNode=(BSTNode*)malloc(sizeof(BSTNode));
(*bstNode)->data=e;
(*bstNode)->lchild=NULL;
(*bstNode)->rchild=NULL;
return 1;
}
//如果BST樹不為空,則比較插入值與根節(jié)點值的大小關系
if ((*bstNode)->data==e)
return 0;//關鍵值相同,則插入失敗
else if ((*bstNode)->data>e)
return BST_InsertNode(&(*bstNode)->lchild,e);//大于插入值,將其作為左子樹節(jié)點
else if ((*bstNode)->data<e)
return BST_InsertNode(&(*bstNode)->rchild,e);//小于插入值,將其作為右子樹節(jié)點
}
/**
* 創(chuàng)建BST樹
*/
void BST_Create(BSTNode** bstTree,ElemType* dataSet,int n){
int i=0;
*bstTree=NULL;//BST樹初始化為空
while (i<n)
{
printf("%d\t",dataSet[i]);
BST_InsertNode(bstTree,dataSet[i++]);
}
printf("\n");
}
/**
* 查找BST樹節(jié)點
*/
BSTNode* BST_SearchNode(BSTNode** bstNode,ElemType e){
if (*bstNode==NULL)//判空
return *bstNode;
//查找結點
if ((*bstNode)->data==e)//驗證是否為根節(jié)點
return *bstNode;
else if ((*bstNode)->data>e)
{
return BST_SearchNode(&(*bstNode)->lchild,e);//如果小于根節(jié)點的值,查找左子樹
}else
{
return BST_SearchNode(&(*bstNode)->rchild,e);//如果大于根節(jié)點的值,查找右子樹
}
}
/**
* 遍歷BST樹節(jié)點
*/
void BST_PrintNodes(BSTNode* bstNode){
if (bstNode==NULL)//根節(jié)點判空
{
return;
}
//打印根節(jié)點的值
printf("%d\t",(bstNode)->data);
//從根節(jié)點開始遍歷
if (bstNode->lchild!=NULL)
BST_PrintNodes((bstNode)->lchild);//遍歷左子樹
if (bstNode->rchild!=NULL)
BST_PrintNodes(bstNode->rchild);//遍歷右子樹
}
測試
#include "BSTree.h"
int main(int argc,char** argv){
int i;
ElemType arr[]={45,24,53,45,12,24,68,25,36,96,100,25,64,78};//只有4個元素,因為關鍵字重復的元素不能被插入
BSTNode* bstNode=NULL;
BSTNode* bstTemp=NULL;
//創(chuàng)建BST樹
BST_Create(&bstNode,arr,sizeof(arr)/sizeof(ElemType));
printf("%d\t%d\n",bstNode,bstNode->data);
printf("%d\t%d\n",bstNode,bstNode->lchild->data);
//查找結點
bstTemp=BST_SearchNode(&bstNode,53);
printf("the aimed node is %d,\n",bstNode->data);
//遍歷BST樹的所有節(jié)點
BST_PrintNodes(bstNode);
printf("\n");
}
貼上測試結果如下,【插入和遍歷的節(jié)點數(shù)量不一致是因為-如果BST樹中的節(jié)點關鍵值相同,就終止插入操作】

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
C語言實現(xiàn)動態(tài)順序表的實現(xiàn)代碼
這篇文章主要介紹了C語言實現(xiàn)動態(tài)順序表的實現(xiàn)代碼的相關資料,動態(tài)順序表在內(nèi)存中開辟一塊空間,可以隨我們數(shù)據(jù)數(shù)量的增多來擴容,需要的朋友可以參考下2017-08-08
C++ 動態(tài)內(nèi)存分配詳解(new/new[]和delete/delete[])
這篇文章主要介紹了C++ 動態(tài)內(nèi)存分配詳解(new/new[]和delete/delete[]),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05

