A simple student score management program (binary tree implementation)

zhaozj2021-02-16  48

#include #include struct feng // Record score {Int pshuxue; int penglish; int pchesse; int count;} total; class stunode // child node {private: int ID; string name; int shuxue; Int english; int class; // left node stunode * right; // right node public: stunode (void); // Constructive STUNODE (int SID, STRING SNAME, INT SSHUXUE, INT SENGLISH, INT SCHINESE) ; ~ stunode (void); void insertleft (stunode * newnode); void insertright (stunode * newnode); stunode * returnleft (void); stunode * returnright (void); int getid (void); void setname (string nwname) { Name = nwname;} void setshuxue (int SSX) {shuxue = ssx;} void setnglish (int se) {English = se;} void setChinese (int sc) {Chinese = sc;} Friend Void PrintNode (stunode * head); // Print student score Friend void pingchengji (stunode * head); // calculate average grade}; stunode :: stunode (void) {left = null; right = null;} stunode :: stunode (int SID, STRING SNAME, INT) SSHUXUE, INT Senglish, int SCHINESE) {ID = SID; name = sname; shuxue = sshuxue; english = senglish; chinese = schinese; left = NULL; right = NULL;} stunode :: ~ stunode (void) {left = NULL; right = NULL;} void stunode :: insertleft (stunode * newnode) {left = newnode;} void stunode :: insertright (stunode * newnode) {right = newnode;} stunode * stunode :: returnleft (void) {return left;} stunode * stunode :: returnright (void) {return Right;} int stunode :: getId (void) {return;} void printnode (stunode * head) {cout << Head-> id << "<< Head-> name <<" "<< Head-> Shuxue << "<< Head-> English <<" << Head-> Chinese << endl;

} void pingchengji (stunode * head) {total.pshuxue = Total.pshuxue head-> shuxue; total.penglish = Total.PENGLISH head-> English; total.pchinese = Total.pchinese head-> Chinese; Total. Count = Total.count 1;} class stutree / binary tree {private: stunode * root, * newnode, * currnode, * parent; void print; void zonghe (stunode * temp); int ischild (); int ischild (STUNODE * TEMP); int ischild stunode * pnode, stunode * childnode); stunode * findnode (const int rid, stunode * & parentnode); public: stutree (void); void creattree (stunode * node); void prints (void); stunode * findnode (const int RID); void insertnode; void pingjun (void); stunode * deletenode (int DID); void deletetree; ~ stutree (void);}; stutree :: stutree (void) {root = NULL; newNode = NULL; currnode = null; parent = null;} void stutree :: deletetree (stunode * temp) {if (Temp! = Null) {deletetree (Temp-> returnft ()); deletetree (TEMP-> ReturnRight ()); delete temp;}} stutree :: ~ stutree (void) {deletetree (RO OT);} void stutree :: createtree (stunode * node) // Create a binary tree {if (root == null) {root = node;} else {if (node-> getId () getId ()) {Parent = root; currnode = root-> returnveft ();} if (node-> getId ()> root-> getId ()) {parent = root; currnode = root-> retURHT ();} if (node- > getId () == root-> getId ()) {cout << "The student was in list" << end1; return;} while (currnode! = null) {if (node-> getId () == currnode -> getId ()) {cout << "The student was in list" << Endl; return;} if (node->

GetId () getId ()) {parent = currnode; currnode = parent-> returnveft ();} else {parent = currnode; currnode = parent-> retURNRight ();}} if (node-> getId) getId ()) {parent-> insertleft (node); currnode = node;} if (node-> getId ()> parent-> getId ()) {parent-> insert (node); currnode = Node;}}} void stutree :: Print (stunode * temp) {= {print (Temp-> Returnleft ()); printnode (TEMP); print (Temp-> returnright ());} } void stutree :: zonghe (stunode * Temp) {i (temp! = null) {zonghe (Temp-> Returnleft ()); pingchengji (TEMP); zonghe (Temp-> ReturnRight ());}} void stutree: : Prints (void) {print (root);} stunode * stutree :: findNode (const- rid) {if (root-> getId () == rid) {return root;} if (rid getId) )) {Parent = root; currnode = root-> returnveft ();} if (rid> root-> getId ()) {parent = root; currnode = root-> returnright ();} while (currnode! = Null) {IF (currnode-> getId () == rid) { return currnode;} if (rid getid ()) {parent = currnode; currnode = currnode-> returnleft ();} else if (rid> currnode-> getid ()) {parent = currnode; currnode = currnode -> retURNRIGHT ();}} Return NULL;}

