Data structure - user login system

Keywords: iOS Programming

Course design of data structure user management system

Things involved: MFC, file, AVL;
As a beginner, record the course design of this semester's data structure; first, the requirements of the task book.
1. [problem description]:
When logging in the server system, you need to verify the user name and password, such as telnet remote login server.
After the user enters the user name and password, the server program will first verify the legitimacy of the user information. Due to the verification of user information
With high frequency, it is necessary for the system to organize these user information effectively, so as to search and verify users quickly. In addition, the system
It will often add new users, delete old users and update user passwords. Therefore, the system must adopt a dynamic structure,
After adding, deleting or updating, the verification process can still be fast. Please use the corresponding data structure to simulate user login
System, its functional requirements include user login, user password update, user add and user delete.
2. [basic requirements]:

  1. The binary tree structure and its related functions are required to be programmed to store user information. Standard template classes are not allowed
    The binary tree structure and function of. At the same time, according to the change of binary tree, the corresponding balance operation, i.e. AVL, is required
    Four kinds of balancing operations must be considered in balancing tree operation. During the test, all kinds of situations need to be tested, and the test section is attached
    Graph;
  2. It is required to adopt the design idea of class. It is not allowed to have function definitions other than class, but it is allowed to have friend function. Only in the main function
    Calls to member functions of a class are allowed, and calls to other functions are not allowed.
  3. Multi file mode is required: Declaration of. h file storage class, implementation of. cpp file storage class, main function storage
    Stored in a separate cpp file. If a class template is used, the declaration and implementation of the class are in the. h file.
  4. Class template and visual window are not required, and corresponding comments are required in the source program;
  5. It is required that the test examples should be more detailed, and various limit situations should be considered. The output information of the test should be detailed and easy to understand
    The implementation of each function is correct;
  6. It is recommended to use Visual C++ 6.0 and above for debugging;

In short, it is three points: 1. Realize the operation of balanced binary tree; 2. Read and write files; 3. Design of login interface;

1. Implementation of balanced binary tree

The key functions of AVL tree include the height of update tree and four kinds of rotation operations;
1.LL (left single rotation)

template<typename T>
binnode<T>*AVL<T>::LL(binnode<T>*ptr) {

	binnode<T>*tmp = ptr->right;
//Rotate the right node of the incoming node;
	ptr->right = tmp->left;//Hang the left subtree of tmp on the right subtree of ptr
	tmp->left = ptr;//Hang ptr as the left subtree of tmp
	ptr->height = getheight(ptr->left) > getheight(ptr->right) ? getheight(ptr->left)+1 : getheight(ptr->right)+1;//The function to update the tree height is explained later
	tmp->height = getheight(tmp->left) > ptr->height ? getheight(tmp->left) + 1 : ptr->height + 1;
	return tmp;//Return the root node after rotation;

}

Left single rotation diagram:

2.RR rotation (right single rotation)

template<typename T>
binnode<T>* AVL<T>::RR(binnode<T>*ptr) {

	binnode<T>*tmp = ptr->left;//tmp is the left daughter of ptr
	ptr->left = tmp->right;//Hang the right subtree of tmp on the left subtree of ptr
	tmp->right = ptr;//ptr as a subtree is hung on the right subtree of tmp;
	ptr->height = getheight(ptr->left) > getheight(ptr->right) ? getheight(ptr->left)+1 : getheight(ptr->right)+1;//Update tree height
	tmp->height = getheight(tmp->left) > ptr->height ? getheight(tmp->left) + 1 : ptr->height + 1;
	return tmp;//Return to the current root node;
}

Right single rotation diagram:

3. LR: trouble nodes appear on the right subtree of the left subtree of the unbalanced node;

template<typename T>
binnode<T>*AVL<T>::LR(binnode<T>*ptr) {

	 ptr->left = LL(ptr->left);//First, the left node of ptr is rotated to the left

	return RR(ptr);//After the ptr was right-handed and returned;
    //The double rotation can be regarded as two single rotation;

}

Left and right double rotation diagram:

4. Right left double rotation (RL): when the trouble node appears on the left subtree of the right subtree of the unbalanced node;

template<typename T>
binnode<T>*AVL<T>::RL(binnode<T>*ptr) {
   //As above, it can be decomposed into two double rotation processes;

    ptr->right= RR(ptr->right);//Right turn right child

	return LL(ptr);//Turn it to the left;
}

Right left double rotation:

5. Insert function: for the node to be inserted, first search the AVL tree, if found, do not insert, if not found, insert. (note that AVL is a sort tree, so the operation of inserting is similar to that of inserting the BST tree, but only one more operation of updating the height of the tree);

