Home > Back-end >  Himself wrote a binary search tree why delete nodes that step out of the BUG
Himself wrote a binary search tree why delete nodes that step out of the BUG

Time:12-20

Just delete the root node and the root node has about two children program would crash other conditions are normal BUG
The following are the normal


Delete the left and right subtrees not is empty root node of the error


program source code#include
using namespace std;
The class TreeNode {
Public:
Friend class Btree;
TreeNode (int the info) {value=https://bbs.csdn.net/topics/info; LTree=NULL; RTree=NULL; }
The int value.
Private:
TreeNode * LTree;
TreeNode * RTree;
};
The class Btree {
Public:
Btree ();
~ Btree ();
Void insertNode (int value);
Void firstprintf ();
Void middleprintf ();
Void lastprintf ();
Void showsize ();
Void deleteValueOfTree (int k);
TreeNode * FindMinValue ();
TreeNode * FindMaxValue ();
TreeNode * FindValueOfTree (int k);
Private:
Root TreeNode *;
Int Nodenumber;
Void Firstprintf TreeNode * (p);
Void Btreeclear TreeNode * (p);
Void insertNode (TreeNode * p, int value);
Void Middleprintf TreeNode * (p);
Void Lastprintf TreeNode * (p);
TreeNode * FindParentofNode TreeNode * (p);
};
Btree: : Btree ()//constructor
{
The root=NULL;
Nodenumber=0;
}
Btree: : ~ Btree ()//destructors
{
If (root==NULL)
return;
The else
Btreeclear (root);
}
Void Btree: : Btreeclear (TreeNode * p)//delete all node
{
If (p!=NULL) {
Btreeclear (p - & gt; LTree);
Btreeclear (p - & gt; RTree);
The delete p;

}
}
Void Btree: : insertNode (int value)//call insert node
{
If (root==NULL)
{
The root=new TreeNode (value);
Nodenumber++;
}
The else
{
InsertNode (root, value);
Nodenumber++;
}
}
Void Btree: : insertNode (TreeNode * p, int value)//insert node
{
If (p - & gt; Value> Value)
{
If (p - & gt; LTree==NULL)
P - & gt; LTree=new TreeNode (value);
The else
InsertNode (p - & gt; LTree, value);
}
Else if (p - & gt; Value{
If (p - & gt; RTree==NULL)
P - & gt; RTree=new TreeNode (value);
The else
InsertNode (p - & gt; RTree, value);
}
}
Void Btree: : firstprintf ()//call the first sequence traversal
{if (Nodenumber==0)
Cout<& lt;" The tree is empty "& lt; The else
{
Firstprintf (root);
Cout}
}
Void Btree: : Firstprintf (TreeNode * p)//first sequence traversal
{
If (p!=NULL)
{
Cout

Firstprintf (p - & gt; LTree);
Firstprintf (p - & gt; RTree);
}

}
TreeNode * Btree: : FindParentofNode (TreeNode * p)//find the node's parent p
{if (p==root)
{cout<& lt;" Is the root node to find out, not parents "& lt; return NULL;
}
The parent=root TreeNode *;
While (the parent - & gt; LTree!=p& & The parent - & gt; RTree!=p)
{
If (the parent - & gt; Value> P - & gt; Value)
The parent=parent - & gt; LTree;
The else
The parent=parent - & gt; RTree;
}
Return the parent;
}
TreeNode * Btree: : FindMinValue ()//to find the minimum
{
P=root TreeNode *;
While (p - & gt; LTree!=NULL)
{
P=p - & gt; LTree;
}
return p;
}
TreeNode * Btree: : FindMaxValue ()//find the maximum
{
P=root TreeNode *;
While (p - & gt; RTree!=NULL)
{
P=p - & gt; RTree;
}
return p;
}
Void Btree: : showsize ()//show the node number
{
Cout}
TreeNode * Btree: : FindValueOfTree (int k)//find the k value return pointer
{
P=root TreeNode *;
While (p!=NULL& & P - & gt; The value!=k)
{
If (p - & gt; ValueP=p - & gt; RTree;
The else
P=p - & gt; LTree;
}
If (p==NULL)
{
Cout<& lt;" Didn't find the value "& lt; return p;
}
The else
{
Cout<& lt;" Find the value "& lt; return p;
}
}
Void: : Btree: : middleprintf ()//in the call sequence traversal
{
If (Nodenumber==0)
{
Cout<& lt;" The tree is empty "& lt; return;
}
Middleprintf (root);
Cout}
Void: : Btree: : Middleprintf TreeNode * (p) in the sequence traversal
{
If (p!=NULL)
{
Middleprintf (p - & gt; LTree);
Cout

Middleprintf (p - & gt; RTree);
}
}
After the void Btree: : lastprintf ()//sequence traversal
{
If (Nodenumber==0)
{
Cout<& lt;" The tree is empty "& lt; return;
}
Lastprintf (root);
Cout}
Void Btree: : Lastprintf (TreeNode * p)//call sequence traversal
{
If (p!=NULL)
{
Lastprintf (p - & gt; LTree);
Lastprintf (p - & gt; RTree);
Cout

}
}
Void Btree: : deleteValueOfTree (int k)//delete the value of k node
* p={TreeNode FindValueOfTree (k);
If (p==NULL)
{
return;
}
TreeNode * parent=FindParentofNode (p);
If (p - & gt; LTree==NULL& & P - & gt; RTree==NULL)//if the deleted is the operation of the leaf node
{
If (p==root)//if there is only one of the root node
{
The delete p;
The root=NULL;
Nodenumber -;
}
Else if (the parent - & gt; LTree==p)
{
The parent - & gt; LTree=NULL;
The delete p;
Nodenumber -;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull

  • Related