用平衡二叉树的知识实现用户登录系统模拟

基本思路:

  类:AVLnode (树的节点类)

    AVLtree (树的基本操作类 包括insert remove search 平衡树的4种旋转)

    UserInfo(用户信息类)

决定还是按日期更新 这样更完整一点

12-15

昨天写到12点多,基本写完了AVL类,删除部分还有待完善,之后可能要补充平衡过程。

昨天写完类后想到具体的用户交互过程,想到一个节点里不仅要存用户名还要存密码,然后之前写类的时候一直是当一个nodeValue来写的,写到头昏脑胀有点晕以为要重新写一遍把nodeValue更新到两个了。今天想到了很简单的解决办法,只要在AVLnode类里加一个string password然后在每次insert完后返回insert的节点,再用节点指向它的password就可以了。

初步做了交互界面。

不是数据的问题,insert方法本身就有问题 可能不只是旋转上的错误

apple fhy1118
Apple 1110111
abc ABC4C
Aabc1 **2
cat 890
but happy
flower flower
trees 5678910
flowst 9087
but1 000000
flowar 080808

12-16

尼玛挑了半天旋转 本来没问题的都快被我调成有问题的了。格式也超优化 结果发现是insert里面p往上遍历写错

for(int i=0;i<count-2;i++)

  p=newnode->parent;

发现的时候心中真的千万匹草泥马开始奔腾啊!!!!!!

但是成功构建以后很爽!!!!!!!

这尼玛大概就是程序猿的痛并快乐着了吧!!!!!!!!!!

贴上完整代码!!!

  1. // AVLnode.h: interface for the AVLnode class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #if !defined(AFX_AVLNODE_H__C8E651E6_0EA9_4808_848B_0CB927923FAE__INCLUDED_)
  6. #define AFX_AVLNODE_H__C8E651E6_0EA9_4808_848B_0CB927923FAE__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11. #include <stddef.h>
  12. #include <string>
  13. using namespace std;
  14.  
  15. class AVLnode
  16. {
  17. public:
  18. string nodeValue;// node data
  19. string password;
  20. AVLnode *left, *right, *parent; // child pointers and pointer to the node's parent
  21. AVLnode (const string item, AVLnode *lptr=NULL, AVLnode *rptr=NULL, AVLnode *pptr=NULL):
  22. nodeValue(item), left(lptr), right(rptr), parent(pptr)
  23. {}
  24. public:
  25. AVLnode(){password="";};
  26. virtual ~AVLnode(){};
  27.  
  28. };
  29.  
  30. #endif // !defined(AFX_AVLNODE_H__C8E651E6_0EA9_4808_848B_0CB927923FAE__INCLUDED_)

