The latest implementation of C ++ operations on the binary sorting tree (Binary Search Tree)

Source: Internet
Author: User

 

// Perform operations on the chain Binary Search Tree. cpp: Defines the entry point for the console application.

//

# Include "stdafx. h"

# Include <iostream>

Using namespace std;

Struct BSTree

{

Int data;

BSTree * left;

BSTree * right;

};

// When the tag is inserted, if it already exists, it is true, indicating that no insert is required; otherwise, it is false.

Bool flag = false;

Int a [100];

// Search operation

BSTree * search (BSTree * r, int x)

{

If (r = NULL)

Return NULL;

Else

{

If (r-> data = x)

Return r;

Else if (r-> data> x)

Return search (r-> left, x );

Else

Return search (r-> right, x );

}

}

// Insert operation

BSTree * insert (BSTree * r, BSTree * s)

{

// First, check whether the node already exists in the tree.

BSTree * p = search (r, s-> data );

If (p = NULL)

{

If (r = NULL)

{

R = s;

}

Else if (s-> data <r-> data)

R-> left = insert (r-> left, s );

Else if (s-> data> r-> data)

R-> right = insert (r-> right, s );

}

Else

Flag = true;

Return r;

}

// Build

BSTree * createBSTree (BSTree * r, int * a, int n)

{

Int I;

BSTree * t;

T = r;

For (I = 0; I <n; I ++)

{

BSTree * s = (BSTree *) malloc (sizeof (BSTree ));

S-> data = a [I];

S-> left = NULL;

S-> right = NULL;

T = insert (t, s );

}

Return t;

}

// Obtain the parent node

BSTree * getFather (BSTree * r, BSTree * s)

{

BSTree * sf;

If (r = NULL | r = s)

Sf = NULL;

Else

{

If (s = r-> left | s = r-> right)

Sf = r;

Else if (s-> data> r-> data)

Sf = getFather (r-> right, s );

Else

Sf = getFather (r-> left, s );

}

Return sf;

}

// Delete operation

BSTree * deleteNode (BSTree * r, BSTree * s)

{

// BSTNODE * temp, * tfather, * pf;

BSTree * temp, * father, * sf;

// Pf = getfather (p, r );

Sf = getFather (r, s );

// The deleted node is a leaf node, not a root node.

If (s-> left = NULL & s-> right = NULL & sf! = NULL)

//

If (sf-> left = s)

Sf-> left = NULL;

//

Else

Sf-> right = NULL;

// The deleted node is a leaf node and a root node.

Else if (s-> left = NULL & s-> right = NULL & sf! = NULL)

R = NULL;

//

Else if (s-> left = NULL & s-> right! = NULL & sf! = NULL)

If (sf-> left = s)

Sf-> left = s-> right;

Else

Sf-> right = s-> right;

// The deleted node has a right child and no left child. The deleted node is the root node.

Else if (s-> left = NULL & s-> right! = NULL & sf = NULL)

R = s-> right;

// The deleted node has a left child and no right child. The deleted node is not the root node.

Else if (s-> left! = NULL & s-> right = NULL & sf! = NULL)

If (sf-> left = s)

Sf-> left = s-> left;

Else

Sf-> right = s-> left;

// The deleted node has a left child and no right child. The deleted node is the root node.

Else if (s-> left! = NULL & s-> right = NULL & sf = NULL)

R = s-> left;

Else if (s-> left! = NULL & s-> right! = NULL)

{

Temp = s-> left;

Father = s;

// Find the largest node in the left subtree

While (temp-> right! = NULL)

{

Father = temp;

Temp = temp-> right;

}

S-> data = temp-> data;

If (father! = S)

Father-> right = temp-> left;

Else

Father-> left = temp-> left;

}

If (r = NULL)

After cout <"is deleted, the binary sorting tree is empty! "<Endl;

Else

Cout <"deleted successfully! "<Endl;

Return r;

}

// Forward Output

Void preOrder (BSTree * r)

{

If (r = NULL)

Return;

Else

{

Cout <r-> data <"";

PreOrder (r-> left );

PreOrder (r-> right );

}

}

// Output in the middle order

Void inOrder (BSTree * r)

{

If (r = NULL)

Return;

Else

{

InOrder (r-> left );

Cout <r-> data <"";

InOrder (r-> right );

}

}

