Course Design of Data Structure -- balanced binary tree

Source: Internet
Author: User

The last assignment of my sophomore year will come to an end after tomorrow's reply. This course was not designed with great care, so it was basically completed on a pay-as-you-go basis. However, there are still a lot of things to learn, so I just finished writing it and carefully sorted it out, easy to learn later.

Next, enter the subject

Use a balanced binary tree to implement a dynamic search table

(1) three basic functions for dynamic table search: search, insert, and delete;

(2) merge two balanced binary trees;

(3) split a balanced binary tree into two balanced binary trees, so that all the keywords in a tree are less than or equal to X, and any keyword in the other tree is greater than X.

Completion description

The course is written in c/c ++ language, and the development platform selects vc6.0. (Khan, I still only use vc, of course I also use TC, but the antique class is really not used), and I have never learned the interface in C language, so we can only use dos. 

Outline Design

Typedef int Status;

Typedef int ElemType;

Typedef struct BSTNode {

ElemType data;

Int bf;

Struct BSTNode * lchild, * rchild;

} BSTNode, * BSTree;

 

Status SearchBST (BSTree T, ElemType e) // find

Void R_Rotate (BSTree & p) // right-hand

Void L_Rotate (BSTree & p) // left-hand

Void LeftBalance (BSTree & T) // insert Balance Adjustment

Void RightBalance (BSTree & T) // insert Balance Adjustment

Status InsertAVL (BSTree & T, ElemType e, int & taller) // insert

Void DELeftBalance (BSTree & T) // Delete Balance Adjustment

Void DERightBalance (BSTree & T) // Delete Balance Adjustment

Status Delete (BSTree & T, int & shorter) // Delete operation

Status DeleteAVL (BSTree & T, ElemType e, int & shorter) // delete an operation

Void merge (BSTree & T1, BSTree & T2) // merge operation

Void splitBSTree (BSTree T, ElemType e, BSTree & T1, BSTree & T2) // split the operation

Void PrintBSTree (BSTree & T, int eV) // show in the concave table

 

Next we will attach all source code

# Include <stdio. h>
# Include <stdlib. h>
// # Define TRUE 1
// # Define FALSE 0
// # Define OK 1
// # Define ERROR 0
# Define LH + 1
# Define EH 0
# Define RH-1

Typedef int Status;
Typedef int ElemType;
Typedef struct BSTNode {
ElemType data;
Int bf;
Struct BSTNode * lchild, * rchild;
} BSTNode, * BSTree;

 


/*
Search Algorithm
*/
Status SearchBST (BSTree T, ElemType e ){
If (! T ){
Return FALSE; // search failed
}
Else if (e = T-> data ){
Return TRUE; // search successful
}
Else if (e <T-> data ){
Return SearchBST (T-> lchild, e );
}
Else {
Return SearchBST (T-> rchild, e );
}
}


// Right-handed
Void R_Rotate (BSTree & p ){
BSTree lc;
Lc = p-> lchild;
P-> lchild = lc-> rchild;
Lc-> rchild = p;
P = lc;

}
// Left-handed
Void L_Rotate (BSTree & p ){
BSTree rc;
Rc = p-> rchild;
P-> rchild = rc-> lchild;
Rc-> lchild = p;
P = rc;
}

Void LeftBalance (BSTree & T ){
BSTree lc, rd;
Lc = T-> lchild;
Switch (lc-> bf ){
Case LH:
T-> bf = lc-> bf = EH;
R_Rotate (T );
Break;
Case RH:
Rd = lc-> rchild;
Switch (rd-> bf ){
Case LH: T-> bf = RH; lc-> bf = EH;
Break;
Case EH: T-> bf = lc-> bf = EH;
Break;
Case RH: T-> bf = EH; lc-> bf = LH;
Break;
}
Rd-> bf = EH;
Rochelle rotate (T-> lchild );
R_Rotate (T );
}
}