AVLnode.h

  1. // AVLtree.h: interface for the AVLtree class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #if !defined(AFX_AVLTREE_H__36B69688_6A87_4949_A008_13138B14D185__INCLUDED_)
  6. #define AFX_AVLTREE_H__36B69688_6A87_4949_A008_13138B14D185__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11.  
  12. #include "AVLnode.h"
  13. #include "tnodeShadow.h"
  14. #include <iostream>
  15. #include <iomanip>
  16. using namespace std;
  17.  
  18. class AVLtree
  19. {
  20. public:
  21. AVLtree(); // constructor. initialize root to NULL and size to 0
  22. ~AVLtree(); // destructor
  23. tnodeShadow *buildShadowTree(AVLnode *t, int level, int& column);
  24.  
  25. void displayTree(int maxCharacters);
  26. void deleteShadowTree(tnodeShadow *t);
  27. AVLnode *insert(const string &item);
  28. void remove(const string &item);
  29. AVLnode *search(const string &item) const;
  30. AVLnode *getRoot();
  31.  
  32. private:
  33. AVLnode *root; // pointer to tree root
  34. int treeSize; // number of elements in the tree
  35. AVLnode *creatAVLNode(const string item, AVLnode *lptr, AVLnode *rptr, AVLnode *pptr);
  36.  
  37. int depth(AVLnode *p)
  38. {
  39. int ldep=,rdep=,depval;
  40. if(p==NULL)
  41. depval=-;
  42. else
  43. {
  44. ldep=depth(p->left);
  45. rdep=depth(p->right);
  46. depval=+(ldep>rdep?ldep:rdep);
  47. }
  48. return depval;
  49. };
  50.  
  51. int balanceFactor(AVLnode *cur)
  52. {
  53. int ldep=,rdep=;
  54. AVLnode *p=cur;
  55. ldep=depth(p->left);
  56. rdep=depth(p->right);
  57. return (rdep-ldep);
  58. };
  59.  
  60. void rightRotate(AVLnode *cur)
  61. {
  62. //cur is the middle one
  63. if(cur->right!=NULL)
  64. {
  65. cur->parent->left=cur->right;
  66. cur->right->parent=cur->parent;
  67. }
  68. else cur->parent->left=NULL;
  69.  
  70. if(cur->parent->parent==NULL)
  71. {
  72. cur->parent->parent=cur;
  73. cur->right=cur->parent;
  74. cur->parent=NULL;
  75. root=cur;
  76. }
  77. else
  78. {
  79. AVLnode *pr=cur->parent->parent;
  80. cur->right=cur->parent;
  81. cur->parent->parent=cur;
  82.  
  83. cur->parent=pr;
  84. if(cur->nodeValue>pr->nodeValue)
  85. pr->right=cur;
  86. else pr->left=cur;
  87. }
  88. };
  89.  
  90. void leftRotate(AVLnode *cur)
  91. {
  92. //cur is the middle one
  93. if(cur->left!=NULL)
  94. {
  95. cur->parent->right=cur->left;
  96. cur->left->parent=cur->parent;
  97. }
  98. else cur->parent->right=NULL;
  99.  
  100. if(cur->parent->parent==NULL)
  101. {
  102. cur->parent->parent=cur;
  103. cur->left=cur->parent;
  104. cur->parent=NULL;
  105. root=cur;
  106. }
  107. else
  108. {
  109. AVLnode *pr=cur->parent->parent;
  110. cur->left=cur->parent;
  111. cur->parent->parent=cur;
  112.  
  113. cur->parent=pr;
  114. if(cur->nodeValue>pr->nodeValue)
  115. pr->right=cur;
  116. else pr->left=cur;
  117. }
  118. };
  119.  
  120. void leftrightRotate(AVLnode *cur)
  121. {
  122. //cur is the third one
  123. //全空
  124. if(cur->left==NULL&&cur->right==NULL)
  125. {
  126. cur->parent->right=NULL;
  127. cur->parent->parent->left=NULL;
  128. }
  129. //一边空 另一边最多一个
  130. else if(cur->right==NULL)
  131. {
  132. cur->left->parent=cur->parent;
  133. cur->parent->right=cur->left;
  134. cur->parent->parent->left=NULL;
  135. }
  136. else if(cur->left==NULL)
  137. {
  138. cur->right->parent=cur->parent->parent;
  139. cur->parent->parent->left=cur->right;
  140. cur->parent->right=NULL;
  141. }
  142. //非空 挂在一边 另一边最多只有一个元素
  143. else
  144. {
  145. cur->left->parent=cur->parent;
  146. cur->parent->right=cur->left;
  147.  
  148. cur->right->parent=cur->parent->parent;
  149. cur->parent->parent->left=cur->right;
  150.  
  151. }
  152. AVLnode *pr=cur->parent->parent->parent;
  153.  
  154. cur->right=cur->parent->parent;
  155. cur->parent->parent->parent=cur;
  156.  
  157. cur->parent->parent=cur;
  158. cur->left=cur->parent;
  159.  
  160. if(pr!=NULL)
  161. {
  162. cur->parent=pr;
  163. if(cur->nodeValue<pr->nodeValue)
  164. pr->left=cur;
  165. else pr->right=cur;
  166. }
  167. else
  168. {
  169. cur->parent=NULL;
  170. root=cur;
  171. }
  172. };
  173.  
  174. void rightleftRotate(AVLnode *cur)
  175. {
  176. //cur is the third one
  177. //全空
  178. if(cur->left==NULL&&cur->right==NULL)
  179. {
  180. cur->parent->left=NULL;
  181. cur->parent->parent->right=NULL;
  182. }
  183. //一边空 另一边最多一个
  184. else if(cur->left==NULL)
  185. {
  186. cur->right->parent=cur->parent;
  187. cur->parent->left=cur->right;
  188. cur->parent->parent->right=NULL;
  189. }
  190. else if(cur->right==NULL)
  191. {
  192. cur->left->parent=cur->parent->parent;
  193. cur->parent->parent->right=cur->left;
  194. cur->parent->left=NULL;
  195. }
  196. //非空 挂在一边 另一边最多只有一个元素
  197. else
  198. {
  199. cur->right->parent=cur->parent;
  200. cur->parent->left=cur->right;
  201.  
  202. cur->left->parent=cur->parent->parent;
  203. cur->parent->parent->right=cur->left;
  204.  
  205. }
  206. AVLnode *pr=cur->parent->parent->parent;
  207.  
  208. cur->left=cur->parent->parent;
  209. cur->parent->parent->parent=cur;
  210.  
  211. cur->parent->parent=cur;
  212. cur->right=cur->parent;
  213.  
  214. if(pr!=NULL)
  215. {
  216. cur->parent=pr;
  217. if(cur->nodeValue<pr->nodeValue)
  218. pr->left=cur;
  219. else pr->right=cur;
  220. }
  221. else
  222. {
  223. cur->parent=NULL;
  224. root=cur;
  225. }
  226. }
  227.  
  228. };
  229.  
  230. #endif // !defined(AFX_AVLTREE_H__36B69688_6A87_4949_A008_13138B14D185__INCLUDED_)