// Subsequent output

Void postOrder (BSTree * r)

{

If (r = NULL)

Return;

Else

{

PostOrder (r-> left );

PostOrder (r-> right );

Cout <r-> data <"";

}

}

Int _ tmain (int argc, _ TCHAR * argv [])

{

Int cases;

Cout <"Enter the number of cases:" <endl;

Cin> cases;

While (cases --)

{

Int n;

Flag = false;

BSTree * root = NULL;

Cout <"Enter the number of elements:" <endl;

Cin> n;

Int I;

Cout <"Enter these elements:" <endl;

For (I = 0; I <n; I ++)

Cin> a [I];

Cout <"creates a binary sorting tree! "<Endl;

Root = createBSTree (root, a, n );

If (root! = NULL)

Cout <"binary sorting tree created successfully! "<Endl;

Else

{

Cout <"An error occurred while establishing the binary sorting tree! "<Endl;

Return 0;

}

Cout <"the root value of this binary tree is:" <endl;

Cout <root-> data <endl;

Cout <"select the operation you want to perform:" <endl;

Cout <"1. insert (I/I)" <endl;

Cout <"2. Search (S/s)" <endl;

Cout <"3. Delete (D/d)" <endl;

Cout <"4. FIFO output (P/p)" <endl;

Cout <"5. Middle-order output (M/m)" <endl;

Cout <"6. Post-order output (L/l)" <endl;

Cout <"7. Exit (E/e)" <endl;

Char s;

Cin> s;

While (1)

{

If (s = 'E' | s = 'E ')

Break;

Else if (s = 'I' | s = 'I ')

{

Cout <"Enter the value you want to insert:" <endl;

Int x;

Cin> x;

BSTree * p = (BSTree *) malloc (sizeof (BSTree ));

P-> data = x;

P-> left = NULL;

P-> right = NULL;

Root = insert (root, p );

If (flag = false)

Cout <"inserted successfully! "<Endl;

Else

{

Cout <"this value already exists in this binary tree! "<Endl;

Flag = false; // restore the original value

}

}

Else if (s = 's' | S ='s ')

{

Cout <"Enter the value you want to search for:" <endl;

Int x;

Cin> x;

BSTree * p = search (root, x );

BSTree * pfather = getFather (root, p );

Cout <"Search value:" <p-> data <endl;

If (pfather! = NULL)

Cout <"parent node value:" <pfather-> data <endl;

Else

Cout <"It is the root node and has no parent node! "<Endl;

If (p-> left = NULL & p-> right = NULL)

Cout <"it is a leaf node with no sub-nodes" <endl;

Else

{

If (p-> left! = NULL)

Cout <"the value of its left son node is:" <p-> left-> data <endl;

Else

Cout <"the left son node is empty! "<Endl;

If (p-> right! = NULL)

Cout <"the value of its right son is:" <p-> right-> data <endl;

Else

Cout <"the right son node is empty! "<Endl;

}

}

Else if (s = 'D' | s = 'D ')

{

Cout <"Enter the value of the node you want to delete:" <endl;

Int value;

Cin> value;

Cout <"are you sure you want to delete it? (Yy/Nn) "<endl;

Char order;

Cin> order;

While (1)

{

If (order = 'y' | order = 'y ')

{

BSTree * node;

Node = search (root, value );

If (node = NULL)

Cout <"this node does not exist! "<Endl;

Else

BSTree * nodeDel = deleteNode (root, node );

Break;

}

Else if (order = 'n' | order = 'n ')

{

Break;

}

Else

{

The cout <"command is incorrect. Please enter it again! "<Endl;

Cin> order;

}

}

}

Else if (s = 'P' | s = 'P ')

{

Cout <"its forward output is:" <endl;

PreOrder (root );

Cout <endl;

}

Else if (s = 'M' | s = 'M ')

{

Cout <"where the sequential output is:" <endl;

InOrder (root );

Cout <endl;

}

Else if (s = 'l' | s = 'l ')

{

Cout <"the subsequent output is:" <endl;

PostOrder (root );

Cout <endl;

}

Else

{

Cout <"command error. Please enter it again! "<Endl;

}

Cout <"select the operation you want to perform:" <endl;

Cin> s;

}

}

System ("pause ");

Return 0;

}

 

 

From: My dream pursued by me

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.