Binary Tree search and Deletion

Source: Internet
Author: User

(Delete a binary tree) in this exercise, we will discuss how to delete a node from the binary search tree. The deletion algorithm is not as simple as the Insertion Algorithm. There may be three different cases for deleting a value-the value is included in a leaf node (no child node ), this value is included in only one subnode or two subnodes.
If this item is in the leaf node, delete it directly and set the pointer from its parent node to null.
If this item is included in a node with only one child node, the parent node pointer is directed to its child node and then the data item is deleted, and its child node replaces the node location. The last case is the most difficult. When the node to be deleted contains two subnodes, it is replaced by another leaf node.

Compile a member function deletenode of a binary tree, with the root node pointer of the tree and the value to be deleted as the parameter. This function needs to find the node that contains this value and delete the node using the algorithm discussed above. The function also prints a message indicating that the node is deleted. After the node is deleted, use the forward, forward, and backward traversal to check whether the node has been deleted correctly.

//////////////////////////////////////// /// // Binstree. h /////////////////////////////////////// ///////////

# Pragma once
# Include <iostream>
# Include <stdio. h>
Using namespace STD;
Class binstree;
Class treenode
{
Public:
Friend class binstree;
Treenode (char item, treenode * lptr = NULL, treenode * rptr = NULL): contents (item), left (lptr), right (rptr ){}
PRIVATE:
Char contents; // node Value
Treenode * left; // left child
Treenode * right; // right child
};
Typedef treenode * lptreenode;
Class binstree
{
Public:
Binstree (void );
~ Binstree (void );
Void creatbinstree (lptreenode & tree); // create a new tree
Void middleread (treenode * t); // supports sequential traversal.

Void firstread (treenode * t); // pre-order traversal
Void lastread (treenode * t); // post-order traversal
Void find (treenode * tree, char I); // you can check whether a node with a value of I exists.
Lptreenode search (lptreenode & tree );//
Void deletenode (lptreenode & tree, char I); // delete a node with a value of I
Treenode * node; // Root Node
PRIVATE:
Bool TJ;
};

//////////////////////////////////////// Binstree. CPP /////////////////////////////////////// //////

# Include "binstree. H"
Binstree: binstree (void)
{
TJ = false;
}

Binstree ::~ Binstree (void)
{

}
Void binstree: creatbinstree (lptreenode & tree)
{
Char C;
Scanf ("% C", & C); // obtain a node Value
If (C = '')
{
Tree = NULL;
}
Else
{
Tree = new treenode (c); // generate a new node
Creatbinstree (tree-> left); // generate the left subtree
Creatbinstree (tree-> right); // generate the right subtree
}
}
Void binstree: middleread (treenode * t)
{
If (node = NULL)
{
Cout <"empty tree, cannot traverse! "<Endl;
Return;
}
If (T! = NULL)
{
Middleread (t-> left );
Cout <t-> contents <"";
Middleread (t-> right );
}
}
Void binstree: firstread (treenode * t)
{
If (node = NULL)
{
Cout <"empty tree, cannot traverse! "<Endl;
Return;
}
If (T! = NULL)
{
Cout <t-> contents <"";
Middleread (t-> left );
Middleread (t-> right );
}
}
Void binstree: lastread (treenode * t)
{
If (node = NULL)
{
Cout <"empty tree, cannot traverse! "<Endl;
Return;
}
If (T! = NULL)
{
Middleread (t-> left );
Middleread (t-> right );
Cout <t-> contents <"";
}
}
Void binstree: Find (treenode * tree, char I)
{
If (tree! = NULL)
{
If (tree-> contents = I)
{
TJ = true;
Return;
}
If (tree-> left! = NULL)
{
If (tree-> left-> contents = I)
{
TJ = true;
Return;
}
}
If (tree-> right! = NULL)
{
If (tree-> right-> contents = I)
{
TJ = true;
Return;
}
}
Find (tree-> left, I );
Find (tree-> right, I );
}
}
Lptreenode binstree: Search (lptreenode & tree)
{
If (tree! = NULL)
{
If (tree-> left = NULL & tree-> left-> right = NULL)
{
Return tree-> left;
}
Else if (tree-> right-> left = NULL & tree-> right = NULL)
{
Return tree-> right;
}
Return search (tree-> left );
Return search (tree-> right );
}
}

 
 
 
Delete a binary tree node (2)

Void binstree: deletenode (lptreenode & tree, char I)
{
Find (tree, I );
If (! TJ)
{
Cout <"this node is unavailable! "<Endl;
Return;
}
If (tree! = NULL)
{
If (node-> contents = I)
{
If (node-> left = NULL & node-> right = NULL)
{
Delete node;
Node = NULL;
Cout <"deleted node, current tree is empty" <Endl;
Return;
}
Else if (node-> left! = NULL & node-> right = NULL)
{
Treenode * P = node;
Treenode * D = new treenode (search (node)-> contents );
D-> left = tree-> left;
D-> right = NULL;
Node = D;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (node-> right! = NULL & node-> left = NULL)
{
Treenode * P = node;
Treenode * D = new treenode (search (node)-> contents );
D-> left = NULL;
D-> right = tree-> right;
Node = D;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else
{
Treenode * P = node;
Treenode * D = new treenode (search (node)-> contents );
D-> left = tree-> left;
D-> right = tree-> right;
Node = D;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
}
If (tree-> left! = NULL)
{
If (tree-> left-> contents = I)
{
If (tree-> left = NULL & tree-> left-> right! = NULL)
{
Treenode * P = tree-> left;
Tree-> left = tree-> left-> right;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (tree-> left = NULL & tree-> left-> right = NULL)
{
Delete tree-> left;
Tree-> left = NULL;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (tree-> left! = NULL & tree-> left-> right = NULL)
{
Treenode * P = tree-> left;
Tree-> left = tree-> left;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (tree-> left! = NULL & tree-> left-> right! = NULL)
{
Treenode * P = tree-> left;
Treenode * D = new treenode (search (node)-> contents );
D-> left = tree-> left;
D-> right = tree-> left-> right;
Tree-> left = D;
Delete P;
}
Return;
}
}
If (tree-> right! = NULL)
{
If (tree-> right-> contents = I)
{
If (tree-> right-> left = NULL & tree-> right = NULL)
{
Delete tree-> right;
Tree-> right = NULL;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (tree-> right-> left! = NULL & tree-> right = NULL)
{
Treenode * P = tree-> right;
Tree-> right = tree-> right-> left;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (tree-> right-> left = NULL & tree-> right! = NULL)
{
Treenode * P = tree-> right;
Tree-> right = tree-> right;
Delete P;
Cout <"a node with a value of" <I <"has been deleted" <Endl;
}
Else if (tree-> right-> left! = NULL & tree-> right! = NULL)
{
Treenode * P = tree-> right;
Treenode * D = new treenode (search (node)-> contents );
D-> left = tree-> right-> left;
D-> right = tree-> right;
Tree-> right = D;
Delete P;
}
Return;
}
}
Deletenode (tree-> left, I );
Deletenode (tree-> right, I );
}
}

Related Article

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.