AVLtree.h

  1. // menu.h: interface for the menu class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #if !defined(AFX_MENU_H__A58647F3_F271_4C38_8097_A38DF9CA38CF__INCLUDED_)
  6. #define AFX_MENU_H__A58647F3_F271_4C38_8097_A38DF9CA38CF__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11. #include "AVLtree.h"
  12. #include <fstream>
  13. #include <stack>
  14. using namespace std;
  15.  
  16. class menu
  17. {
  18. public:
  19. menu();
  20. virtual ~menu();
  21. void select();
  22. void menuInsert();
  23. void menuLogin();
  24. void menulogsuccess(AVLnode *login);
  25. void menuChange(AVLnode *login);
  26. void menuDelete(AVLnode *login);
  27. void menuexit();
  28.  
  29. private:
  30. AVLtree tree;
  31. };
  32.  
  33. #endif // !defined(AFX_MENU_H__A58647F3_F271_4C38_8097_A38DF9CA38CF__INCLUDED_)

menu.h

  1. // menu.cpp: implementation of the menu class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "menu.h"
  6.  
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10.  
  11. menu::menu()
  12. {
  13. ifstream f;
  14. f.open("1.txt");
  15. string id,pass;
  16. while(!f.eof())
  17. {
  18. f>>id>>pass;
  19. AVLnode *t=tree.insert(id);
  20. t->password=pass;
  21. }
  22. f.close();
  23. }
  24.  
  25. menu::~menu()
  26. {
  27.  
  28. }
  29. void menu::select()
  30. {
  31. cout<<"--------------------------------------------------------------------------------\n";
  32. cout<<" welcome \n";
  33. cout<<"--------------------------------------------------------------------------------\n";
  34. cout<<"--------------------------------------------------------------------------------\n";
  35. cout<<" 1.注册账号 \n\n";
  36. cout<<" 2.登录账号 \n\n";
  37. cout<<" 3.退 出 \n\n";
  38. cout<<"--------------------------------------------------------------------------------\n";
  39. cout<<"输入数字进行选择:";
  40. int choice;
  41. cin>>choice;
  42. if(choice==)
  43. menuInsert();
  44. if(choice==)
  45. menuLogin();
  46. else
  47. menuexit();
  48. }
  49.  
  50. void menu::menuInsert()
  51. {
  52. cout<<"--------------------------------------------------------------------------------\n";
  53. cout<<" 注册账号 \n";
  54. cout<<"--------------------------------------------------------------------------------\n";
  55. bool exist=true;
  56. string userId;
  57. string npassword;
  58. while(exist)
  59. {
  60. cout<<"请输入账号: ";
  61. cin>>userId;
  62.  
  63. AVLnode *judge=tree.search(userId);
  64. if(judge==NULL) exist=false;
  65. else cout<<"您输入的用户名已存在!\n";
  66. }
  67. AVLnode *cur=tree.insert(userId);
  68.  
  69. cout<<"请输入密码: ";
  70. cin>>npassword;
  71. cur->password=npassword;
  72. menuexit(); //更新注册后的用户信息 否则删除出错。
  73. cout<<"--------------------------------------------------------------------------------\n";
  74. cout<<" 注册成功! \n";
  75. cout<<"--------------------------------------------------------------------------------\n";
  76. cout<<"按1键返回上层...";
  77. int num;
  78. cin>>num;
  79. if(num==)
  80. select();
  81. else
  82. menuexit();
  83. }
  84.  
  85. void menu::menuLogin()
  86. {
  87. cout<<"--------------------------------------------------------------------------------\n";
  88. cout<<" 登录账号 \n";
  89. cout<<"--------------------------------------------------------------------------------\n";
  90. string userId;
  91. string npassword;
  92. cout<<"请输入账号: ";
  93. cin>>userId;
  94. cout<<"请输入密码: ";
  95. cin>>npassword;
  96. AVLnode *login=tree.search(userId);
  97. if(login==NULL)
  98. {
  99. cout<<"--------------------------------------------------------------------------------\n";
  100. cout<<" 您输入的账号不存在! \n";
  101. cout<<"--------------------------------------------------------------------------------\n";
  102. cout<<"按下1键重新输入...\n";
  103. int num;
  104. cin>>num;
  105. if(num==)
  106. menuLogin();
  107. else
  108. menuexit();
  109. }
  110. else
  111. {
  112. if(login->password==npassword)
  113. menulogsuccess(login);
  114. else
  115. {
  116. cout<<"--------------------------------------------------------------------------------\n";
  117. cout<<" 密码错误! \n";
  118. cout<<"--------------------------------------------------------------------------------\n";
  119. cout<<"按下1键重新输入...";
  120. int num;
  121. cin>>num;
  122. if(num==)
  123. menuLogin();
  124. else
  125. menuexit();
  126. }
  127. }
  128. }
  129.  
  130. void menu::menulogsuccess(AVLnode *login)
  131. {
  132. cout<<"--------------------------------------------------------------------------------\n";
  133. cout<<" 登录成功! \n";
  134. cout<<"--------------------------------------------------------------------------------\n";
  135. cout<<"--------------------------------------------------------------------------------\n";
  136. cout<<" 1.修改密码 \n\n";
  137. cout<<" 2.删除账号 \n\n";
  138. cout<<" 3.回主菜单 \n\n";
  139. cout<<"--------------------------------------------------------------------------------\n";
  140. cout<<"输入数字进行选择:";
  141. int choice;
  142. cin>>choice;
  143. if(choice==) select();
  144. if(choice==)
  145. menuChange(login);
  146. if(choice==)
  147. menuDelete(login);
  148. else
  149. menuexit();
  150. }
  151.  
  152. void menu::menuChange(AVLnode *login)
  153. {
  154. cout<<"请输入新密码: ";
  155. string npassword;
  156. cin>>npassword;
  157.  
  158. cout<<"请再次输入新密码: ";
  159. string check;
  160. cin>>check;
  161.  
  162. if(check==npassword)
  163. {
  164. login->password=npassword;
  165. cout<<"--------------------------------------------------------------------------------\n";
  166. cout<<" 修改成功! \n";
  167. cout<<"--------------------------------------------------------------------------------\n";
  168. cout<<"按下1键返回登录...";
  169. int num;
  170. cin>>num;
  171. if(num==)
  172. menuLogin();
  173. else
  174. menuexit();
  175. }
  176. else
  177. {
  178. cout<<"--------------------------------------------------------------------------------\n";
  179. cout<<" 两次输入不一致! \n";
  180. cout<<"--------------------------------------------------------------------------------\n";
  181. cout<<"按下1键返回修改 2键返回登录界面...";
  182. int num;
  183. cin>>num;
  184. if(num==)
  185. menuChange(login);
  186. else if(num==)
  187. menulogsuccess(login);
  188. else menuexit();
  189. }
  190. }
  191.  
  192. void menu::menuDelete(AVLnode *login)
  193. {
  194. cout<<"--------------------------------------------------------------------------------\n";
  195. cout<<" 确认删除? \n";
  196. cout<<"--------------------------------------------------------------------------------\n";
  197. cout<<"按下1键确认 2键返回初始界面...";
  198. int num;
  199. cin>>num;
  200. if(num==)
  201. {
  202. tree.remove(login->nodeValue);
  203. cout<<"--------------------------------------------------------------------------------\n";
  204. cout<<" 删除成功! \n";
  205. cout<<"--------------------------------------------------------------------------------\n";
  206. cout<<"按下1键返回初始界面...";
  207. int n;
  208. cin>>n;
  209. if(n==)
  210. select();
  211. else
  212. menuexit();
  213. }
  214. else if(num==)
  215. select();
  216. else menuexit();
  217. }
  218.  
  219. void menu::menuexit()
  220. {
  221. ofstream f;
  222. f.open("1.txt");
  223. string id,pass;
  224.  
  225. stack<AVLnode*> s;
  226. AVLnode *p=tree.getRoot();
  227. while(p!=NULL||!s.empty())
  228. {
  229. while(p!=NULL)
  230. {
  231. s.push(p);
  232. p=p->left;
  233. }
  234. if(!s.empty())
  235. {
  236. p=s.top();
  237. id=p->nodeValue;
  238. pass=p->password;
  239. f<<id<<" "<<pass<<"\n";
  240.  
  241. s.pop();
  242. p=p->right;
  243. }
  244. }
  245. f.flush();
  246. f.close();
  247.  
  248. }

