Implementation of Binary Search Tree

Source: Internet
Author: User

1. Implementation of the Binary Search Tree:


[Cpp] include <stdio. h>
# Include <stdlib. h>
 
Typedef struct Node {// defines the Binary Tree Type
Int data;
Struct Node * parent;
Struct Node * lchild;
Struct Node * rchild;
} BiTreeNode, * BiTree;
 
// Binary tree search. If the keyword x is found, the pointer to the node is returned. Otherwise, null is returned.
BiTree BSTSearch (BiTree T, int x ){
BiTreeNode * p;
If (T! = NULL ){
P = T;
While (p! = NULL ){
If (x = p-> data)
Return p;
Else if (x <p-> data)
P = p-> lchild;
Else
P = p-> rchild;
}
}
Return NULL;
}
// Use recursion to implement the search algorithm
BiTree BSTSearch2 (BiTree T, int x ){
If (T = NULL)
Return NULL;
If (x <T-> data)
Return BSTSearch2 (T-> lchild, x );
Else if (x> T-> data)
Return BSTSearch2 (T-> rchild, x );
Else
Return T;
}
 
// Insert the Binary Search Tree
Int BSTInsert (BiTree * T, int x ){
BiTreeNode * p, * cur, * parent = NULL;
Cur = * T;
While (cur! = NULL ){
If (cur-> data = x)
Return 0; // If a node with the keyword x already exists, insertion fails.
Parent = cur;
If (x <cur-> data)
Cur = cur-> lchild;
Else
Cur = cur-> rchild;
}
P = (BiTreeNode *) malloc (sizeof (BiTreeNode ));
If (! P)
Return-1;
P-> data = x;
P-> parent = parent;
P-> lchild = NULL;
P-> rchild = NULL;
If (! Parent)
* T = p;
Else if (x <parent-> data)
Parent-> lchild = p;
Else
Parent-> rchild = p;
Return 1;
}
 
// Delete node s from the Binary Search Tree and keep the properties of the Binary Search Tree unchanged
Void DeleteNode (BiTree * s ){
BiTree q, x, y;
If (! (* S)-> rchild) {// if the right subtree of s is empty, the left subtree of s becomes the left subtree of the parent node of the deleted Vertex
Q = * s;
* S = (* s)-> lchild;
Free (q );
}
Else if (! (* S)-> lchild) {// If the left subtree of s is empty, the right subtree of s becomes the right subtree of the parent node of the deleted Vertex
Q = * s;
* S = (* s)-> rchild;
Free (q );
}
Else {// if both left and right subtree of s exists, replace s with s's direct precursor node, and make the left subtree of its parent node directly become the right subtree node of its parent node
X = * s;
Y = (* s)-> lchild;
While (y-> rchild) {// find the direct precursor node of s, y is the direct precursor node of s, and x is the parent node of y
X = y;
Y = y-> rchild;
}
(* S)-> data = y-> data; // node s is replaced by y
If (x! = * S) // If the left child node of node s does not exist
X-> rchild = y-> lchild; // convert the left subtree of y to the right subtree of x.
Else
X-> lchild = y-> lchild;
Free (y );
}
 
}
 
// Locate the node with the keyword x in the binary search tree T and delete it. If the deletion is successful, 1 is returned; otherwise, 0 is returned.
Int BSTDelete (BiTree * T, int x ){
If (! * T)
Return 0;
Else {
If (x = (* T)-> data)
DeleteNode (T );
Else if (x <(* T)-> data)
Return BSTDelete (& (* T)-> lchild, x );
Else
Return BSTDelete (& (* T)-> rchild, x );
Return 1;
}
}
 
Void InOrderTraverse (BiTree T ){
If (T ){
InOrderTraverse (T-> lchild );
Printf ("% d \ n", T-> data );
InOrderTraverse (T-> rchild );
}
}
 
BiTree BSTMinNode (BiTree T ){
BiTree p = T;
While (p-> lchild)
P = p-> lchild;
Return p;
}
 
BiTree BSTMaxNode (BiTree T ){
BiTree p = T;
While (p-> rchild)
P = p-> rchild;
Return p;
}
 
BiTree BSTSearchSuccessor (BiTree T ){
If (T-> rchild)
Return BSTMinNode (T-> rchild );
BiTree p = T-> parent;
While (p! = NULL & T = p-> rchild ){
T = p;
P = p-> parent;
}
Return p;
}
 
BiTree BSTSearchPredecessor (BiTree T ){
If (T-> lchild)
Return BSTMaxNode (T-> lchild );
BiTree p = T-> parent;
While (p! = NULL & T = p-> lchild ){
T = p;
P = p-> parent;
}
Return p;
}
 
# Define LEN 10
Int generate_array (int * array ){
Int I = 0;
Srand (unsigned) time (NULL ));
For (I = 0; I <LEN; I ++ ){
Array [I] = rand () %100;
Printf ("% d", array [I]);
}
Printf ("\ n ");
Return 0;
}
 
Int main (void ){
Int num [LEN];
Generate_array (num );
BiTree T = NULL;
Int I;
For (I = 0; I <LEN; I ++ ){
BSTInsert (& T, num [I]);
}
Printf ("traverse the binary search tree in the middle order: \ n ");
InOrderTraverse (T );
Printf ("Search: \ n ");
For (I = 0; I <100; I ++ ){
If (BSTSearch2 (T, I ))
Printf ("the tree contains: % d \ n", I );
}
Printf ("Maximum node value: % d \ n", BSTMaxNode (T)-> data );
Printf ("minimum node value: % d \ n", BSTMinNode (T)-> data );
Printf ("% d", num [4]);
BiTree p;
If (p = BSTSearchPredecessor (BSTSearch2 (T, num [4]) {
Printf ("the precursor node is % d \ n", p-> data );
}
Else
Printf ("NO precursor node \ n ");
If (p = BSTSearchSuccessor (BSTSearch2 (T, num [4]) {
Printf ("successor node: % d \ n", p-> data );
}
Else
Printf ("no successor node \ n ");
Printf ("delete nodes smaller than 50: \ n ");
For (I = 0; I <50; I ++ ){
If (BSTDelete (& T, I ))
Printf ("delete node: % d \ n", I );
}
Printf ("sequential traversal after deletion: \ n ");
InOrderTraverse (T );
 
}

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.