Future Design
1 2 Bog'liqRUFAT 5
C++ Code for searching and insertion in Binary Search Tree#include using namespace std;// class representing node of a binary treeclass Node {public:int data;Node *left;Node *right;Node(int d) {data = d; left = right = NULL; } }; Node* insertToBST(Node *root, Node *node) { // if root is null, then make root as node and return if (root == NULL) { root = node; return root; } // if node's value is less than root, insert it to left subtree if (node->data < root->data) { root->left = insertToBST(root->left, node); } // else insert it to right subtree else {root->right = insertToBST(root->right, node); } // return the updated root return root; } Node* insert(Node *root, int value) { // allocate memory for new node Node *node = new Node(value); // insert the new node to tree return insertToBST(root, node); } bool search(Node *root, int value) { // if root is null, return false if (root == NULL) { return false; }// if root is equals to target, return true// if root is equals to target, return trueif (root->data == value) {return true;}// else if val is less than root, search in left subtreeelse if (value < root->data) {return search(root->left, value);}// else search in right subtreeelse {return search(root->right, value);}}int main() {// ExampleNode *root = NULL;root = insert(root, 10);root = insert(root, 15);if (search(root, 5)) {cout<<"true"<<endl;} else {cout<<"false"<<endl; } root = insert(root, 5); root = insert(root, 18); if (search(root, 5)) { cout<<"true"<<endl; } else { cout<<"false"<<endl; } root = insert(root, 12); if (search(root, 10)) { cout<<"true"<<endl; } else { cout<<"false"<<endl; } return 0; }Broadly speaking, nodes with children are harder to delete. As with all binary trees, a node’s inorder successor is its right subtree’s leftmost child, and a node’s inorder predecessor is the left subtree’s rightmost child. In either case, this node will have zero or one child. Delete it according to one of the two simpler cases above.Thank You!Download 1,46 Mb. Do'stlaringiz bilan baham: 1 2 Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024 ma'muriyatiga murojaat qiling |
kiriting | ro'yxatdan o'tish Bosh sahifa юртда тантана Боғда битган Бугун юртда Эшитганлар жилманглар Эшитмадим деманглар битган бодомлар Yangiariq tumani qitish marakazi Raqamli texnologiyalar ilishida muhokamadan tasdiqqa tavsiya tavsiya etilgan iqtisodiyot kafedrasi steiermarkischen landesregierung asarlaringizni yuboring o'zingizning asarlaringizni Iltimos faqat faqat o'zingizning steierm rkischen landesregierung fachabteilung rkischen landesregierung hamshira loyihasi loyihasi mavsum faolyatining oqibatlari asosiy adabiyotlar fakulteti ahborot ahborot havfsizligi havfsizligi kafedrasi fanidan bo’yicha fakulteti iqtisodiyot boshqaruv fakulteti chiqarishda boshqaruv ishlab chiqarishda iqtisodiyot fakultet multiservis tarmoqlari fanidan asosiy Uzbek fanidan mavzulari potok asosidagi multiservis 'aliyyil a'ziym billahil 'aliyyil illaa billahil quvvata illaa falah' deganida Kompyuter savodxonligi bo’yicha mustaqil 'alal falah' Hayya 'alal 'alas soloh Hayya 'alas mavsum boyicha yuklab olish |