menu.cpp

  1. #include "AVLnode.h"
  2. #include "AVLtree.h"
  3. #include "menu.h"
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8.  
  9. menu guest;
  10. guest.select();
  11. return ;
  12. }

main.cpp

  1. // AVLtree.cpp: implementation of the AVLtree class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "AVLtree.h"
  6.  
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10.  
  11. AVLtree::AVLtree()
  12. {
  13. root=NULL;
  14. treeSize=;
  15. }
  16.  
  17. AVLtree::~AVLtree()
  18. {
  19. }
  20. AVLnode *AVLtree::getRoot()
  21. {
  22. return root;
  23. }
  24.  
  25. AVLnode *AVLtree::creatAVLNode (const string item, AVLnode *lptr, AVLnode *rptr, AVLnode *pptr)
  26. {
  27. AVLnode *newNode;
  28. // initialize the data and all pointers
  29. newNode = new AVLnode (item, lptr, rptr, pptr);
  30. return newNode;
  31. }
  32.  
  33. void AVLtree::displayTree(int maxCharacters)
  34. {
  35. string label;
  36. int level = , column = ;
  37. int colWidth = maxCharacters + ;
  38.  
  39. int currLevel = , currCol = ;
  40.  
  41. if (treeSize == )
  42. return;
  43.  
  44. tnodeShadow *shadowRoot = buildShadowTree(root, level, column);
  45.  
  46. tnodeShadow *currNode;
  47.  
  48. queue<tnodeShadow *> q;
  49.  
  50. q.push(shadowRoot);
  51.  
  52. while(!q.empty())
  53. {
  54. currNode = q.front();
  55. q.pop();
  56.  
  57. if (currNode->level > currLevel)
  58. {
  59. currLevel = currNode->level;
  60. currCol = ;
  61. cout << endl;
  62. }
  63.  
  64. if(currNode->left != NULL)
  65. q.push(currNode->left);
  66.  
  67. if(currNode->right != NULL)
  68. q.push(currNode->right);
  69.  
  70. if (currNode->column > currCol)
  71. {
  72. cout << setw((currNode->column-currCol)*colWidth) << " ";
  73. currCol = currNode->column;
  74. }
  75. cout << setw(colWidth) << currNode->nodeValueStr;
  76. currCol++;
  77. }
  78. cout << endl;
  79.  
  80. deleteShadowTree(shadowRoot);
  81. }
  82.  
  83. void AVLtree::deleteShadowTree(tnodeShadow *t)
  84. {
  85. if (t != NULL)
  86. {
  87. deleteShadowTree(t->left);
  88. deleteShadowTree(t->right);
  89. delete t;
  90. }
  91. }
  92.  
  93. AVLnode *AVLtree::insert(const string &item)
  94. {
  95. AVLnode *t=root,*newnode,*parent=NULL;
  96. while(t!=NULL)
  97. {
  98. parent=t;
  99. if(item==t->nodeValue)
  100. return NULL;
  101. else if(item<t->nodeValue)
  102. t=t->left;
  103. else
  104. t=t->right;
  105. }
  106. newnode=creatAVLNode(item,NULL,NULL,parent);
  107. if(parent==NULL)
  108. root=newnode;
  109. else if(item>parent->nodeValue)
  110. parent->right=newnode;
  111. else
  112. parent->left=newnode;
  113. treeSize++;
  114.  
  115. int bf=,count=;
  116. AVLnode *cur=newnode,*p=newnode;
  117. while(bf>=-&&bf<=&&cur!=root)
  118. {
  119. cur=cur->parent;
  120. count++;
  121. bf=balanceFactor(cur);
  122. }
  123.  
  124. if(bf<-||bf>)
  125. {
  126. if(count==) ;
  127. else
  128. {
  129. for(int i=;i<count-;i++)
  130. p=p->parent;
  131. }
  132. //all left
  133. if(p->nodeValue<p->parent->nodeValue&&p->parent->nodeValue<cur->nodeValue)
  134. rightRotate(p->parent);
  135.  
  136. //all right
  137. else if(p->nodeValue>p->parent->nodeValue&&p->parent->nodeValue>cur->nodeValue)
  138. leftRotate(p->parent);
  139.  
  140. //right left
  141. else if(p->nodeValue<p->parent->nodeValue&&p->parent->nodeValue>cur->nodeValue)
  142. rightleftRotate(p);
  143. //left right
  144. else if(p->nodeValue>p->parent->nodeValue&&p->parent->nodeValue<cur->nodeValue)
  145. leftrightRotate(p);
  146. }
  147. return newnode;
  148. }
  149.  
  150. void AVLtree::remove(const string &item)
  151. {
  152. AVLnode *t=search(item);
  153. if(t==NULL) return;
  154.  
  155. //leaf node
  156. else if(t->left==NULL&&t->right==NULL)
  157. {
  158. if(t==root)
  159. root=NULL;
  160. else if(t->nodeValue>t->parent->nodeValue)
  161. t->parent->right=NULL;
  162. else
  163. t->parent->left=NULL;
  164. }
  165. //only left or right
  166. else if(t->left==NULL)
  167. {
  168. if(t==root)
  169. {
  170. t->right->parent=NULL;
  171. root=t->right;
  172. }
  173. else if(t->nodeValue>t->parent->nodeValue)
  174. t->parent->right=t->right;
  175. else
  176. t->parent->left=t->right;
  177. t->right->parent=t->parent;
  178. }
  179. else if(t->right==NULL)
  180. {
  181. if(t==root)
  182. {
  183. t->left->parent=NULL;
  184. root=t->left;
  185. }
  186. else if(t->nodeValue>t->parent->nodeValue)
  187. t->parent->right=t->left;
  188. else
  189. t->parent->left=t->left;
  190. t->left->parent=t->parent;
  191. }
  192. //left && right
  193. else
  194. {
  195. AVLnode *r=t;
  196. if(t==root)
  197. {
  198. r=r->right;
  199. while(r->left!=NULL)
  200. r=r->left;
  201.  
  202. r->left=t->left;
  203. t->left->parent=r;
  204. if(r->right==NULL&&r->parent!=root)
  205. {
  206. r->right=t->right;
  207. t->right->parent=r;
  208. r->parent->left=NULL;
  209. }
  210. else if(r->parent!=root)
  211. {
  212. r->parent->left=r->right;
  213. r->right->parent=r->parent;
  214. r->right=t->right;
  215. t->right->parent=r;
  216. }
  217.  
  218. r->parent=NULL;
  219. root=r;
  220.  
  221. }
  222.  
  223. else if(t->nodeValue>t->parent->nodeValue)
  224. {
  225. r=r->right;
  226. while(r->left!=NULL)
  227. r=r->left;
  228.  
  229. if(r->parent!=t)
  230. {
  231. if(r->right==NULL)
  232. r->parent->left=NULL;
  233. else
  234. {
  235. r->right->parent=r->parent;
  236. r->parent->left=r->right;
  237. }
  238.  
  239. r->right=t->right;
  240. t->right->parent=r;
  241.  
  242. r->parent=t->parent;
  243. t->parent->right=r;
  244. r->left=t->left;
  245. t->left->parent=r;
  246. }
  247. else
  248. {
  249. r->parent=t->parent;
  250. t->parent->right=r;
  251. r->left=t->left;
  252. t->left->parent=r;
  253. }
  254.  
  255. }
  256. else
  257. {
  258. r=r->right;
  259. while(r->left!=NULL)
  260. r=r->left;
  261.  
  262. if(r->parent!=t)
  263. {
  264. if(r->right==NULL)
  265. r->parent->left=NULL;
  266. else
  267. {
  268. r->right->parent=r->parent;
  269. r->parent->left=r->right;
  270. }
  271.  
  272. r->right=t->right;
  273. t->right->parent=r;
  274.  
  275. r->parent=t->parent;
  276. t->parent->left=r;
  277. r->left=t->left;
  278. t->left->parent=r;
  279. }
  280. else
  281. {
  282. r->parent=t->parent;
  283. t->parent->left=r;
  284. r->left=t->left;
  285. t->left->parent=r;
  286. }
  287.  
  288. }
  289. }
  290. treeSize--;
  291. delete t;
  292. //平衡部分
  293.  
  294. }
  295.  
  296. AVLnode *AVLtree::search(const string &item) const
  297. {
  298. AVLnode *t=root;
  299. while(t!=NULL)
  300. {
  301. if(t->nodeValue==item) return t;
  302. else if(item<t->nodeValue)
  303. t=t->left;
  304. else t=t->right;
  305.  
  306. }
  307. return NULL;
  308. }
  309.  
  310. tnodeShadow *AVLtree::buildShadowTree(AVLnode *t, int level, int& column)
  311. {
  312. tnodeShadow *newNode = NULL;
  313. char text[];
  314. ostrstream ostr(text,);
  315.  
  316. if (t != NULL)
  317. {
  318. newNode = new tnodeShadow;
  319.  
  320. tnodeShadow *newLeft = buildShadowTree(t->left, level+, column);
  321. newNode->left = newLeft;
  322.  
  323. ostr << t->nodeValue << ends;
  324. newNode->nodeValueStr = text;
  325. newNode->level = level;
  326. newNode->column = column;
  327.  
  328. column++;
  329.  
  330. tnodeShadow *newRight = buildShadowTree(t->right, level+, column);
  331. newNode->right = newRight;
  332. }
  333.  
  334. return newNode;
  335. }

