//
# include "stdafx. H"
#include
#include
Typedef int ElemType;
Struct BTreeNode {
ElemType data;
BTreeNode * left;
BTreeNode * Right;
};
Void InitBTree (BTreeNode * & amp; BT)
{
BT=NULL;
}
Void Insert (BTreeNode * & amp; BST, const ElemType& Item)
{
If (BST==NULL)
{
BTreeNode * p=new BTreeNode;
p-> data=https://bbs.csdn.net/topics/item;
p-> Left=p - & gt; Right=NULL;
BST=p;
}
Else if (item
The else
Insert (BST - & gt; Right, the item);
}
Void CreateBSTree (BTreeNode * & amp; BST, ElemType a [], int n)
{
BST=NULL;
for(int i=0; i
}
Void PrintBTree BTreeNode * (BT)
{
If (BT!!!!=NULL)
{
Cout
Cout<'(';
PrintBTree (BT - & gt; Left);
If (BT - & gt; Right!=NULL)
Cout<', ';
PrintBTree (BT - & gt; Right);
Cout<');
}
}
Int DepthBTree BTreeNode * (BT)
{
If (BT==NULL)
return 0;
The else {
Int dep1=DepthBTree (BT - & gt; Left);
Int dep2=DepthBTree (BT - & gt; Right);
If (dep1 & gt; Dep2)
Return the dep1 + 1;
The else
Return dep2 + 1;
}
}
Void InOder BTreeNode * (BT)
{
InOder (BT - & gt; Left);
Cout
}
Bool Find1 (BTreeNode * BST, ElemType& Item)
{
While (BST!=NULL) {
If (item==BST - & gt; Data) {
The item=BST - & gt; data;
return true;
}
Else if (item
The else
BST=BST - & gt; Right;
}
return false;
}
Void Insert1 (BTreeNode * & amp; BST, const ElemType& Item)
{
BTreeNode * t=BST, * parent=NULL;
while(t!=NULL) {
The parent=t;
If (item
The else
T=t - & gt; Right;
}
BTreeNode * p=new BTreeNode;
p-> data=https://bbs.csdn.net/topics/item;
p-> Left=p - & gt; Right=NULL;
If (the parent==NULL)
BST=NULL;
Else if (item
The else parent - & gt; Right=p;
}
Bool Delete (BTreeNode * & amp; BST, const ElemType& Item)
{
If (BST==NULL)
return false;
If (item
If (item> BST - & gt; Data)
Return the Delete (BST - & gt; Right, the item);
Temp=BST BTreeNode *;
If (BST - & gt; Left==NULL)
{
BST=BST - & gt; Right;
The delete temp.
return true;
}
Else if (BST - & gt; Right==NULL)
{
BST=BST - & gt; The left;
The delete temp.
return true;
}
The else {
If (BST - & gt; Left - & gt; Right==NULL) {
BST - & gt; data=https://bbs.csdn.net/topics/BST-> left -> data;
Return the Delete (BST - & gt; Left, BST - & gt; Left - & gt; The data);
}
The else {
P1=BST BTreeNode * and * p2=BST - & gt; The left;
While (p2 - & gt; Right!=NULL) {
P1=p2;
The p2=p2 - & gt; Right;
}
BST - & gt; data=https://bbs.csdn.net/topics/p2-> data;
Return the Delete (p1 - & gt; Right, p2 - & gt; The data);
}
}
}
Void ClearBTree (BTreeNode * & amp; BT)
{
If (BT!!!!=NULL)
{
ClearBTree (BT - & gt; Left);
ClearBTree (BT - & gt; Right);
Delete the BT;
BT=NULL;
}
}
Void main ()
{
ElemType x;
BST BTreeNode *;
InitBTree (BST);
30,50,20,40,25,70,54,23,80,92 ElemType a [10]={};
CreateBSTree (BST, a 10);
PrintBTree (BST);
Cout
Cout
InOder (BST);
Cout
Cin> x;
If (Find1 (BST, x))
Cout<" Look for the element "& lt;
Cout<" Look for the element "& lt;
Cin> x;
Insert1 (BST, x);
Cout<" Enter a node is to be deleted: ";
If (Delete (BST, x))
Cout<" Remove elements "& lt;
Cout<" Remove elements "& lt;
PrintBTree (BST);
Cout
InOder (BST);
Cout
}
CodePudding user response:
Debugging around... Like you who knows up to thatCodePudding user response:
void InOder BTreeNode * (BT)
{
If (BT - & gt; Left)
InOder (BT - & gt; Left);
CoutIf (BT - & gt; Right)
InOder (BT - & gt; Right);
}
This function change. Recursive functions need to set up the termination conditions, the output and a separator
CodePudding user response:
Or itvoid InOder BTreeNode * (BT)
{
if(! BT) return;
InOder (BT - & gt; Left);
CoutInOder (BT - & gt; Right);
nullnullnull