Void RightBalance (BSTree & T)
{
BSTree rc, ld;
Rc = T-> rchild;
Switch (rc-> bf ){
Case RH:
T-> bf = rc-> bf = EH;
Rochelle rotate (T );
Break;
Case LH:
Ld = rc-> lchild;
Switch (ld-> bf ){
Case LH: T-> bf = RH; rc-> bf = EH;
Break;
Case EH: T-> bf = rc-> bf = EH;
Break;
Case RH: T-> bf = EH; rc-> bf = LH;
Break;
}
Ld-> bf = EH;
R_Rotate (T-> rchild );
Rochelle rotate (T );
}
}
// Insert a node
Status InsertAVL (BSTree & T, ElemType e, int & taller ){
If (! T ){
T = (BSTree) malloc (sizeof (BSTNode ));
T-> data = e;
T-> lchild = T-> rchild = NULL;
T-> bf = EH;
Taller = 1;
}
Else {
If (e = T-> data ){
Taller = 0;
Return ERROR;
}
If (e <T-> data ){
If (! InsertAVL (T-> lchild, e, taller ))
Return ERROR;
If (taller)
Switch (T-> bf ){
Case LH:
LeftBalance (T );
Taller = 0;
Break;
Case EH:
T-> bf = LH;
Taller = TRUE;
Break;
Case RH:
T-> bf = EH;
Taller = FALSE;
Break;
}
}
Else {
If (! InsertAVL (T-> rchild, e, taller )){
Return ERROR;
}
If (taller)
Switch (T-> bf ){
Case LH:
T-> bf = EH;
Taller = FALSE;
Break;
Case EH:
T-> bf = RH;
Taller = TRUE;
Break;
Case RH:
RightBalance (T );
Taller = FALSE;
Break;
}
}
}
Return 1;
}
Void DELeftBalance (BSTree & T ){
BSTree lc, rd;
Lc = T-> lchild;
Switch (lc-> bf ){
Case LH:
T-> bf = EH;
// Lc-> bf = EH;
R_Rotate (T );
Break;
Case EH:
T-> bf = EH;
Lc-> bf = EH;
R_Rotate (T );
Break;
Case RH:
Rd = lc-> rchild;
Switch (rd-> bf ){
Case LH: T-> bf = RH; lc-> bf = EH;
Break;
Case EH: T-> bf = lc-> bf = EH;
Break;
Case RH: T-> bf = EH; lc-> bf = LH;
Break;
}
Rd-> bf = EH;
Rochelle rotate (T-> lchild );
R_Rotate (T );
}
}

Void DERightBalance (BSTree & T)
{
BSTree rc, ld;
Rc = T-> rchild;
Switch (rc-> bf ){
Case RH:
T-> bf = EH;
// Rc-> bf = EH;
Rochelle rotate (T );
Break;
Case EH:
T-> bf = EH;
// Rc-> bf = EH;
Rochelle rotate (T );
Break;
Case LH:
Ld = rc-> lchild;
Switch (ld-> bf ){
Case LH: T-> bf = RH; rc-> bf = EH;
Break;
Case EH: T-> bf = rc-> bf = EH;
Break;
Case RH: T-> bf = EH; rc-> bf = LH;
Break;
}
Ld-> bf = EH;
R_Rotate (T-> rchild );
Rochelle rotate (T );
}
}

 

Void SDelete (BSTree & T, BSTree & q, BSTree & s, int & shorter ){

If (s-> rchild ){
SDelete (T, s, s-> rchild, shorter );
If (shorter)
Switch (s-> bf ){
Case EH:
S-> bf = LH;
Shorter = 0;
Break;
Case RH:
S-> bf = EH;
Shorter = 1;
Break;
Case LH:
DELeftBalance (s );
Shorter = 0;
Break;
}
Return;
}

T-> data = s-> data;
If (q! = T)
Q-> rchild = s-> lchild;
Else
Q-> lchild = s-> lchild;
Shorter = 1;

}
// Delete a node
Status Delete (BSTree & T, int & shorter ){
BSTree q;
If (! T-> rchild ){
Q = T;
T = T-> lchild;
Free (q );
Shorter = 1;
}
Else if (! T-> lchild ){
Q = T;
T = T-> rchild;
Free (q );
Shorter = 1;
}
Else {
SDelete (T, T, T-> lchild, shorter );
If (shorter)
Switch (T-> bf ){
Case EH:
T-> bf = RH;
Shorter = 0;
Break;
Case LH:
T-> bf = EH;
Shorter = 1;
Break;
Case RH:
DERightBalance (T );
Shorter = 0;
Break;
}
}
Return TRUE;
}

Status DeleteAVL (BSTree & T, ElemType e, int & shorter ){
Int sign = 0;
If (! T ){
Return sign;
}
Else {
If (e = T-> data ){
Sign = Delete (T, shorter );
Return sign;
}

Else if (e <T-> data ){
Sign = DeleteAVL (T-> lchild, e, shorter );
If (shorter)
Switch (T-> bf ){
Case EH:
T-> bf = RH;
Shorter = 0;
Break;
Case LH:
T-> bf = EH;
Shorter = 1;
Break;
Case RH:
DERightBalance (T );
Shorter = 0;
Break;
}

Return sign;
}

Else {
Sign = DeleteAVL (T-> rchild, e, shorter );
If (shorter)
Switch (T-> bf ){
Case EH:
T-> bf = LH;
Shorter = 0;
Break;
Case RH:
T-> bf = EH;
Break;
Case LH:
DELeftBalance (T );
Shorter = 0;
Break;
}
Return sign;
}

}
}
// Merge
Void merge (BSTree & T1, BSTree & T2 ){
Int taller = 0;
If (! T2)
Return;
Merge (T1, T2-> lchild );
InsertAVL (T1, T2-> data, taller );
Merge (T1, T2-> rchild );

}
Void split (BSTree T, ElemType e, BSTree & T1, BSTree & T2 ){
Int taller = 0;
If (! T)
Return;
Split (T-> lchild, e, T1, T2 );
If (T-> data> e)
InsertAVL (T2, T-> data, taller );
Else
InsertAVL (T1, T-> data, taller );
Split (T-> rchild, e, T1, T2 );

}
// Split
Void splitBSTree (BSTree T, ElemType e, BSTree & T1, BSTree & T2 ){
BSTree t1 = NULL, t2 = NULL;
Split (T, e, t1, t2 );
T1 = t1;
T2 = t2;
Return;
}

