- Experiment purpose and requirements
Objective: by designing a personal telephone number query system, we can further be familiar with the concept, basic knowledge and skills of some binary trees, and use the basic knowledge and skills to solve simple object-oriented programming problems. Realize fast query according to the information entered by the user.
Requirements: implement a simple personal telephone number query system, sort the data according to the information entered by the user (such as name, etc.), realize the balanced binary tree, and query the data quickly. Programming to complete the general management of the address book, such as the addition, modification, search, deletion, output and other functions of the records in the address book. Each record contains basic personal information such as name, telephone number and address.
(1) In the external storage, save the telephone number information with a file;
(2) In the memory, a data structure is designed to store telephone number information;
(3) Provide query function, such as fast query according to name;
(4) Provide other maintenance functions, such as insert, delete, modify, etc.
- Experimental steps
1. Analysis of experimental problems
You need to use the knowledge of balanced binary tree learned in the data structure class to realize deletion and insertion and increase functions. Understand the basic algorithms related to binary trees. Save the input information into and output from a file.
2. Experimental summary analysis
1. Use structure to store contact information;
2. Initialize binary tree;
3. Record the height of nodes and solve the problem of node imbalance;
4. Realize the function of adding contacts and querying;
5. Main function.
- Experimental content
The code is implemented as follows:
#include <stdio.h> #include <stdlib.h> struct stu_num { char stu_name[30];//full name int num;//Telephone int number;//number }; //Defines the structure of a binary tree typedef struct person_list { struct person_list *lchild;//Left child struct person_list *rchild;//Right child int height; struct stu_num perfor_info;//Data of structure type }person_node; //Initialize binary tree person_node *request_person_node(const struct stu_num *value) { person_node *new_node; new_node = malloc(sizeof(person_node)); if (new_node == NULL) { perror("Binary tree node not requested\n"); return NULL; } if (value != NULL) { new_node->perfor_info = *value; new_node->lchild = NULL; new_node->rchild = NULL; } return new_node; } //Record the height of the node #define MAX(A, B) ((A)>(B)?(A):(B)) int get_tree_height(person_node *root) { if(root == NULL) return 0; return MAX(get_tree_height(root->lchild), get_tree_height(root->rchild))+1; } //Solve the left imbalance person_node *tree_node_rotate_right(person_node *root) { person_node *tmp; tmp = root->lchild; root->lchild = tmp->rchild; tmp->rchild = root; tmp->height = get_tree_height(tmp); root->height = get_tree_height(root); return tmp;//Return the new root address to the user calling this function to update the new root address } //Solve the right imbalance person_node *tree_node_rotate_left(person_node *root) { person_node *tmp; tmp = root->rchild; root->rchild = tmp->lchild; tmp->lchild = root; tmp->height = get_tree_height(tmp); root->height = get_tree_height(root); return tmp; } //Solve the left-right imbalance person_node *tree_node_rotate_left_right(person_node *root) { root->lchild = tree_node_rotate_left(root->lchild); root = tree_node_rotate_right(root); return root; } //Solve right left imbalance person_node *tree_node_rotate_right_left(person_node *root) { root->rchild = tree_node_rotate_right(root->rchild); root = tree_node_rotate_left(root); return root; } //Add insert personal phone information person_node *insert_node_to_tree(person_node *root,person_node *new_node) { if (root == NULL) return new_node; if(root ->perfor_info.number > new_node->perfor_info.number) { root->lchild = insert_node_to_tree(root->lchild,new_node); } else { root->rchild = insert_node_to_tree(root->rchild,new_node); } if(get_tree_height(root->lchild)-get_tree_height(root->rchild) == 2)//Left imbalance { //If the inserted data is smaller than the data of the following left tree node, it must be inserted to the left of root - > lchild, resulting in left-left imbalance if(new_node->perfor_info.number < root->lchild->perfor_info.number)//Left left imbalance { root = tree_node_rotate_right(root); } else//Otherwise, it is inserted to the right of root - > lchild, resulting in left-right imbalance { root = tree_node_rotate_left_right(root); } } else if(get_tree_height(root->rchild)-get_tree_height(root->lchild) == 2)//Right imbalance { if( new_node->perfor_info.number >= root->rchild->perfor_info.number)//Right right imbalance { printf("Right imbalance\n"); root = tree_node_rotate_left(root); } else//Right left imbalance { root = tree_node_rotate_right_left(root); } } root->height = get_tree_height(root); return root; } //Search and query personal phone information person_node *find_tree_node(person_node *root,int input_value) { if (root == NULL) return NULL; if (root->perfor_info.number > input_value) { return find_tree_node(root ->lchild,input_value); } else if (root->perfor_info.number < input_value) { return find_tree_node(root ->rchild,input_value); } return root; } //Traverse and print all telephone information in middle order void tree_for_each(person_node *root) { if (root == NULL) { return; } tree_for_each(root->lchild); printf("No.:%d,name:%s,Phone number:%d\n",root->perfor_info.number,root->perfor_info.stu_name,root->perfor_info.num); tree_for_each(root->rchild); } int main(void) { int choose,input_value,a; struct stu_num input_person_data;//Define a structure type data to cache contact data person_node *root = NULL,*new_node,*find_node; //Initialize a node. The root is empty while(1) { printf("****Enter the number to select the corresponding command****\n"); printf("****1.Add a Contact **************\n"); printf("****2.find contact **************\n"); printf("****3.Output all contacts**********\n"); printf("****4.Exit the system**************\n"); printf("******************************\n"); scanf("%d",&choose); switch(choose) { case 1: printf("Please enter contact number:\n"); scanf("%d",&input_person_data.number); printf("Please enter the name of the contact:\n"); scanf("%s",input_person_data.stu_name); printf("Please enter the phone number of the contact:\n"); scanf("%d",&input_person_data.num); new_node = request_person_node(&input_person_data); root = insert_node_to_tree(root,new_node); break; case 2: printf("Please enter the contact number query information\n"); scanf("%d",&input_value); find_node = find_tree_node(root,input_value); if (find_node == NULL) { printf("The contact's information could not be found\n"); break; } printf("No.:%d, full name:%s, Phone number:%d\n",find_node->perfor_info.number,find_node->perfor_info.stu_name,find_node->perfor_info.num); break; case 3: printf("The information of all contacts is:\n"); tree_for_each(root); break; case 4: goto log_out; } } return 0; log_out: return 0; }
4. Experimental results
5. Experimental summary and analysis
Through this experiment, we realized that we should master the following contents:
1. Learn to do a good job of modules first, and refine them from large to small.
2. Write the function, test and debug it.
3. Be good at using library functions when writing programs, which can improve efficiency.
4. When defining a function, you should select the number of parameters and data type.
However, we still need to further learn the relevant knowledge of binary tree, so that we can more skillfully use data structure (C + +) to design the system.