Guruh: swd026-2 Talaba: Muhammadov Murodjon 1-vazifa
Download 149.01 Kb.
|
“Ma’lumotlar tuzilmasi va algoritmlar» fanidan topshiriq bo’yicha hisobot Topshiriq: 1 Guruh: SWD026-2 Talaba: Muhammadov Murodjon 1-vazifaN #include <iostream> using namespace std; class tree_elem //TREE_ELEM nomli class yaratib olamiz {public: //public orqali razrisheniyalarga ruxsat beramiz int m_data; //int tipiga m data o;zgaruvchi kiritib olamiz tree_elem * m_left; tree_elem * m_right; tree_elem(int val){ m_left = NULL; m_right = NULL; m_data = val;}}; class binary_tree //yana binary_tree nomli class yaratiladi {private: //foydalanishlarga cheklov qoyish maqsadida private dan foydalanamiz tree_elem * m_root; Int m_size; void print_tree(tree_elem *); //print tree funksiyasini yaratib olamizvoid delete_tree(tree_elem *); //tree_elem ni o'chirishimiz uchun delete_tree funksiyasini ham yozib olamiz public: // yuqoridagi bergan razrisheniyamizni chaqirib ichiadi malumotlardan foydalanib olamiZ binary_tree(int);~binary_tree(); void print(); bool find(int); void insert(int); void erase(int);I nt size();}; binary_tree::binary_tree(int key){ m_root = new tree_elem(key); m_size = 1;} binary_tree::~binary_tree(){ delete_tree(m_root);} void binary_tree::delete_tree(tree_elem * curr){if (curr){ delete_tree(curr->m_left); delete_tree(curr->m_right); delete curr;}}void binary_tree::print(){ print_tree(m_root);cout << endl;} void binary_tree::print_tree(tree_elem * curr){if (curr){ print_tree(curr->m_left);cout << curr->m_data << " "; print_tree(curr->m_right);}} bool binary_tree::find(int key){ tree_elem * curr = m_root; while (curr && curr->m_data != key) //while ga sikl yozib olamiz {if (curr->m_data > key) // agar curr->m_data > key dan katta bolsa curr = curr->m_left; // curr-> m_left chiqsin else // aks holda curr = curr->m_right; // curr->m_right chiqsin }return curr != NULL;} void binary_tree::insert(int key){ tree_elem * curr = m_root; while (curr && curr->m_data != key) {if (curr->m_data > key && curr->m_left == NULL){ curr->m_left = new tree_elem(key);++m_size; return;} If (curr->m_data < key && curr->m_right == NULL){ curr->m_right = new tree_elem(key);++m_size; return;} If (curr->m_data > key) curr = curr->m_left;else curr = curr->m_right;}} void binary_tree::erase(int key){ tree_elem * curr = m_root; tree_elem * parent = NULL; while (curr && curr->m_data != key){ parent = curr; If (curr->m_data > key){ curr = curr->m_left;} else{curr = curr->m_right;}} If (!curr) return; If (curr->m_left == NULL){ If (parent && parent->m_left == curr) parent->m_left = curr->m_right; If (parent && parent->m_right == curr) parent->m_right = curr->m_right;--m_size; delete curr; return;} If (curr->m_right == NULL){ If (parent && parent->m_left == curr) parent->m_left = curr->m_left; If (parent && parent->m_right == curr) parent->m_right = curr->m_left;--m_size; delete curr; return;} tree_elem * replace = curr->m_right; while (replace->m_left)replace = replace->m_left; Int replace_value = replace->m_data; erase(replace_value); curr->m_data = replace_value;} Int main() { binary_tree bin(55); bin.insert(56); bin.insert(45); bin.insert(65); bin.insert(22); bin.insert(34); bin.insert(46); bin.insert(58); bin.print();cout<<endl;cout<<bin.find(34); bin.erase(34);cout<<bin.find(34); return 0;} Download 149.01 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling