Insertion and deletion of binary search tree (detailed analysis) _c language

Source: Internet
Author: User
Tags assert

Topic: Create a class, the data members in the class of a binary search tree, the external interface provided by the addition of nodes and delete the nodes of the two methods. The user is not concerned about the binary tree. We are asked to give the structure of this class and the methods in the implementation class.

Ideas
To add a node:
Add the node is actually very easy, we only need to find the corresponding location of the node can be, and there is no requirement is a balanced two-fork search tree, so each add nodes are in the leaf node operation, do not need to modify the structure of the two-fork search tree overall. To find out where the added node is in the binary search tree, you can use a loop to resolve it. Determine the size of the insertion node and the current header node, and continue searching for the right subtree if greater than the header node, or continue searching for the left subtree if less than the header node. Until the leaf node is searched, the Insert node operation is performed at this point. If the inserted node equals the value of a current node in the two-fork search tree, exit the insert operation and tell the user that the node already exists.

To delete a node:
Deleting a node is tricky because you need to adjust the tree's structure because deleting a node does not necessarily occur at the leaf node. If the deletion is the leaf node, then the operation is very simple, just do the corresponding delete on it, but if the deletion is not a leaf node, then you need to adjust the structure of the binary search tree. There are two policies to adjust. Suppose the current node to be deleted is a,

1. Find the maximum node B in the left subtree of a node and adjust B to the position of the original A.
2. Find the Minimum node C in the right subtree of a node and adjust C to the position of the original A.

This involves a lot of complex pointer operations, in the following code example does not complete the node deletion operation, and so on time to supplement the study.

code example

Copy Code code as follows:

#include <iostream>
#include <stdlib.h>
#include <cassert>
using namespace Std;

Two-fork tree node
struct Binarytreenode
{
int m_nvalue;
binarytreenode* M_pleft;
binarytreenode* M_pright;
};

Class BST
{
Public
BST (int value);//constructor
~bst ();//destructor
void AddNode (int value);//Add node
void Deletenode (int value);//Delete node
binarytreenode* createbinarytreenode (int value);//Create a binary tree node
void Inorderprinttree ();//in-sequence traversal
void Inorderprinttree (binarytreenode* proot);//in-sequence traversal
binarytreenode* Getmaxnode (binarytreenode* pnode);/Find binary search tree maximum value
binarytreenode* Getminnode (binarytreenode* pnode)//Find binary search tree minimum value

Private
binarytreenode* Proot;
};

Constructors
BST::BST (int value)
{
Proot=createbinarytreenode (value);
}

destructor
Bst::~bst ()
{
Delete Proot;
Proot=null;
}

Create a two-fork tree node
binarytreenode* bst::createbinarytreenode (int value)
{
binarytreenode* pnode=new Binarytreenode ();
pnode->m_nvalue=value;
pnode->m_pleft=null;
pnode->m_pright=null;
return pnode;
}

Find the maximum value of a binary search tree
binarytreenode* Bst::getmaxnode (binarytreenode* pnode)
{
ASSERT (Pnode!=null); Use assertions to ensure that incoming header nodes are not empty
The maximum value is on the right subtree, so the right subtree is traversed so that the pnode equals the right subtree, and if only one node is returned directly to Pnode
while (Pnode->m_pright!=null)
{
pnode=pnode->m_pright;
}
return pnode;

}
Find the minimum value of a binary search tree
binarytreenode* Bst::getminnode (binarytreenode* pnode)
{
ASSERT (Pnode!=null); Using assertions
The minimum value is on the left subtree, and the whole idea is the same as the maximum value.
while (Pnode->m_pleft!=null)
{
pnode=pnode->m_pleft;
}
return pnode;
}
Two-fork Search tree Add node
void Bst::addnode (int value)
{
binarytreenode* Pinsertnode=createbinarytreenode (value);//Initialize the node that you want to create.
binarytreenode* Pnode=proot;
while (true)
{
If the inserted value already exists in the binary search tree, the insert operation is not performed and the loop is out.
if (Pnode->m_nvalue==value)
{
cout<< "Node value already exists" <<endl;
Break
}

Find the location where the node is inserted, and continue searching if the node you want to insert is less than the current header node Zuozi
else if (Pnode->m_nvalue > Value)
{
if (pnode->m_pleft==null)//If the current header node is a leaf node, insert the node to be inserted directly into the left subtree and then jump out of the loop
{
pnode->m_pleft=pinsertnode;
Break
}
else//otherwise continue traversing its left subtree
pnode=pnode->m_pleft;
}
With the same idea as above
else if (Pnode->m_nvalue < value)
{
if (pnode->m_pright==null)
{
pnode->m_pright=pinsertnode;
Break
}
pnode=pnode->m_pright;
}
}

}

//Incomplete
void BST::D eletenode (int value)
{
    binarytreenode* pnode=proot;
    while (true)
    {
        if (proot- >m_nvalue==value)//If it is a header node
        {
             if (proot->m_pleft!=null)
             {
                 binarytreenode* Pleftmaxnode=getmaxnode (proot->m_pleft);
           }
            Else if (proot->m_pright!=null)
            {

}
Else
{
Delete Proot;
Proot=null;
}
}

if (Pnode->m_nvalue==value)
{
if (pnode->m_pleft!=null)
{

}
else if (pnode->m_pright!=null)
{

}
Else
{

}
}
}
}
void Bst::inorderprinttree (binarytreenode* proot)//in-sequence traversal
{
if (proot!=null)
{
If the Zoozi tree is not empty, traverse the Zuozi
if (proot->m_pleft!=null)
Inorderprinttree (Proot->m_pleft);
Traversing the leaf nodes of the left subtree
cout<< "Value of this node is" <<pRoot->m_nValue<<endl;
If the right subtree is not empty, traverse the right subtree
if (proot->m_pright!=null)
Inorderprinttree (Proot->m_pright);
}
Else
{
cout<< "This node is null." <<endl;
}
}
Because of the need to use recursion for the sequence traversal, you need to call a sequence traversal function with parameters
void Bst::inorderprinttree ()//in-sequence traversal
{
Inorderprinttree (Proot);
}
void Main ()
{
bst* b=new BST (10);//Initialize the class with the definition of the head node of the two-fork search tree, which eliminates the judgment that the head node is empty
B->addnode (6);
B->addnode (14);
B->inorderprinttree ();
System ("pause");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.