
درخت جست و جوی دودویی Binary search tree
آپدیت : در قسمت اول کد و الگوریتم درخت جست و جوی دودویی را که با سی پلاس پلاس نوشته ام می بینید و در قسمت دوم کد درخت BST را با زبان جاوا که آقای Farhang Amary برایمان فرستاده اند را خواهید دید.
درخت جست و جوی دو دویی – Binary search tree :
درخت جست و جوی دودویی یکی از بهترین ساختمان داده ها است و بر اساس آن می توان ساختمان داده های متعددی تولید کرد. این درخت که به آن BST هم گفته می شود دارای ویژگی ها بسیار خوبی است ازجمله سرعت در انجام عملیات های نختلف و نگه داری از آن.
درخت جست و جوی دودویی درخت است که حداکثر دو فرزند دارد و تمام نود ها در زیر درخت سمت راست یک گره برزگ تر مساوی گره است و تمام گره ها در زیر درخت سمت چپ گره کوچک تر از گره است به شکل زیر نگاه کنید :
به بالا ترین گره در درخت که فرزند هیج گره ای نیست ریشه می گویند.
برای گره ها ساختمان داده زیر را درنظر گرفته ام :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class node { public: int data; node* parent; node* leftChild; node* rightChild; node(void); }; node::node(void) { data = 0; parent = NULL; leftChild = NULL; rightChild = NULL; } |
که دارای ویژگی های زیر است :
data : دیتا که همان مقدار یا کلید گره است و باید توجه داسته باشید که مقداری منحصر به فرد است یعنی دو گره یک مقدار ندارند ،البنه با کمی تغییر کد می توانید این مشکل را حل کنید.
parent: که اشارگری به کلاس node است و به پدر یک گره اشاره می کند ، تمام گره های پدر دارند بجز ریشه که نال است.
leftChild: اشارگری به شی از کلاس node است که به بچه ی سمت چپ یک گره اشاره می کند و در صورتی که بچه سمت چپ وجود نداشت مقدار نال را دارد.
rightChild: اشارگری به شی از کلاس node است که به بچه ی سمت راست یک گره اشاره می کند و در صورتی که بچه سمت راست وجود نداشت مقدار نال را دارد.
node : سازنده کلاس است .
در شکل زیر ساختمان داده گره ها را می بینید:
برای درخت هم کلاس و ساختمان داره زیر را در نظر گرفته ام:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //binary search tree class bst { public: node* root; bst(void); void insert(int); void display(node*, int); node* minimum(); node* maximum(); node* search(int); void inorder_tree_walk(node*); node* successor(node*); node* predecessor(node *); void transplant(node*, node*); void deletion(node *); }; |
در ادامه تمام توابع درون آن را همراه الگوریتمشان شرح خواهیم داد:
root: این کلاس فقط یک ویژگی دارد و آن ریشه درخت است.
تابع Insert :
این تابع یک مقدار را می گیرد و آن را به درخت اضافه می کند.اگر یادتان باشد گفتیم که در درخت جست و جوی دودویی گره های سمت راست یک گره بزرگ تر مساوی یک گره اند و گره های سمت سمت یک گره کوچک تر یک گره انید ما نیز برای اضافه کردن یک گره از همین قاعده استفاده می کنیم اگر درخت خالی بود آن هیچ کار خاصی نمی کنیم و گره جدید را ریشه قرار می دهیم اگر درخت خالی نبود از ریشه شروع به حرکت می کنیم . مقداری که می خواهیم به درخت اضافه کنیم اگر بزرگ تر مساوی ریشه بود به سمت راست می رویم اگر کوچک تر بود به سمت چپ می رویم و این کار را آنقدر ادامه می دهیم تا به یک جای خالی (گره ی نال) در درخت برسیم . وقتی که به جای مناسب رسیدیم اشارگر به پدر را برای گره ی جدید تنظیم می کنیم در زیر کد تا Insert را می بینید:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | void bst::insert(int d) { node* newNode = new node(); newNode->data = d; if (root == NULL) { root = newNode; } else { node* tempNode = new node(); node* backTempNode = new node(); tempNode = root; while (tempNode != NULL) { backTempNode = tempNode; if (tempNode->data <= newNode->data) { tempNode = tempNode->rightChild; } else { tempNode = tempNode->leftChild; } } newNode->parent = backTempNode; if (backTempNode->data <= newNode->data) { backTempNode->rightChild = newNode; } else { backTempNode->leftChild = newNode; } } } |
اگر ارتفاع درخت h باشد هزینه درج گره جدید برابر او h است.
تابع display :
این تابع درخت را در خروجی کنسول رسم می کند . رسم یک درخت معمولا کار سخت و زمان گیری است ولی با یه کم کد باحال می شه این کار در چند خط کد به زیبایی انجام داد :
کد تابع نمایش :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //display binary search tree void bst::display(node *Node,int level) { if (root == NULL) { cout << "Tree is empty." << endl; } else { if (Node->rightChild != NULL) { display(Node->rightChild, level + 1); } if (Node != root) { for (int i = 0; i < level + 1; i++) { cout << " "; } } else { cout << "Root->"; } cout << Node->data << endl; if (Node->leftChild != NULL) { display(Node->leftChild, level + 1); } } } |
در زیر نمونه ای از رسم درخت را می بینید :
همین طور که می بینید درخت بسیار مرت چاپ شده است بطوری که هر گره زیر پدر خود قرار دارد ، همین طور ریشه مشخص شده است.
تابع minimum :
این تابع کوچک ترین گره ی درخت را بر می گرداند.همین طور که گفتیم هر گره های سمت چپ یک گره از آن گره کوچک تر هستند، پس کوچک ترین گره سمت چپ ترین گره است پس ما آن قدر به سمت چپ می رویم تا به نال برسیم.
کد زیر که تابع مینیمم است :
1 2 3 4 5 6 7 8 9 10 11 12 | node* bst::minimum() { node* tempNode = new node(); node* backTempNode = new node(); backTempNode = tempNode = root; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->leftChild; } return backTempNode; } |
تابع maximum :
تابع ماکزیمم هم مانند تابع مینیمم است ، گره های سمت راست یک گره بزرگ تر مساوی آن گره هستند پس بزرگ ترین گره سمت راست ترین گره در درخت است و برای رسیدن به آن از ریشه شروع می کنیم و آن قدر به راست می رویم تا به هیچ (نال) برسیم.
کد تابع ماکزیمم را در زیر می بینید:
1 2 3 4 5 6 7 8 9 10 11 12 13 | //return maximum node node* bst::maximum() { node* tempNode = new node(); node* backTempNode = new node(); backTempNode = tempNode = root; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->rightChild; } return backTempNode; } |
تابع Search :
همین طور که از اسم درخت (Binary search tree) مشخص است سرچ در درخت یکی از جنبه های خیلی مهم این درخت است.فرض کنید یک گره به اسم x را در درخت می خواهیم بیابیم از ریشه شروع به مقایسه می کنیم اگر x.data برابر با ریشه بود یک اشارگر به ریشه برمی گردانیم . در صورت برابر نبود ، اگر x.data بزرگ تر مساوی مقدار ریشه بود به سمت راست می رویم
اگر کوچک تر بود به سمت چپ می رویم این کار را مرتب انجام می دهیم تا به گره مورد نظر برسیم و اگر تا برگ های (برگ به گره های آخر درخت می گویند که هیچ فرزندی ندارند) درخت رسیدم و گره مورد نظر را پیدا نکردیم هیچ یا همان نال را بر می گردانیم که مشخص می کند گره ی مورد نظر در درخت نبوده است.
کد زیر مربوط به تا بع سرچ است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //Search in BST Tree node* bst::search(int Data) { node* tempNode = new node(); tempNode = root; while (tempNode != NULL) { if (tempNode->data == Data) { return tempNode; } else { if (tempNode->data <= Data) { tempNode = tempNode->rightChild; } else { tempNode = tempNode->leftChild; } } } return NULL; } |
تابع inorder_tree_walk یا Sort ( مرتب سازی):
این تابع به صورت inorder درخت را پیمایش می کند یعنی ابتدا فرزند چپ را می بیند بعد ریشه و بعد فرزند سمت راست که این کار نود های درخت را به صورت سعودی و مرتب شده نشان می دهد.
در واقع این تابع همان Binary sort یا مرتب سازی دودویی است
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //inoreder tree walk = show all node in sorted mood void bst::inorder_tree_walk(node* currentNode) { if (root != NULL) { if (currentNode->leftChild != NULL) { inorder_tree_walk(currentNode->leftChild); } cout << currentNode->data << " ,"; if (currentNode->rightChild != NULL) { inorder_tree_walk(currentNode->rightChild); } } else { cout << "Tree is empty!" << endl; } } |
تابع successor :
successor یک گره به گره ای می گویند که اگر تمام گره های یک درخت را مرتب شده و سعودی کنار هم قرار دهیم بلافاصله بعد از گره مورد نظر ما ظاهر می شود ، این تابع در تابع حذف گره کاربرد دارد.
می توان درخت را به سورت سورت شده در آورد و بعد ساکسسور گره را پیدا کرد ولی این کار بسیار پرهزینه است . را آسان تری نیز برای این کار وجود دارد.
اگر گره ای که می خواهیم ساکسسورش را پیدا کنیم بچه ی سمت راست داشت آنگاه ساکسسور گره کوچک ترین گره در زیر درخت سمت راست گره است به شکل زیر دقت کنید:
همین طور که می بینید successor گره نود گره ی نود و پنج است که در زیر درخت سمت راست گره ی نو کوچک ترین گره است.
این روش برای زمانی بود که گره بچه ی سمت راست داشته باشد .حال اگر گره ای که می خواستیم successor آن را پیدا کنیم فرزند سمت راست نداشت یا کلا فرزند نداشت چه کنیم؟
در این موقع از پدر گره ی مورد نظر شروع می کنیم و مرتب به بالا می رویم تا زمانی این کار را می کنیم که گره ای که رویش هستیم فرزند سمت راست پدرش باشد و به نال نرسیده باشیم.
به شکل بالا نگاه کنید successor گره ی ۸۷ را می خواهیم پیدا کنیم ، از پدر ۸۷ مه گره ی ۸۵ است شروع می کنیم به بالا رفتن ، ۸۷ پسر سمت راست ۸۵ است پس مجاز است دوباره یک گره می رویم بالا و به گره ی ۹۰ می رسیم ۸۵ گره ی سمت چپ ۹۰ است پس همین جا دست از کار می کشیم ، ۹۰ ساکسسور ۸۷ است.
کد تابع successor را در زیر می بینید :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //successor node* bst::successor(node* currentNode) { node* tempNode = new node(); node* backTempNode = new node(); tempNode = currentNode; if (tempNode->rightChild != NULL) { tempNode = tempNode->rightChild; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->leftChild; } return backTempNode; } else { backTempNode = tempNode; tempNode = tempNode->parent; while (tempNode != NULL && tempNode->rightChild == backTempNode) { backTempNode = tempNode; tempNode = tempNode->parent; } return tempNode; } } |
تابع predecessor :
predecessor یک گره به گره ای می گویند که در حالت سورت شده تمام گره ها دقیقا قبل از گره ی مورد نظر می آید.
predecessor دقیقا پیدا کردنش شبیه ساکسسور است ولی هر کاری که برای ساکسسور می کردیم برعکسش را برای این تابع می کنیم.
کد تابع predecessor را در زیر می بینید :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //predecessor node* bst::predecessor(node* currentNode) { node* tempNode = new node(); node* backTempNode = new node(); tempNode = currentNode; if (tempNode->leftChild != NULL) { tempNode = tempNode->leftChild; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->rightChild; } return backTempNode; } else { backTempNode = tempNode; tempNode = tempNode->parent; while (tempNode != NULL && tempNode->leftChild == backTempNode) { backTempNode = tempNode; tempNode = tempNode->parent; } return tempNode; } } |
تابع transplant :
این تابع در تابع حذف گره از درخت استفاده می شود ، این تابع دوگره u و v را به عنوان ورودی می گیرد و اگر u ریشه درخت باشد آنگاه v را ریشه درخت قرار می دهد ،اگر u ریشه دره نباشد پدر u را به v اشاره می دهد ،و اگر v نال نبود پدر v را پدر u قرار می دهد ، در واقع این تابع v را جای u قرار می دهد و از نظر رابطه با گره ی پدر کار ها را درست می کند ولی کاری به جابهجا کردن فرزندان u , v ندارد.
کد این تابع را در زیر آمده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //transplant void bst::transplant(node* u, node* v) { if (u->parent == NULL) { root = v; } else { if (u == u->parent->leftChild) { u->parent->leftChild = v; } else { u->parent->rightChild = v; } } if (v != NULL) { v->parent = u->parent; } } |
تابع deletion :
این تابع گره ای را به عنوان ورودی می گیرد و آن را از درخت حذف می کند ، الگوریتم آن به این شکل است :
– اگر گره هیچ فرزندی نداشت خوب کاری خاصی انجام نمی دهیم فقط حذفش می کنیم.
– اگر گره فرزند سمت راست نداشت با استفاده از تابع transplant فرزند سمت چپش را جایش می گذاریم.
-اگر گره فرزند فقط سمت راست داشت ، با استفاده از transplant فرزند سمت راستش را جایش می می گذاریم.
– اگر گره دو فرزند داشت ، آنگاه successor گره را پیدا می کنیم ،
-اگر ساکسسور گره فرزند گره مورد نظر نبود : آنگاه با استفاده از transplant بچه ی سمت راست ساکسسور را جا ساسسکسور می گداریم و فرزند راشت ساکسسور را فرزند راست گره ی مورد نظر قرار می دهیم،و در ادامه با استفاده از تابع transplant ساکسسور را جای گره ی مورد نظز می گذاریم و بچه ی سمت چپ ساکسسور را بچه ی سمت راست گره ی مورد می کنیم.
-اگر ساکسسور گره ی فرزند گره ی مورد نظر بود:با استفاده از تابع transplant ساکسسور را جای گره ی مورد نظز می گذاریم و بچه ی سمت چپ ساکسسور را بچه ی سمت راست گره ی مورد می کنیم.
یه خورده چیزی که خواستم بگم عجیب قریب شد اگر به کد نگاه کنید حتما متوجه منظورم می شوید :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //remove node from tree void bst::deletion(node *z) { if (z->leftChild == NULL) { transplant(z, z->rightChild); } else { if (z->rightChild == NULL) { transplant(z, z->leftChild); } else { node* succesor = new node(); succesor = successor(z); if (succesor->parent != z) { transplant(succesor, succesor->rightChild); succesor->rightChild = z->rightChild; succesor->rightChild->parent = succesor; } transplant(z, succesor); succesor->leftChild = z->leftChild; succesor->leftChild->parent = succesor; } } delete z; } |
تابع menu :
این تابع منویی برای کار کردن با درخت جست و جوی دودویی می سازد مانند شکل زیر:
کد منو را در زیر می بینید:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | //menu void menu() { char ch; int inttemp; node *temp = new node(); bst myTree; int select; do { system("CLS"); cout << "0. Exit" << endl; cout << "1. Insert"<<endl; cout << "2. Display" << endl; cout << "3. Minimum" << endl; cout << "4. Maximum" << endl; cout << "5. Search" << endl; cout << "6. Inorder walk(sort)" << endl; cout << "7. Successor" << endl; cout << "8. Predecessor" << endl; cout << "9. Delete" << endl; cout << endl << "Enter your selection:" << endl; cin >> select; system("CLS"); switch (select) { case 0: { break; } case 1: { do { int data; cout << "Enter Data: "; cin >> data; myTree.insert(data); cout << endl << "Do you want another node?[y/n]" << endl; cin >> ch; } while (ch != 'n'); break; } case 2: { myTree.display(myTree.root, 0); cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 3: { temp = myTree.minimum(); if (temp != NULL) { cout << "Minimum:" << temp->data << endl; } else { cout << "Tree is empty!" << endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 4: { temp = myTree.maximum(); if (temp != NULL) { cout << "Maximum:" << temp->data << endl; } else { cout << "Tree is empty!" << endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 5: { cout << "Enter key of node:"; cin >> inttemp; temp = myTree.search(inttemp); if (temp != NULL) { cout << "Node with key (" << temp->data << ") exist in tree." << endl; } else { cout << "It isn't in tree!"<< endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 6: { cout << "Sorted order:" << endl; myTree.inorder_tree_walk(myTree.root); cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 7: { cout << "Enter key of node , for finding it's successor: "; cin >> inttemp; temp = myTree.search(inttemp); if (temp == NULL) { cout << "Node with key (" << inttemp << ") isn't in tree." << endl; } else { temp = myTree.successor(temp); if (temp == NULL) { cout << "It hasn't successor" << endl; } else { cout << "Successor: " << temp->data << endl; } } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 8: { cout << "Enter key of node , for finding it's predecessor: "; cin >> inttemp; temp = myTree.search(inttemp); if (temp == NULL) { cout << "Node with key (" << inttemp << ") isn't in tree." << endl; } else { temp = myTree.predecessor(temp); if (temp == NULL) { cout << "It hasn't predecessor" << endl; } else { cout << "Predecessor: " << temp->data << endl; } } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 9: { cout << "Enter key(Data) of node that you want delete: "; cin >> inttemp; temp = myTree.search(inttemp); if (temp == NULL) { cout << "Node with key (" << inttemp << ") isn't in tree." << endl; } else { myTree.deletion(temp); cout << "Node with key (" << inttemp << ") removed from tree." << endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } default: break; } } while (select != 0); } |
و در آخر هم کد کلی درخت جست و جوی دو دویی (Binary search tree – BST ) را می بینید همراه main برنامه :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | // Binary Search Tree.cpp //www.open-mind.ir //Farhad Dalirani #include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; //tree node //------------------------------------------ class node { public: int data; node* parent; node* leftChild; node* rightChild; node(void); }; node::node(void) { data = 0; parent = NULL; leftChild = NULL; rightChild = NULL; } //------------------------------------------ //binary search tree class bst { public: node* root; bst(void); void insert(int); void display(node*, int); node* minimum(); node* maximum(); node* search(int); void inorder_tree_walk(node*); node* successor(node*); node* predecessor(node *); void transplant(node*, node*); void deletion(node *); }; bst::bst(void) { root = NULL; } void bst::insert(int d) { node* newNode = new node(); newNode->data = d; if (root == NULL) { root = newNode; } else { node* tempNode = new node(); node* backTempNode = new node(); tempNode = root; while (tempNode != NULL) { backTempNode = tempNode; if (tempNode->data <= newNode->data) { tempNode = tempNode->rightChild; } else { tempNode = tempNode->leftChild; } } newNode->parent = backTempNode; if (backTempNode->data <= newNode->data) { backTempNode->rightChild = newNode; } else { backTempNode->leftChild = newNode; } } } //display binary search tree void bst::display(node *Node,int level) { if (root == NULL) { cout << "Tree is empty." << endl; } else { if (Node->rightChild != NULL) { display(Node->rightChild, level + 1); } if (Node != root) { for (int i = 0; i < level + 1; i++) { cout << " "; } } else { cout << "Root->"; } cout << Node->data << endl; if (Node->leftChild != NULL) { display(Node->leftChild, level + 1); } } } //return minimum node node* bst::minimum() { node* tempNode = new node(); node* backTempNode = new node(); backTempNode = tempNode = root; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->leftChild; } return backTempNode; } //www.open-mind.ir //return maximum node node* bst::maximum() { node* tempNode = new node(); node* backTempNode = new node(); backTempNode = tempNode = root; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->rightChild; } return backTempNode; } //Search in BST Tree node* bst::search(int Data) { node* tempNode = new node(); tempNode = root; while (tempNode != NULL) { if (tempNode->data == Data) { return tempNode; } else { if (tempNode->data <= Data) { tempNode = tempNode->rightChild; } else { tempNode = tempNode->leftChild; } } } return NULL; } //inoreder tree walk = show all node in sorted mood void bst::inorder_tree_walk(node* currentNode) { if (root != NULL) { if (currentNode->leftChild != NULL) { inorder_tree_walk(currentNode->leftChild); } cout << currentNode->data << " ,"; if (currentNode->rightChild != NULL) { inorder_tree_walk(currentNode->rightChild); } } else { cout << "Tree is empty!" << endl; } } //successor node* bst::successor(node* currentNode) { node* tempNode = new node(); node* backTempNode = new node(); tempNode = currentNode; if (tempNode->rightChild != NULL) { tempNode = tempNode->rightChild; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->leftChild; } return backTempNode; } else { backTempNode = tempNode; tempNode = tempNode->parent; while (tempNode != NULL && tempNode->rightChild == backTempNode) { backTempNode = tempNode; tempNode = tempNode->parent; } return tempNode; } } //predecessor node* bst::predecessor(node* currentNode) { node* tempNode = new node(); node* backTempNode = new node(); tempNode = currentNode; if (tempNode->leftChild != NULL) { tempNode = tempNode->leftChild; while (tempNode != NULL) { backTempNode = tempNode; tempNode = tempNode->rightChild; } return backTempNode; } else { backTempNode = tempNode; tempNode = tempNode->parent; while (tempNode != NULL && tempNode->leftChild == backTempNode) { backTempNode = tempNode; tempNode = tempNode->parent; } return tempNode; } } //transplant void bst::transplant(node* u, node* v) { if (u->parent == NULL) { root = v; } else { if (u == u->parent->leftChild) { u->parent->leftChild = v; } else { u->parent->rightChild = v; } } if (v != NULL) { v->parent = u->parent; } } //remove node from tree void bst::deletion(node *z) { if (z->leftChild == NULL) { transplant(z, z->rightChild); } else { if (z->rightChild == NULL) { transplant(z, z->leftChild); } else { node* succesor = new node(); succesor = successor(z); if (succesor->parent != z) { transplant(succesor, succesor->rightChild); succesor->rightChild = z->rightChild; succesor->rightChild->parent = succesor; } transplant(z, succesor); succesor->leftChild = z->leftChild; succesor->leftChild->parent = succesor; } } delete z; } //------------------------------------------ //menu void menu() { char ch; int inttemp; node *temp = new node(); bst myTree; int select; do { system("CLS"); cout << "0. Exit" << endl; cout << "1. Insert"<<endl; cout << "2. Display" << endl; cout << "3. Minimum" << endl; cout << "4. Maximum" << endl; cout << "5. Search" << endl; cout << "6. Inorder walk(sort)" << endl; cout << "7. Successor" << endl; cout << "8. Predecessor" << endl; cout << "9. Delete" << endl; cout << endl << "Enter your selection:" << endl; cin >> select; system("CLS"); switch (select) { case 0: { break; } case 1: { do { int data; cout << "Enter Data: "; cin >> data; myTree.insert(data); cout << endl << "Do you want another node?[y/n]" << endl; cin >> ch; } while (ch != 'n'); break; } case 2: { myTree.display(myTree.root, 0); cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 3: { temp = myTree.minimum(); if (temp != NULL) { cout << "Minimum:" << temp->data << endl; } else { cout << "Tree is empty!" << endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 4: { temp = myTree.maximum(); if (temp != NULL) { cout << "Maximum:" << temp->data << endl; } else { cout << "Tree is empty!" << endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 5: { cout << "Enter key of node:"; cin >> inttemp; temp = myTree.search(inttemp); if (temp != NULL) { cout << "Node with key (" << temp->data << ") exist in tree." << endl; } else { cout << "It isn't in tree!"<< endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 6: { cout << "Sorted order:" << endl; myTree.inorder_tree_walk(myTree.root); cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 7: { cout << "Enter key of node , for finding it's successor: "; cin >> inttemp; temp = myTree.search(inttemp); if (temp == NULL) { cout << "Node with key (" << inttemp << ") isn't in tree." << endl; } else { temp = myTree.successor(temp); if (temp == NULL) { cout << "It hasn't successor" << endl; } else { cout << "Successor: " << temp->data << endl; } } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 8: { cout << "Enter key of node , for finding it's predecessor: "; cin >> inttemp; temp = myTree.search(inttemp); if (temp == NULL) { cout << "Node with key (" << inttemp << ") isn't in tree." << endl; } else { temp = myTree.predecessor(temp); if (temp == NULL) { cout << "It hasn't predecessor" << endl; } else { cout << "Predecessor: " << temp->data << endl; } } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } case 9: { cout << "Enter key(Data) of node that you want delete: "; cin >> inttemp; temp = myTree.search(inttemp); if (temp == NULL) { cout << "Node with key (" << inttemp << ") isn't in tree." << endl; } else { myTree.deletion(temp); cout << "Node with key (" << inttemp << ") removed from tree." << endl; } cout << endl << "Press 0 to continue!" << endl; cin >> ch; break; } default: break; } } while (select != 0); } //------------------------------------------ int main( ) { menu(); return 0; } |
قسمت دوم:
کد درخت جست و جوی دو دویی (BST) را با زبان جاوا می بینید که آقای Farhang Amary لطف کرده اند و در اختیارمان قرار داده اند.
جمله خودشون در مورد کد زیر:
پیاده سازی نود و درخت جستجوی دودویی رو به صورت جنریک (کلید و مقدار) که کلید، هر نوعی که واسط Comparable رو پیاده سازی کنه قبول میکنه ، به همراه توابع جستجو ،درج ، حذف ،کمترین و بیشترین رو نوشتم
کد های برنامه در دوفایل جداگانه به اسم BST.java و BSTNode.java قرار دارد که در زیر آن را مشاهده می کنید:
BST.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mydatastructures; /** * * @author Farhang */ public class BST<K extends Comparable<K>,V> { private BSTNode<K,V> root; public BST() { this.root=null; } public BST(BSTNode<K,V> root) { this.root=root; } public void setRoot(BSTNode<K,V> root){this.root=root;} public BSTNode<K,V> getRoot(){return this.root;} //Inserting a node into the tree. public void insert(BSTNode<K,V> node) throws Exception { if (this.root==null){this.setRoot(node);} else {insert(node,this.root);} } private void insert(BSTNode<K,V> node,BSTNode<K,V> root) throws Exception { if (root.getKey().compareTo(node.getKey())==0){throw new Exception("A node with this key already exists");} else if (node.getKey().compareTo(root.getKey())>0) { if(root.getRight()==null){root.setRight(node);} else{insert(node,root.getRight());} } else if (node.getKey().compareTo(root.getKey())<0) { if(root.getLeft()==null){ root.setLeft(node);} else{insert(node,root.getLeft());} } } //Checking if a node belongs to the tree. public Boolean lookup(BSTNode<K,V> node){return lookup(node,this.root);} private Boolean lookup(BSTNode<K,V> node,BSTNode<K,V> root) { if(root==null){return false;} else if(root.getKey().compareTo(node.getKey())==0){return true;} else if(root.getKey().compareTo(node.getKey())>0){return lookup(node,root.getRight());} else if(root.getKey().compareTo(node.getKey())<0){return lookup(node,root.getLeft());} return false; } //Removing a node from the tree. public void delete(K key) { this.root= delete(root, key); } private BSTNode<K,V> delete(BSTNode<K,V> n, K key) { if (n == null) { return null; } if (key.equals(n.getKey())) { // n is the node to be removed if (n.getLeft() == null && n.getRight() == null) { return null; } if (n.getLeft() == null) { return n.getRight(); } if (n.getRight() == null) { return n.getLeft(); } // if we get here, then n has 2 children K smallVal = getMin(n.getRight()).getKey(); n.setKey(smallVal); n.setRight( delete(n.getRight(), smallVal) ); return n; } else if (key.compareTo(n.getKey()) < 0) { n.setLeft( delete(n.getLeft(), key) ); return n; } else { n.setRight( delete(n.getRight(), key) ); return n; } } // Getting the minimum node in the tree. public BSTNode<K,V> getMin() { if (this.root==null || this.root.getLeft()==null){return this.root;} else {return getMin(this.root.getLeft());} } private BSTNode<K,V> getMin(BSTNode<K,V> root) { if(root.getLeft()==null){return root;} else{return getMin(root.getLeft());} } //Getting the maximum node in the tree. public BSTNode<K,V> getMax() { if (this.root==null || this.root.getRight()==null){return this.root;} else {return getMin(this.root.getRight());} } private BSTNode<K,V> getMax(BSTNode<K,V> root) { if(root.getRight()==null){return root;} else{return getMax(root.getRight());} } } |
BSTNode.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mydatastructures; /** * * @author Farhang */ public class BSTNode<K,V> { private K key; private V value; private BSTNode<K,V> left,right; public BSTNode(K key,V value) { this.key=key; this.value=value; this.left=null; this.right=null; } public BSTNode(K key,V value,BSTNode<K,V> left,BSTNode<K,V> right) { this.key=key; this.value=value; this.left=left; this.right=right; } public void setKey(K key){this.key=key;} public void setValue(V value){this.value=value;} public void setLeft(BSTNode<K,V> left){this.left=left;} public void setRight(BSTNode<K,V> right){this.right=right;} public K getKey(){return this.key;} public V getValue(){return this.value;} public BSTNode<K,V> getLeft(){return this.left;} public BSTNode<K,V> getRight(){return this.right;} } |
سلام و عرض ادب، بسیار لذت بردم و استفاده کردم از مطالب خوبتون، امیدوارم روز به روز در راه نشر علم و دانش موفق تر باشید و آرزوی سلامتی و دل خوش دارم براتون. جدا زحمت می کشید، خسته نباشید، باعث افتخار هستید.
ارادتمند
بابت این نظر دلگرم کننده تشکر می کنم.
حیف ام اومد بخونم ولی تشکر نکنم ؛
ممنون از مطالب خوبی که میذارین
:*)
تشکر،لطف دارید. همین کامنت های شماست که انرژی می ده برای پست های بیشتر.
سلام سایت خیلی خوبی هست مرسی
من میخوام شجرهنامه درست کنم بعد اگه بیشتر از دوتا فرزند داشته باشه نمیشه از دو دویی استفاده کنم اگه میشه یک راهنمایی کنین یا کدش رو بدین مرسی
خوب از یک درخت غیر دوتایی استفاده کن! ببین برای ساخت درخت دوتایی هر راس درختت دو اشاره گر دارد یکی به فرزند چپ و یکی به راست ! حالا یک راه ساده این است که اشاره گر های درون راس درخت را افزایش دهیم
منظورتون رو نفهمیدم درختی که میخوام بسازم پویا هست و ممکن هست تعداد فرزندان زیاد باشه برای همین نمیشه به تعداد فرزندان اشاره گر زد
برای سرچ کردن هم مشکل پیدا کردم باید پیمایش ککنیم درخت رو اما وقتی اشاره گری ببه فرزندان دیگه نداریم نمیشه
یک کتاب الگوریتم خیلی معروف هست به اسم
Introduction to algorithms
که به
کتاب الگوریتم CLRS هم معروفه (هر کدام از کاراکترها اول فامیلی هر کدام از نویسنده هاش است).
به صفحه ی ۲۴۷ شکل شماره ی ۱۰.۱۰ ویرایش ۳ این کتاب مراجعه کن – مشکلت حل می شه.
خیلی مرسی مشکل من حل شد خیلی ممنون اگه مشکل برخوردم باز مزاحم میشم خیلی ممنون
سلام ممنون بابت برنامه خوبتون واقعا مفید بود . یه سوال داشتم :
بازسازی درخت جستجوی دودویی چه فرقی با کدی که شما گذاشتین داره؟
سلام – ببین ک بالا درخت جست و جوی دودویی رو به صورت عادی می سازد یعنی ما یک نود را وارد درخت می کنیم و طبق قواعد درخت باینری دودویی یک جایی از درخت قرار می گیره که بعضی واقع باعث می شه درخت به صورت نامتوازن رشد کند و عمق درخت در یک قسمت زیاد شود اون موقع دیگه هزینه ی جست و جو در درخت دیگر lg n نیست بلکه n lg n است. به همین دلیل باید یک سری الگوریتم های دیگه هستند مانند red black tree که حذف و اضافه کردن نود به درخت رو طوری انجام می دهند که درخت متوازن می ماند.
سلام
ببینید من جواب سوالمو نگرفتم . راستش استادم یه پروژه تعیین کرده برای بازسازی درخت جستجوی دودویی و من شبیه این کد رو نوشتم و تحویل استادم دادم ولی ایشون میگه من بازسازی میخوام نه خود کد رو . میشه راهنمایی م کنید بازسازی درخت یعنی چی؟
منم اولین باره که دارم از خود شما می شنوم
سلام……میخواستم اگه درخت رو بازسازی کنم یعنی وقتی یک مقدار رو حذف کنم که جاش خالی نمونه و مقدار بعدی جایگزینش بشه (((به اصطلاح بازسازی درخت جستجوی دودویی کنم)) باید چیکار کنم—خیلی ممنون
تابع
deletion
همین کار رو می کنه خوب!
با سلام خدمت open-mind ببخشید برای موازنه کردن این درخت میشه راهنمایی کنید؟
red – black tree
را باید پیاده سازی کنید
سوال:
۱.آیا میشه در یک نود از درخت، دو یا چند داده ذخیره کرد؟
۲.و یکی از اون داده ها رو به عنوان کلید نود درنظر گرفت؟
ممنون میشم جواب بدید.
سلام بله ، من از یک دیتا از نوع عدد صحیح استفاده کردم ، هدفم این بود که کارکرد درخت باینری رو نشان بدهم ، شما می تونی مانند کد جاوا آخر پست برنامتو جنریک بنویسی یعنی مهم نباشه به کلاست درختت چی می دی.برای این کار کد درخت bst به صورت template س بنویس
اگه میشه پیمایش ترتیب سطح درخت دودویی رو بزارید رو سایت
سلام
منظور شما level-order هست ؟
اضافه خواهد شد …
سلام آقای دلیرانی وقت بخیر
این سوال به این بخش ربطی نداره ولی من در اجرای این برنامه دچار مشکل شدم …در خط ۲۹ برنامه
(srand time0)()وقتی با devاز میکنم خطا در اجرا میده این خطا را میده ([Error] ‘getch’ was not declared in this scope)و برنامه را ران نمیکنه میشه بهم کمک کنید هرچه سریع تر
۱دنیا ممنون
#include
using std::cout;
using std::cin;
using std::endl;
#include
#include
using std::setw;
const int RACE_END = 70;
void moveTortoise(int * const);
void moveHare(int * const);
void printCurrentPositions(const int * const, const int * const);
int main()
{
int tortoise = 1, hare = 1, timer = 0;
srand(time(0));
cout << "ON YOUR MARK, GET SET\nBANG !!!!"
<= hare)
cout << "\nTORTOISE WIN. Yuch.\n";
cout << "TIME ELABSED = " << timer << " seconds" <= 1 && x <= 5) //fast plod
*turtlePtr += 3;
else if (x == 6 || x == 7) //slip
*turtlePtr -= 6;
else //slow plod
++(*turtlePtr);
if(*turtlePtr RACE_END)
*turtlePtr = RACE_END;
}
//+++++++++++++++
void moveHare(int * const rabbitPtr)
{
int y = 1 + rand() % 10;
if(y == 3 || y == 4) //big hop
*rabbitPtr += 9;
else if(y == 5) //big slip
*rabbitPtr -= 12;
else if(y >= 6 && y 8) //small slip
*rabbitPtr -= 2;
if(*rabbitPtr RACE_END)
*rabbitPtr = RACE_END;
}
//——————-
void printCurrentPositions(const int * const snapperPtr, const int * const bunnyPtr)
{
if(*bunnyPtr == *snapperPtr)
cout << setw(*bunnyPtr) << "oUCH!!!";
else if (*bunnyPtr < *snapperPtr)
cout << setw(*snapperPtr – *bunnyPtr) << 'T'
<< setw(*snapperPtr – *bunnyPtr) << 'T';
else
cout << setw(*snapperPtr) << 'T'
<< setw(*bunnyPtr – *snapperPtr) << 'H';
cout << '\n';
}
کتابخانه ی
رو به برنامت اضافه کن درست می شه. اگر نشد با ویژوال استدیو برنامه رو کامپایل کن.
سلام وقت بخیر
فایل کتابخانه ای را کجاش اضافه کنم خودم بنویسم ی جلوی یکی از #include های داخل برنامه بنویسم ؟
وقتی اضافه میکنم ارور میده به بقیه سرآمدهام ؟؟؟؟؟؟؟؟؟؟؟؟
سلام
لطفا متن ارور رو بنویسید
سلام و عرض ادب
ببخشید چطوری میشه مثل شما دو تا کلاس در ویژوال استادیو ۲۰۱۰ تعریف کرد؟؟
من این کار رو کردم ولی error دادو براش ناشناس بود
خواهش میکنم کمکم کنید
چه اروری داد ؟ محیط توسعه ات چیه ؟ چه کامپایلری استفاده می کنی؟
قربون دستت !!!!!!!!!!!!!!!!!
کارتون عالیه، متشکریم.
در مورد این مطلب یک نکته مهم اینکه اگر یک Object از کلاس bst در کلاس node قرار میدادید (اشاره گری به درخت حاوی گره جاری) کدهای مربوط به برخی متدهای دیگه میتونست بسیار بهینه بشه همچنین به نظر بنده این یک نقص است در کد وقتی کلاسها و کد قابلیت تشخیص اینکه یک گره به کدام درخت تعلق دارد را نداشته باشد.
تشکر از نظر خوبتان. متاسفانه من این کد بیشتر برای بررسی الگوریتمی درخت جست و جوی دو دویی نوشتم ، اگر شما کد این درخت رو با نکاتی که خود ذکر کرده اید نوشته اید می توانید آن را برایم ایمیل کنید تا آن را در اختیار سایر دوستان و خوانندگان قرار دهیم.
راستش وقت نکردم هنوز تو سی پلاس پلاس انجام بدم ولی چون خودم جاواکار هستم اومدم برای یادآوری واسه خودم دقیقاً یک پروژه به اسم mydatastructures ایجاد کردم و میخوام پیاده سازی تمام ساختمانهای داده و الگوریتمهای مربوطه رو تو این پکیج بنویسم خیلی لازم میشه فعلاً پیاده سازی نود و درخت جستجوی دودویی رو به صورت جنریک (کلید و مقدار) که کلید، هر نوعی که واسط Comparable رو پیاده سازی کنه قبول میکنه ، به همراه توابع جستجو ،درج ، حذف ،کمترین و بیشترین رو نوشتم همین الان جفت کلاس ها رو واستون ایمیل میکنم
تشکر فراوان ، به زودی پست رو آپدیت می کنم و کد شما را در آن قرار می دهم.
بسیار عالی بود! تشکر.
همچین مطالبی تو وب پارسی بسیار کمیاب است. کارتون رو ادامه بدید، و دسته بندیتون رو بهتر کنید.
سپاس ، در روز های آینده تغییراتی اساسی انجام میدیم 🙂