template<typename T>
void AVL<T>::insert( binnode<T>* &ptr, const T &item) {

	if (ptr==NULL)
	{
		ptr = new binnode<T>();
           //When ptr is NULL, it is proved that there is no node to insert in this node, so the operation of inserting is needed;
		ptr->data = item;
		ptr->height=1;//Set the height of the tree to 1;
		ptr->left = ptr->right = NULL;//Left and right subtrees empty;
		
		
	}
	else if (item<ptr->data)
	{
	     insert(ptr->left, item);//Recursively insert left subtree

		if (getheight(ptr->left)-getheight(ptr->right)==2)
		{//When imbalance occurs, only the left subtree is higher than the right subtree because it is operated on the left subtree;

			if (item<ptr->left->data)
			{//When the inserted node constitutes RR imbalance, that is, the troublesome node is on the left subtree of the left subtree of the unbalanced node;
				ptr = RR(ptr);
		//Dextral rotation	
			}
			else {
				ptr = LR(ptr);
		//If LR imbalance is formed, left-handed;
			}
		}
	}
	else if (item > ptr->data) {
           //The operation of right subtree is the same as that of left subtree;
		   insert(ptr->right, item);

		if (getheight(ptr->left)-getheight(ptr->right)==-2)
		{
			if (item > ptr->right->data) {
				
				ptr = LL(ptr);

			}
			else {
				ptr = RL(ptr);
			}
		}
	}
	else
	{
		cerr << "The item already exists in the tree, insertion failed";
		return;
		
	}
	ptr->height=getheight(ptr->left) > getheight(ptr->right) ? getheight(ptr->left) +1: getheight(ptr->right)+1;
//At the end of the function, the tree height needs to be updated;
}

6. Delete function: the same as the delete of BST tree, there are three situations, and pay attention to the balance situation; the code is as follows:

template <typename T>
void AVL<T>::remove(binnode<T>*&ptr, const T &item) {
//The operation is similar to inserting, but when deleting, there must be left and right subtrees, or none, or only one
//No more details;
	if (ptr == NULL) {
		return;
	}
	else {

		if (item < ptr->data) {


			remove(ptr->left, item);

			if (getheight(ptr->right) - getheight(ptr->left) > 1) {

				if (getheight(ptr->right->left) > getheight(ptr->right->left)) {
					ptr = RL(ptr);

				}
				else {
					ptr = LL(ptr);
				}
			}
			else {

				ptr->height = getheight(ptr->left) > getheight(ptr->right) ? getheight(ptr->left) + 1 : getheight(ptr->right) + 1;
			}


		}
		else if (item > ptr->data) {
			remove(ptr->right, item);

			if (getheight(ptr->left) - getheight(ptr->right) > 1) {

				if (getheight(ptr->left->right) > getheight(ptr->left->left)) {

					ptr = LR(ptr);
				}
				else {
					ptr = RR(ptr);
				}

			}
			else
			{
				ptr->height = getheight(ptr->left) > getheight(ptr->right) ? getheight(ptr->left) + 1 : getheight(ptr->right) + 1;
			}
		}
		else {
			if (ptr->left != NULL && ptr->right != NULL) {

				if (getheight(ptr->left) > getheight(ptr->right)) {
					binnode<T>*tmp = ptr->left;
					while (tmp->right != NULL) {
						tmp = tmp->right;
					}
					ptr->data = tmp->data;
					remove(ptr->left, ptr->data);
				}
				else {

					binnode<T>*tmp = ptr->right;

					while (tmp->left != NULL) {

						tmp = tmp->left;

					}
					ptr->data = tmp->data;
					remove(ptr->right, ptr->data);

				}
			}
			else {
				binnode<T>*tmp = ptr;

				if (ptr->left != NULL) {

					ptr = ptr->left;
				}
				else {

					ptr = ptr->right;

				}
				delete  tmp;
			}
        
		}
	}

}

7. Tree height update: add data height to the node node to record the height of the current node's tree;

template<typename T>
class binnode {

public:

	T data;

	binnode*left;

	binnode*right;

	int  height;//Record the height of the tree;

};

Get tree height function

template<typename T>
int AVL<T>::getheight(binnode<T>*p) {

	if (p == NULL) {
		int a = 0;
		return a;
	}
	else
	{
		return p->height;//For non empty nodes, the tree height can be accessed directly;
	}

	
}

User interface design (MFC library)

In this assignment, MFC (downloadable in VS) is used as the user interface; first, the result chart is used to explain step by step;

The interface consists of password input box, user name input box and four controls;
1. Create a new MFC dialog box;

