数据结构大型实验的记录(done)
用平衡二叉树的知识实现用户登录系统模拟
基本思路:
类: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_)
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 26 void displayTree(int maxCharacters); 27 void deleteShadowTree(tnodeShadow *t); 28 AVLnode *insert(const string &item); 29 void remove(const string &item); 30 AVLnode *search(const string &item) const; 31 AVLnode *getRoot(); 32 33 private: 34 AVLnode *root; // pointer to tree root 35 int treeSize; // number of elements in the tree 36 AVLnode *creatAVLNode(const string item, AVLnode *lptr, AVLnode *rptr, AVLnode *pptr); 37 38 int depth(AVLnode *p) 39 { 40 int ldep=0,rdep=0,depval; 41 if(p==NULL) 42 depval=-1; 43 else 44 { 45 ldep=depth(p->left); 46 rdep=depth(p->right); 47 depval=1+(ldep>rdep?ldep:rdep); 48 } 49 return depval; 50 }; 51 52 int balanceFactor(AVLnode *cur) 53 { 54 int ldep=0,rdep=0; 55 AVLnode *p=cur; 56 ldep=depth(p->left); 57 rdep=depth(p->right); 58 return (rdep-ldep); 59 }; 60 61 void rightRotate(AVLnode *cur) 62 { 63 //cur is the middle one 64 if(cur->right!=NULL) 65 { 66 cur->parent->left=cur->right; 67 cur->right->parent=cur->parent; 68 } 69 else cur->parent->left=NULL; 70 71 if(cur->parent->parent==NULL) 72 { 73 cur->parent->parent=cur; 74 cur->right=cur->parent; 75 cur->parent=NULL; 76 root=cur; 77 } 78 else 79 { 80 AVLnode *pr=cur->parent->parent; 81 cur->right=cur->parent; 82 cur->parent->parent=cur; 83 84 cur->parent=pr; 85 if(cur->nodeValue>pr->nodeValue) 86 pr->right=cur; 87 else pr->left=cur; 88 } 89 }; 90 91 void leftRotate(AVLnode *cur) 92 { 93 //cur is the middle one 94 if(cur->left!=NULL) 95 { 96 cur->parent->right=cur->left; 97 cur->left->parent=cur->parent; 98 } 99 else cur->parent->right=NULL; 100 101 if(cur->parent->parent==NULL) 102 { 103 cur->parent->parent=cur; 104 cur->left=cur->parent; 105 cur->parent=NULL; 106 root=cur; 107 } 108 else 109 { 110 AVLnode *pr=cur->parent->parent; 111 cur->left=cur->parent; 112 cur->parent->parent=cur; 113 114 cur->parent=pr; 115 if(cur->nodeValue>pr->nodeValue) 116 pr->right=cur; 117 else pr->left=cur; 118 } 119 }; 120 121 void leftrightRotate(AVLnode *cur) 122 { 123 //cur is the third one 124 //全空 125 if(cur->left==NULL&&cur->right==NULL) 126 { 127 cur->parent->right=NULL; 128 cur->parent->parent->left=NULL; 129 } 130 //一边空 另一边最多一个 131 else if(cur->right==NULL) 132 { 133 cur->left->parent=cur->parent; 134 cur->parent->right=cur->left; 135 cur->parent->parent->left=NULL; 136 } 137 else if(cur->left==NULL) 138 { 139 cur->right->parent=cur->parent->parent; 140 cur->parent->parent->left=cur->right; 141 cur->parent->right=NULL; 142 } 143 //非空 挂在一边 另一边最多只有一个元素 144 else 145 { 146 cur->left->parent=cur->parent; 147 cur->parent->right=cur->left; 148 149 cur->right->parent=cur->parent->parent; 150 cur->parent->parent->left=cur->right; 151 152 } 153 AVLnode *pr=cur->parent->parent->parent; 154 155 cur->right=cur->parent->parent; 156 cur->parent->parent->parent=cur; 157 158 cur->parent->parent=cur; 159 cur->left=cur->parent; 160 161 if(pr!=NULL) 162 { 163 cur->parent=pr; 164 if(cur->nodeValue<pr->nodeValue) 165 pr->left=cur; 166 else pr->right=cur; 167 } 168 else 169 { 170 cur->parent=NULL; 171 root=cur; 172 } 173 }; 174 175 void rightleftRotate(AVLnode *cur) 176 { 177 //cur is the third one 178 //全空 179 if(cur->left==NULL&&cur->right==NULL) 180 { 181 cur->parent->left=NULL; 182 cur->parent->parent->right=NULL; 183 } 184 //一边空 另一边最多一个 185 else if(cur->left==NULL) 186 { 187 cur->right->parent=cur->parent; 188 cur->parent->left=cur->right; 189 cur->parent->parent->right=NULL; 190 } 191 else if(cur->right==NULL) 192 { 193 cur->left->parent=cur->parent->parent; 194 cur->parent->parent->right=cur->left; 195 cur->parent->left=NULL; 196 } 197 //非空 挂在一边 另一边最多只有一个元素 198 else 199 { 200 cur->right->parent=cur->parent; 201 cur->parent->left=cur->right; 202 203 cur->left->parent=cur->parent->parent; 204 cur->parent->parent->right=cur->left; 205 206 } 207 AVLnode *pr=cur->parent->parent->parent; 208 209 cur->left=cur->parent->parent; 210 cur->parent->parent->parent=cur; 211 212 cur->parent->parent=cur; 213 cur->right=cur->parent; 214 215 if(pr!=NULL) 216 { 217 cur->parent=pr; 218 if(cur->nodeValue<pr->nodeValue) 219 pr->left=cur; 220 else pr->right=cur; 221 } 222 else 223 { 224 cur->parent=NULL; 225 root=cur; 226 } 227 } 228 229 230 }; 231 232 #endif // !defined(AFX_AVLTREE_H__36B69688_6A87_4949_A008_13138B14D185__INCLUDED_)