Home > Back-end >  Compilation can pass, but a run collapses
Compilation can pass, but a run collapses

Time:09-19

//binary search tree. CPP: Defines the entry point for the console application.
//

# 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 (itemInsert (BST - & gt; Left, item);
The else
Insert (BST - & gt; Right, the item);
}

Void CreateBSTree (BTreeNode * & amp; BST, ElemType a [], int n)
{
BST=NULL;
for(int i=0; iInsert (BST, a [I]);
}



Void PrintBTree BTreeNode * (BT)
{
If (BT!!!!=NULL)
{
CoutIf (BT - & gt; Left!=NULL | | BT - & gt; Right!=NULL)
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);
CoutInOder (BT - & gt; Right);
}

Bool Find1 (BTreeNode * BST, ElemType& Item)
{
While (BST!=NULL) {
If (item==BST - & gt; Data) {
The item=BST - & gt; data;
return true;
}
Else if (itemBST=BST - & gt; The left;
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 (itemT=t - & gt; The left;
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 (itemThe parent - & gt; Left=p;
The else parent - & gt; Right=p;
}

Bool Delete (BTreeNode * & amp; BST, const ElemType& Item)
{
If (BST==NULL)
return false;
If (itemReturn the Delete (BST - & gt; Left, 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);
CoutCout<" Depth: ";
CoutCout<" Sequence traversal of the binary search tree: ";
InOder (BST);
CoutCout<" Input with find integer: ";
Cin> x;
If (Find1 (BST, x))
Cout<" Look for the element "& lt; The else
Cout<" Look for the element "& lt; Cout<" Input to insert a node: ";
Cin> x;
Insert1 (BST, x);
Cout<" Enter a node is to be deleted: ";
If (Delete (BST, x))
Cout<" Remove elements "& lt; The else
Cout<" Remove elements "& lt;
PrintBTree (BST);
CoutCout<" Sequence traversal of the binary search tree: ";
InOder (BST);
CoutClearBTree (BST);
}

CodePudding user response:

Debugging around... Like you who knows up to that

CodePudding 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 it
 void InOder BTreeNode * (BT) 
{
if(! BT) return;
InOder (BT - & gt; Left);
CoutInOder (BT - & gt; Right);
nullnullnull
  • Related