#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "drawtree.h"
#if 0
typedef int DataType_t;
typedef struct BSTreeNode
{
DataType_t data;
struct BSTreeNode *lchild;
struct BSTreeNode *rchild;
}BSTnode_t;
#endif
BSTnode_t * BSTree_Create(DataType_t KeyVal)
{
BSTnode_t *Root = (BSTnode_t *)calloc(1,sizeof(BSTnode_t));
if (NULL == Root)
{
perror("Calloc memory for Root is Failed");
exit(-1);
}
Root->data = KeyVal;
Root->lchild = NULL;
Root->rchild = NULL;
return Root;
}
BSTnode_t * BSTree_NewNode(DataType_t KeyVal)
{
BSTnode_t *New = (BSTnode_t *)calloc(1,sizeof(BSTnode_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
New->data = KeyVal;
New->lchild = NULL;
New->rchild = NULL;
return New;
}
bool BSTree_InsertNode(BSTnode_t *Root,DataType_t KeyVal)
{
BSTnode_t *Proot = Root;
BSTnode_t * New = BSTree_NewNode(KeyVal);
if (NULL == New)
{
printf("Create NewNode Error\n");
return false;
}
if (NULL == Root)
{
Root = New;
}
else
{
while(Proot)
{
if (Proot->data == New->data)
{
printf("Can Not Insert,.....\n");
return false;
}
else
{
if( New->data < Proot->data )
{
if (Proot->lchild == NULL)
{
Proot->lchild = New;
break;
}
Proot = Proot->lchild;
}
else
{
if (Proot->rchild == NULL)
{
Proot->rchild = New;
break;
}
Proot = Proot->rchild;
}
}
}
}
return true;
}
int main(int argc, char const *argv[])
{
BSTnode_t *root = BSTree_Create(10);
BSTree_InsertNode(root,5);
BSTree_InsertNode(root,20);
BSTree_InsertNode(root,7);
BSTree_InsertNode(root,12);
BSTree_InsertNode(root,8);
BSTree_InsertNode(root,3);
BSTree_InsertNode(root,25);
BSTree_InsertNode(root,11);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术