// Build
Void CreatBSTree (BSTree & T ){
Int num, I, e, taller = 0;
Printf ("Number of input nodes :");
Scanf ("% d", & num );
Printf ("Enter the node value \ n" in sequence ");
For (I = 0; I <num; I ++ ){
Printf ("value of node % d", I + 1 );
Scanf ("% d", & e );
InsertAVL (T, e, taller );
}
Printf ("build successful, input any character to return \ n ");
Getchar ();
Getchar ();

}
// Display in the concave table format
Void PrintBSTree (BSTree & T, int eV ){
Int I;
If (T-> rchild)
PrintBSTree (T-> rchild, lev+ 1 );
For (I = 0; I <lev; I ++)
Printf ("");
Printf ("% d \ n", T-> data );
If (T-> lchild)
PrintBSTree (T-> lchild, lev+ 1 );
}

 

 

Void Start (BSTree & T1, BSTree & T2 ){

Int cho, taller, e, k;
Taller = 0;
K = 0;
While (1 ){
System ("cls ");
Printf ("demo of balanced binary tree operation \ n ");
Printf ("************************************* **************************************** * \ n ");
Printf ("balanced binary tree display area \ n ");
Printf ("T1 tree \ n ");
If (! T1)
Printf ("\ n currently empty tree \ n ");
Else {
PrintBSTree (T1, 1 );
}

Printf ("T2 tree \ n ");
If (! T2)
Printf ("\ n currently empty tree \ n ");
Else
PrintBSTree (T2, 1 );
Printf ("\ n *********************************** **************************************** * ** \ n ");
Printf ("T1 operation: 1. Create 2. Insert 3. Search 4. Delete 10. Split \ n ");
Printf ("T2 operation: 5. Create 6. insert 7. Search 8. Delete 11. Split \ n ");
Printf ("9. Merge T1, T2 0. Exit \ n ");
Printf ("************************************* **************************************** * \ n ");
Printf ("Enter the operation you want to perform :");
Scanf ("% d", & cho );
Switch (cho ){
Case 1:
CreatBSTree (T1 );
Break;
Case 2:
Printf ("Enter the value of the keyword to be inserted ");
Scanf ("% d", & e );
InsertAVL (T1, e, taller );
Break;
Case 3:
Printf ("Enter the value of the keyword to be searched ");
Scanf ("% d", & e );

If (SearchBST (T1, e ))
Printf ("search successful! \ N ");
Else
Printf ("failed to search! \ N ");
Printf ("press any key to return 87 ");
Getchar ();
Getchar ();
Break;
Case 4:
Printf ("Enter the value of the keyword to be deleted ");
Scanf ("% d", & e );
If (DeleteAVL (T1, e, k ))
Printf ("deleted successfully! \ N ");
Else
Printf ("deletion failed! \ N ");
Printf ("press any key to return ");
Getchar ();
Getchar ();
Break;
Case 5:
CreatBSTree (T2 );
Break;
Case 6:
Printf ("Enter the value of the keyword to be inserted ");
Scanf ("% d", & e );
InsertAVL (T2, e, taller );
Break;
Case 7:
Printf ("Enter the value of the keyword to be searched ");
Scanf ("% d", & e );

If (SearchBST (T2, e ))
Printf ("search successful! \ N ");
Else
Printf ("failed to search! \ N ");
Printf ("press any key to return ");
Getchar ();
Getchar ();
Break;
Case 8:
Printf ("Enter the value of the keyword to be deleted ");
Scanf ("% d", & e );
If (DeleteAVL (T2, e, k ))
Printf ("deleted successfully! \ N ");
Else
Printf ("deletion failed! \ N ");
Printf ("press any key to return ");
Getchar ();
Getchar ();
Break;
Case 9:
Merge (T1, T2 );
T2 = NULL;
Printf ("merged successfully, press any key to return ");
Getchar ();
Getchar ();
Break;
Case 10:
Printf ("Enter the value of the median ");
Scanf ("% d", & e );
SplitBSTree (T1, e, T1, T2 );
Printf ("split successful, press any key to return ");
Getchar ();
Getchar ();
Break;
Case 11:
Printf ("Enter the value of the median ");
Scanf ("% d", & e );
SplitBSTree (T2, e, T1, T2 );
Printf ("split successful, press any key to return ");
Getchar ();
Getchar ();
Break;
Case 0:
System ("cls ");
Exit (0 );
}

}


}

Void main (){
BSTree T1 = NULL;
BSTree T2 = NULL;
Start (T1, T2 );
}

Attach a running chart to the next one.

 

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.