Select MFC application program in VS new creation, no friends can download it in extension program by themselves; after opening, select dialog based and continue to the next step;

Then you'll come to the interface like the following picture
Add related controls in the toolbar on the left; after adding, we need to add a class to the dialog box and add variables to the related controls; (the purpose is that they can be connected with the program by normal calls);
1. Add a class (1) right click the dialog box to add a class; then a. h header file will be generated automatically; (2) add a variable: right click the related control to add a variable and name the variable;
2. Programming of related controls: take the login button as an example, after adding variables, double-click the login button; you will enter the class just added and include the header file;

#include "stdafx.h"
#include "classwork.h"
#include "CDia.h"//The file just added, include it;
#include "afxdialogex.h"
#include "AVL.h"//include the written AVL tree header file, and initialize a tree according to the file content

Double click to log in and it will automatically jump to the writing of the control;

void CclassworkDlg::OnBnClickedButton1()
{      //Implementation of login button.
		UpdateData(true);//Get the value in the latest user name and password input box
	if (user_name.IsEmpty()) {//User? Name is the added user name variable;
		MessageBox(_T("Login failed, user name cannot be empty"));
		return;//When the user name is empty, return;
	}
	string str2 = CW2A(user_name.GetString());
//Since the type of user input box is cstring type, it needs to be converted to string type;
//Use CW2A to convert a CSstring to a string type.
	if (t.search(str2)) {
		//Find the given user name in the tree and call the find function in the tree (not written out in this article)
		//If the search function is found successfully and returns true, it fails to find and returns false;
		string str1 = CW2A(user_pwd.GetString());
		//If found, assign the value in the input box to str1;
		if (f.search(str2)==str1) {
			//Call the File class search function and return the password for comparison
			//The search function in the flie class searches the file according to the user name, and returns the password if it is found;
			//If the password matches, the login is successful;
			MessageBox(_T("Login succeeded! Welcome!"));
		}
		else {
			MessageBox(_T("Login failed, password error"));
		}
	}
	else {
		MessageBox(_T("Login failed, user name does not exist"));
	}
}

Other controls are similar in writing, but more details are needed;

File file class (read-write)

The purpose to be realized: return the password according to the content of the file;

class file{
	public:
	void Insert(string name,string pwd);//Insert new user into text
	void remove(string name);//Delete the specified user in the text
	string search(string name);//Return the password according to the user name;
	private:
		fstream f;
};

Insert function:

void file::Insert(string name,string pwd){
	if(search(name)!="-1"){
		return;
	}
	f.open("1.txt",ios::app|ios::out);
	f<<name<<" "<<pwd<<endl;
	f.close(); 
}

Delete function: first of all, because flie has no way to delete the specified content, the idea is to selectively save the content in the text, and then write it again; the code is as follows;

void file::remove(string name){
		fstream tmpf;	
		tmpf.open("2.txt",ios::out|ios::trunc);
		f.open("1.txt",ios::in);	
		while(1){
			string str1,str2;
			f>>str1>>str2;
			if(str1!=name){	
			tmpf<<str1<<" "<<str2<<endl;
		    }
			if(f.eof()){
				break; 
			}
		}  
        f.close();
	    tmpf.close();
	    tmpf.open("2.txt",ios::in);
		f.open("1.txt",ios::out|ios::trunc) ;
		while(1){
			string str1,str2;
			tmpf>>str1>>str2;
			f<<str1<<" "<<str2<<endl;
			if(tmpf.eof()){
				break;
			}	
		} 
	    tmpf.close() ;
	    tmpf.open("2.txt",ios::trunc|ios::out);
	    tmpf.close();
	    f.close();
}

Search function: search the given user name and return the password;

string file::search(string name){
	f.open("1.txt",ios::in);
	while(1){
		string str,str2;
		f>>str>>str2;
		if(name==str){
			f.close();
			return str2;
		}
		else if(f.eof()){
			f.close();
			break;
			
		}
	} 
	f.close();
	return "-1";//Use - 1 as a special value;
	
}

The above is about the content of this course design. After the teacher's correction, he found some errors. For example, in the search of login control, it should be the search and deletion of AVL tree, and the file stream only provides the read-write operation. The above is only the implementation of some key functions. It is not complete code, with limited ability. I hope you can give me some advice. This semester's computer major just turned , I'm still a rookie (record my growth), welcome to give me some advice; if you want the complete code, you can send it to me personally;
//If there is any problem in the operation of AVL tree, please refer to the book of Chen Yue (second edition of data structure) (more detailed analysis in the book)

Published 1 original article, praised 0 and visited 13
Private letter follow

Posted by localhost on Wed, 15 Jan 2020 03:03:30 -0800