AVLtree.cpp

附调试时用的结构化打印树的函数

  1. // tnodeShadow.h: interface for the tnodeShadow class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #if !defined(AFX_TNODESHADOW_H__18034BAE_051F_4D57_91FF_BE10AFD37CDA__INCLUDED_)
  6. #define AFX_TNODESHADOW_H__18034BAE_051F_4D57_91FF_BE10AFD37CDA__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11.  
  12. #include <iomanip> // for setw()
  13. #include <strstream> // for format conversion
  14. #include <string> // node data formatted as a string
  15. #include <queue>
  16. #include <utility>
  17.  
  18. using namespace std;
  19.  
  20. class tnodeShadow
  21. {
  22. public:
  23. string nodeValueStr; // formatted node value
  24. int level,column;
  25. tnodeShadow *left, *right;
  26.  
  27. tnodeShadow ()
  28. {}
  29. };
  30. #endif // !defined(AFX_TNODESHADOW_H__18034BAE_051F_4D57_91FF_BE10AFD37CDA__INCLUDED_)

tnodeShadow.h

第一次写这么完整的程序 运行的一刻真的好爽!

数据结构大型实验的记录(done)的更多相关文章

  1. 20172328《程序设计与数据结构》实验三 敏捷开发与XP实践报告

    20172328<程序设计与数据结构>实验三 敏捷开发与XP实践报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 李馨雨 学号:20172328 实验教师:王志强 ...

  2. 20172310 2017-2018-2 《程序设计与数据结构》实验三报告(敏捷开发与XP实践)

    20172310 2017-2018-2 <程序设计与数据结构>实验三报告(敏捷开发与XP实践) 课程:<程序设计与数据结构> 班级: 1723 姓名: 仇夏 学号:20172 ...

  3. 20172301 《Java软件结构与数据结构》实验三报告

    20172301 <Java软件结构与数据结构>实验三报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 郭恺 学号:20172301 实验教师:王志强老师 ...

  4. 20172328《程序设计与数据结构》实验四 Android程序设计报告

    20172328<程序设计与数据结构>实验四 Android程序设计报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 李馨雨 学号:20172328 实验教师:王志 ...

  5. 20172301 《Java软件结构与数据结构》实验二报告

    20172301 <Java软件结构与数据结构>实验二报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 郭恺 学号:20172301 实验教师:王志强老师 ...

  6. 20172329 2018-2019 《Java软件结构与数据结构》实验三报告

    20172329 2018-2019-2 <Java软件结构与数据结构>实验三报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 王文彬 学号:2017232 ...

  7. 20172329 2018-2019-2 《Java软件结构与数据结构》实验二报告

    20172329 2018-2019-2 <Java软件结构与数据结构>实验二报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 王文彬 学号:2017232 ...

  8. 20172301 2017-2018-2 《程序设计与数据结构》实验一《Java开发环境的熟悉》实验报告

    20172301 2017-2018-2 <程序设计与数据结构>实验一<Java开发环境的熟悉>实验报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 郭 ...

  9. 20172301 《Java软件结构与数据结构》实验一报告

    20172301 <Java软件结构与数据结构>实验一报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 郭恺 学号:20172301 实验教师:王志强老师 ...

