C++ BST插入、删除、搜索
我正在解决一个问题,它说: 编写一个程序,处理二进制搜索树上的以下查询:C++ BST插入、删除、搜索,c++,tree,insert,binary-search-tree,C++,Tree,Insert,Binary Search Tree,我正在解决一个问题,它说: 编写一个程序,处理二进制搜索树上的以下查询: i x:在BST中插入x d x:从BST中删除x 输入格式 第1行包含一个整数Q,即查询数 接下来的Q行的形式为ix或dx 输出格式 对于每个查询,打印x在BST中的位置 如果节点的位置为p,则其左和右子节点的位置分别为2*p和2*p+1 根节点的位置为1 在删除节点9之前,一切正常。然后节点14的位置出来为5。如图所示:最初 15 / \ 9
15
/ \
9 25
/ \ /
8 13 18
/ / \
7 11 19
删去第9条后
15
/ \
11 25
/ \ /
8 13 18
/ \
7 19
插入14后
15
/ \
11 25
/ \ /
8 14 18
/ / \
7 13 19
正确的格式应该是
15
/ \
11 25
/ \ /
8 13 18
/ \ \
7 14 19
#包括
#定义ll long long
使用名称空间std;
ll位置=1;
结构节点
{
int数据;
BSTNode*左,*右;
};
BSTNode*getNewNode(整型数据)
{
BSTNode*newNode=newbstnode();
新建节点->数据=数据;
newNode->left=newNode->right=NULL;
return newNode;//返回新节点的地址
}
BSTNode*插入(BSTNode*根,int数据)
{
if(root==NULL){
root=getNewNode(数据);
}
else if(数据)
{
根->左=插入(根->左,数据);
}
else if(数据>根目录->数据)
{
根->右=插入(根->右,数据);
}
返回根;
}
BSTNode*findMin(BSTNode*root)
{
如果(根->左==NULL)
{
返回根;
}
其他的
findMin(根->左);
}
布尔搜索(BSTNode*root,int数据)
{
if(root==NULL)
{
返回0;
}
如果(数据)
{
位置=2*位置;
搜索(根->左,数据);
}
else if(数据>根目录->数据)
{
位置=2*位置+1;
搜索(根->右,数据);
}
其他的
{
coutleft,数据);
}
else if(数据>根目录->数据)
{
根->右=删除(根->右,数据);
}
else//Found
{
//案例1:没有儿童
如果(根->左==根->右==空)
{
删除根;
root=NULL;
}
//案例2:一个孩子
else if(root->left==NULL)
{
BSTNode*temp=root;
根=根->右;
删除临时文件;
}
else if(root->right==NULL)
{
BSTNode*temp=root;
根=根->左;
删除临时文件;
}
//案例3:两名儿童
其他的
{
BSTNode*temp=findMin(根->右);
根->数据=临时->数据;
根->右=删除(根->右,根->数据);
}
返回根;
}
}
int main()
{
BSTNode*root=NULL;//rootptr-指向节点的指针
//树是空的
int n,输入,数据,del;
字符c;
cin>>n;
而(n--)
{
cin>>c;
cin>>输入;
如果(c=='i')
{
根=插入(根,输入);
搜索(根,输入);
}
如果(c=='d')
{
搜索(根,输入);
删除(根,输入);
}
不能在同一条线上
正确的格式应为
你的图表错了。14是13岁的孩子。
假设要插入的节点是二叉搜索树,如果未找到给定节点,则需要在搜索路径中保存最后一个父节点。并将新节点插入函数insert()
中获得的最后一个父节点。该程序不是递归的
下面是C语言中的一个例子
#include<stdio.h>
#include<stdlib.h>
struct t_Data
{
int m_Info;
};
struct t_Node
{
struct t_Data m_Data;
struct t_Node* m_LeftChild;
struct t_Node* m_RightChild;
};
typedef struct t_Node* t_BinarySortTree;
/* return targetNode or the last node in the path */
int SearchBST(t_BinarySortTree T, int aGivenInfo, struct t_Node* lastParentNode, struct t_Node* *result)
{
if(!T)
{
*result = lastParentNode;
return 0;
}
else if (aGivenInfo == (*T).m_Data.m_Info)
{
*result = T;
return 1;
}
else if(aGivenInfo < (*T).m_Data.m_Info)
{
lastParentNode = T;
return SearchBST((*T).m_LeftChild,aGivenInfo,lastParentNode,result);
}
else
{
lastParentNode = T;
return SearchBST((*T).m_RightChild,aGivenInfo,lastParentNode,result);
}
}
void InsertBST(t_BinarySortTree *T, struct t_Data newData)
{
int status;
struct t_Node* result;
status = SearchBST(*T,newData.m_Info,NULL,&result);
/* condition: fail to search for 'newData' in the binary sort tree */
if (!status)
{
struct t_Node* newNode;
newNode = (struct t_Node*)malloc(sizeof(struct t_Node));
(*newNode).m_Data = newData;
(*newNode).m_LeftChild = NULL;
(*newNode).m_RightChild = NULL;
/* condition: result == NULL */
if (!result)
{
*T = newNode;
}
else if (newData.m_Info < (*result).m_Data.m_Info)
{
(*result).m_LeftChild = newNode;
}
/* condition: newData.m_Info > (*result).m_Data.m_Info */
else
{
(*result).m_RightChild = newNode;
}
}
}
int main(void)
{
t_BinarySortTree aBST;
aBST = NULL;
struct t_Data d1,d2,d3,d4,d5,d6,d7,d8,d9,d10;
d1.m_Info = 62;
d2.m_Info = 88;
d3.m_Info = 58;
d4.m_Info = 47;
d5.m_Info = 35;
d6.m_Info = 73;
d7.m_Info = 51;
d8.m_Info = 99;
d9.m_Info = 37;
d10.m_Info = 93;
InsertBST(&aBST,d1);
InsertBST(&aBST,d2);
InsertBST(&aBST,d3);
InsertBST(&aBST,d4);
InsertBST(&aBST,d5);
InsertBST(&aBST,d6);
InsertBST(&aBST,d7);
InsertBST(&aBST,d8);
InsertBST(&aBST,d9);
InsertBST(&aBST,d10);
}
#包括
#包括
结构t_数据
{
int m_信息;
};
结构t_节点
{
结构t_数据m_数据;
结构t_节点*m_LeftChild;
结构t_节点*m_RightChild;
};
typedef结构t_节点*t_二进制SortTree;
/*返回targetNode或路径中的最后一个节点*/
int SearchBST(t_二进制SortTree t,int AgiveInfo,结构t_节点*lastParentNode,结构t_节点**结果)
{
如果(!T)
{
*结果=lastParentNode;
返回0;
}
else if(AgiveInfo==(*T).m_Data.m_Info)
{
*结果=T;
返回1;
}
else if(AgiveInfo<(*T).m_数据.m_信息)
{
lastParentNode=T;
返回SearchBST((*T).m_LeftChild,agivenfo,lastParentNode,result);
}
其他的
{
lastParentNode=T;
返回SearchBST((*T).m_RightChild,agivenfo,lastParentNode,result);
}
}
void InsertBST(t_BinarySortTree*t,struct t_Data newData)
{
智力状态;
结构t_节点*结果;
status=SearchBST(*T,newData.m_Info,NULL和result);
/*条件:无法在二进制排序树中搜索“newData”*/
如果(!状态)
{
结构t_节点*新节点;
newNode=(struct t_Node*)malloc(sizeof(struct_Node));
(*newNode).m_Data=newData;
(*newNode).m_LeftChild=NULL;
(*newNode).m_RightChild=NULL;
/*条件:结果==NULL*/
如果(!结果)
{
*T=新节点;
}
else if(newData.m_Info<(*结果).m_Data.m_Info)
{
(*result).m_LeftChild=newNode;
}
/*条件:newData.m_Info>(*结果)。m_Data.m_Info*/
其他的
{
(*结果).m_RightChild=newNode;
}
}
}
内部主(空)
{
t_binarysortttree abs;
abs=NULL;
结构图数据d1、d2、d3、d4、d5、d6、d7、d8、d9、d10;
d1.m_Info=62;
d2.m_Info=88;
d3.m_Info=58;
d4.m_Info=47;
d5.m_Info=35;
d6.m_Info=73;
d7.m_Info=51;
d8.m_Info=99;
d9.m_Info=37;
d10.m_Info=93;
插入BST(&abs,d1);
插入BST(&Abs,d2);
插入BST(&Abs,d3);
插入BST(&Abs,d4);
插入BST(&Abs,d5);
插入BST(&abs,d6);
插入BST(&Abs,d7);
插入BST(&Abs,d8);
插入BST(&Abs,d9);
插入BST(&Abs,d10);
}
在行中
正确的格式应为
你的图表错了。14是13岁的孩子。
假设要插入的节点是二叉搜索树,如果未找到给定节点,则需要在搜索路径中保存最后一个父节点。并将新节点插入函数insert()
中获得的最后一个父节点。该程序不是递归的
下面是C语言中的一个例子
#include<stdio.h>
#include<stdlib.h>
struct t_Data
{
int m_Info;
};
struct t_Node
{
struct t_Data m_Data;
struct t_Node* m_LeftChild;
struct t_Node* m_RightChild;
};
typedef struct t_Node* t_BinarySortTree;
/* return targetNode or the last node in the path */
int SearchBST(t_BinarySortTree T, int aGivenInfo, struct t_Node* lastParentNode, struct t_Node* *result)
{
if(!T)
{
*result = lastParentNode;
return 0;
}
else if (aGivenInfo == (*T).m_Data.m_Info)
{
*result = T;
return 1;
}
else if(aGivenInfo < (*T).m_Data.m_Info)
{
lastParentNode = T;
return SearchBST((*T).m_LeftChild,aGivenInfo,lastParentNode,result);
}
else
{
lastParentNode = T;
return SearchBST((*T).m_RightChild,aGivenInfo,lastParentNode,result);
}
}
void InsertBST(t_BinarySortTree *T, struct t_Data newData)
{
int status;
struct t_Node* result;
status = SearchBST(*T,newData.m_Info,NULL,&result);
/* condition: fail to search for 'newData' in the binary sort tree */
if (!status)
{
struct t_Node* newNode;
newNode = (struct t_Node*)malloc(sizeof(struct t_Node));
(*newNode).m_Data = newData;
(*newNode).m_LeftChild = NULL;
(*newNode).m_RightChild = NULL;
/* condition: result == NULL */
if (!result)
{
*T = newNode;
}
else if (newData.m_Info < (*result).m_Data.m_Info)
{
(*result).m_LeftChild = newNode;
}
/* condition: newData.m_Info > (*result).m_Data.m_Info */
else
{
(*result).m_RightChild = newNode;
}
}
}
int main(void)
{
t_BinarySortTree aBST;
aBST = NULL;
struct t_Data d1,d2,d3,d4,d5,d6,d7,d8,d9,d10;
d1.m_Info = 62;
d2.m_Info = 88;
d3.m_Info = 58;
d4.m_Info = 47;
d5.m_Info = 35;
d6.m_Info = 73;
d7.m_Info = 51;
d8.m_Info = 99;
d9.m_Info = 37;
d10.m_Info = 93;
InsertBST(&aBST,d1);
InsertBST(&aBST,d2);
InsertBST(&aBST,d3);
InsertBST(&aBST,d4);
InsertBST(&aBST,d5);
InsertBST(&aBST,d6);
InsertBST(&aBST,d7);
InsertBST(&aBST,d8);
InsertBST(&aBST,d9);
InsertBST(&aBST,d10);
}
#包括
#包括
结构t_数据
{
int m_信息;
};
结构t_节点
{
结构t_数据m_数据;
结构t_节点*m_LeftChild;
结构t_节点*m_RightChild;
};
类型定义
#include<stdio.h>
#include<stdlib.h>
struct t_Data
{
int m_Info;
};
struct t_Node
{
struct t_Data m_Data;
struct t_Node* m_LeftChild;
struct t_Node* m_RightChild;
};
typedef struct t_Node* t_BinarySortTree;
/* return targetNode or the last node in the path */
int SearchBST(t_BinarySortTree T, int aGivenInfo, struct t_Node* lastParentNode, struct t_Node* *result)
{
if(!T)
{
*result = lastParentNode;
return 0;
}
else if (aGivenInfo == (*T).m_Data.m_Info)
{
*result = T;
return 1;
}
else if(aGivenInfo < (*T).m_Data.m_Info)
{
lastParentNode = T;
return SearchBST((*T).m_LeftChild,aGivenInfo,lastParentNode,result);
}
else
{
lastParentNode = T;
return SearchBST((*T).m_RightChild,aGivenInfo,lastParentNode,result);
}
}
void InsertBST(t_BinarySortTree *T, struct t_Data newData)
{
int status;
struct t_Node* result;
status = SearchBST(*T,newData.m_Info,NULL,&result);
/* condition: fail to search for 'newData' in the binary sort tree */
if (!status)
{
struct t_Node* newNode;
newNode = (struct t_Node*)malloc(sizeof(struct t_Node));
(*newNode).m_Data = newData;
(*newNode).m_LeftChild = NULL;
(*newNode).m_RightChild = NULL;
/* condition: result == NULL */
if (!result)
{
*T = newNode;
}
else if (newData.m_Info < (*result).m_Data.m_Info)
{
(*result).m_LeftChild = newNode;
}
/* condition: newData.m_Info > (*result).m_Data.m_Info */
else
{
(*result).m_RightChild = newNode;
}
}
}
int main(void)
{
t_BinarySortTree aBST;
aBST = NULL;
struct t_Data d1,d2,d3,d4,d5,d6,d7,d8,d9,d10;
d1.m_Info = 62;
d2.m_Info = 88;
d3.m_Info = 58;
d4.m_Info = 47;
d5.m_Info = 35;
d6.m_Info = 73;
d7.m_Info = 51;
d8.m_Info = 99;
d9.m_Info = 37;
d10.m_Info = 93;
InsertBST(&aBST,d1);
InsertBST(&aBST,d2);
InsertBST(&aBST,d3);
InsertBST(&aBST,d4);
InsertBST(&aBST,d5);
InsertBST(&aBST,d6);
InsertBST(&aBST,d7);
InsertBST(&aBST,d8);
InsertBST(&aBST,d9);
InsertBST(&aBST,d10);
}