Stunode * stutree :: FindNode (const Int rid, stunode * & parntnode) {if (root-> getId () == rid) {parentnode = null; return root;} if (Rid getId ()) { Parent = root; currnode = root-> returnveft ();} if (rid> root-> getId ()) {parent = root; currnode = root-> ReturnRight ();} while (currnode! = null) {IF Currnode-> getId () == RID) {ParentNode = Parent; return currnode;}} ()) {parent = currnode; currnode = currnode-> returnveft ();} else if (rid> currnode-> getid ()) {parent = currnode; currnode = currnode-> returnright ();}}} void stutree :: insertnode (stunode * temp) {creattree (temp);} void stutree :: pingjun (void) { Zonghe (root); cout << "Shuxue:" << Total.pshuxue / total.count << endl; cout << "English:" << Total.PENGLISH / TOTAL.COUNT << Endl; cout << "chinese : "<< Total.pChinese / Total.count << Endl;} int stutree :: ischild (stunode * pnode, stunode * childNode) {// Judgment the bit relationship IF of the parent node and sub-node (pnode == null) {Return -1; } IF (pnode-> returnveft () == childNode) {return 1;} if (pnode-> returnright () == childNode) {return 0;}} stunode * stutree :: deleteNode (int DID) // Delete node {stunode * parentnode, * temp; stunode * leftnode, * rightnode; stunode * minxnode, * minright, * parenode = NULL, * renode; int postion; temp = findnode (did, parentnode); // temp node is to be removed IF (Temp == NULL) {cout << "student not find" << end1;} posthen = ischild (parentnode, temp); leftnode = temp->

RightNode = Temp-> retURNRight (); if (leftnode == null && rightnode == null) // Temp left and right nodes {Switch (post) {case 1: ParentNode-> Insert (NULL); BREAK Case 0: ParentNode-> INSERTRIGHT (NULL); Break; Case-1: Break;} delete temp; return root;} if (leftnode! = Null && rightnode == null) // There is no right node {SWITCH (postnode); Break; case 0: Parentnode-> InsertNode; Break; Case -1: root = root-> returnft (); break;} delete temp; return;} delete temp; return; Root;} if (leftnode == null && rightnode! = null) There is no left node {switch (post) {casse 1: parentnode-> INSERTLEFT (RightNode); Break; cas 0: Parentnode-> InsertNode Break; Case -1: root = root-> retURNRight (); Break; } Delete Temp; Return Root;} IF (LeftNode! = Null && RightNode! = Null) There is {Parenode = rightnode; renode = rightnode; // renode is the replacement node while (renode-> returnveft ()! = NULL) // Find the replacement node {// The minimum node in the right sub-tree in the right child is the replacement node we are looking for Parenode = renode; // can also find the most larger node in the left subtree of the left child Renode = renode -> returnveft ();} If (renode == rightnode) // One case is: Replace node is Temp's right son {rightnode-> insertleft (leftnode);

} Else {// If you don't have the right son, you will do the following: Renode-> ReturnRight (); renode-> insertleft; renode-> insert (RightNode);} switch (post) {casse 1: root = renode; break; case 1: parentnode-> insertleft (renode); break; cas 0: Parentnode-> InsertRight (renode); break;} delete temp; return root;}} void main (void) {int Select, NID, NSHUXUE, NENGLISH, NCHINESE COUT << "2> Print a list" << endl; cout << "3> Find student << endl; cout <<" 4> INSERT A Student << Endl; cout << "5> ping jun CHENG JI "<< Endl; cout <<" 6> delete a student "<< endl; cout <<" 7> Chang student information "<< Endl COUT << "0> EXIT" << Endl; cin >> select; switch (select) {case 1: cout << "" please input a id "<< endl; cin >> NID; getLine (CIN, Space, '/ n'); cout << "please input a name" << endl; getLine (cin, nname, '/ n'); cout << "please input a shuxue" << endl; cin >> nShuxue; cout << "" please input a english "<< Endl; CIN >> NENGLISH; COUT <<" please input a Chinese "" << endl; cin >> nCHINESE

Newstudent = New Stunode (NID, NNAME, NSHUXUE, NEENGLISH, NCHINESE); if (newstudent == null) {cout << "error" << endl; exit (1);} stulist.creattree (newstudent); break; cas 2: stulist.prints (); Break; Case 3: cout << "please input a id for student" << Endl; cin >> wid; findrezult = stulist.findNode (wid); if (FindRezult! = Null) { PRINTNODE (FINDREZUT);} else {cout << "student not find << Endl;} Break; Case 4: cout <<" please input a id "<< Endl; CIN >> NID; GetLine (CIN, Space, '/ n'); cout << "please input a name" << endl; getLine (cin, nname, '/ n'); cout << "please input a shuxue" << endl; cin >> nShuxue; cout << "please input a english" << Endl; CIN >> NENGLISH; cout << "please input a Chinese" << Endl; cin >> nCHINESE; newstudent = new stunode (NID, NNAME, NSHUXUE, NENGLISH, NCHINESE) ; If (newstudent == null) {cout << "error" << endl; exit (1);} stulist.insertnode (newstudent); break; case 5: stulist.pingjun (); break; CAS E 6: COUT << "please input a id" << endl; cin >> deid; stulist.deletenode (deid); break; case 7: cout << "please input a student id for chenge << endl; cin >> NID; FINDREZULT = Stulist.FindNode (NID); cout << "20.change name" << endl; cout << "21.change shuxue << Endl; cout << 22.change english << Endl; cout << "23.change Chinese << Endl; CIN >>

SELECT; SWITCH (SELECT) {Case 20: getLine (cin, space, '/ n'); cout << "please input new name" << endl; getLine (cin, nname, '/ n'); Findrezult-> SetName (NName); Break; Case 21: COUT << "please input new shuxue" << Endl; cin >> nShuxue; findrezult-> setshuxue (nshuxue); Break; Case 22: cout << "please input new English" << endl; cin >> Nenglish; new; Break; Case 23: COUT << "" please input new Chinese "<<"; cin >> nCHINESE (NCHINESE); BREAK; } break; case 0: exit (0);}}} this program debugging environment in linux source codes follow the GPL References: C language data structure of << >> description of: William Ford William Topp the Tsinghua University. the first edition of the University community << data structure (C language) >> author: Huang Guoyu Yenai Jing Tsinghua University Press, please correct me

转载请注明原文地址:https://www.9cbs.com/read-26969.html

New Post(0)