随机推荐

  1. iOS:原生二维码扫描

    做iOS的二维码扫描,有两个第三方库可以选择,ZBar和ZXing.今天要介绍的是iOS7.0后AVFoundation框架提供的原生二维码扫描. 首先需要添加AVFoundation.framewo ...

  2. mapreduce 关于小文件导致任务缓慢的问题

    小文件导致任务执行缓慢的原因: 1.很容易想到的是map task 任务启动太多,而每个文件的实际输入量很小,所以导致了任务缓慢 这个可以通过 CombineTextInputFormat,解决,主要 ...

  3. centos6.5安装vsftpd

    开通FTP有gssftp和vsftpd二种,查了查,据说vsftpd更稳定和更安全.就用vsftpd吧. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序.特点是小 ...

  4. [LeetCode]题解(python):087-Scramble String

    题目来源: https://leetcode.com/problems/scramble-string/ 题意分析: 给定一个字符串,字符串展成一个二叉树,如果二叉树某个或多个左右子树颠倒得到的新字符 ...

  5. 关于document.selection和TextRange对象的介绍

    document.selection只有IE支持 window.getSelection()也只有FireFox和Safari支持,都不是标准语法. selection 对象代表了当前激活选中区,即高 ...

  6. python OptionParser模块

    Python中强大的选项处理模块. #!/usr/bin/python from optparse import OptionParser parser = OptionParser() parser ...

  7. HDU 2108 Shape of HDU

    题解:按照输入顺序依次将点连接起来,对于连续的三个点p0,p1,p2,令向量a=p1-p0,b=p2-p1 若是凸多边形,那么b相对于a一定是向逆时针方向旋转的 判断两向量的旋转方向,可以使用向量的叉 ...

  8. Sicily-1028

    一.        题意: 算出汉诺塔移动序列中对应位置的号码,数据规模很大,所以不能单纯递归,而是要找出汉诺塔序列的规律. 二.        汉诺塔数列 为了得出最少的移动步数,当n为偶数时,最上 ...

  9. Curling 2.0(dfs回溯)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15567   Accepted: 6434 Desc ...

  10. 1148 - Mad Counting(数学)

    1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...