






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
data structures and algorithms (bst)
Typology: Cheat Sheet
1 / 10
This page cannot be seen from the preview
Don't miss anything!







//Binary Search Tree Program #include
void insert(int); void remove(int); }; // Smaller elements go left // larger elements go right void BinarySearchTree::insert(int d) { tree_node* t = new tree_node; tree_node* parent; t->data = d; t->left = NULL; t->right = NULL; parent = NULL; // is this a new tree? if (isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* curr; curr = root; // Find the Node's parent while (curr) { parent = curr; if (t->data >= curr->data) curr = curr->right; else curr = curr->left; }
parent = curr; if (d>curr->data) curr = curr->right; else curr = curr->left; } } if (!found) { cout << " Data not found! " << endl; return; } // 3 cases : // 1. We're removing a leaf node // 2. We're removing a node with a single child // 3. we're removing a node with 2 children // Node with single child if ((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL)) { if (curr->left == NULL && curr->right != NULL) { if (parent->left == curr) { parent->left = curr->right; delete curr; } else
parent->right = curr->right; delete curr; }§ } else // left child present, no right child { if (parent->left == curr) { parent->left = curr->left; delete curr; } else { parent->right = curr->left; delete curr; } } return; } //We're looking at a leaf node if (curr->left == NULL && curr->right == NULL) { if (parent->left == curr) parent->left = NULL; else parent->right = NULL; delete curr; return; }
delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = curr->right; curr->data = tmp->data; curr->right = tmp->right; delete tmp; } } return; } } void BinarySearchTree::print_inorder() { inorder(root); } void BinarySearchTree::inorder(tree_node* p)//LNR { if (p != NULL) { if (p->left) inorder(p->left); cout << " " << p->data << " "; if (p->right) inorder(p->right);
else return; } void BinarySearchTree::print_preorder() { preorder(root); } void BinarySearchTree::preorder(tree_node* p) { if (p != NULL) { cout << " " << p->data << " "; if (p->left) preorder(p->left); if (p->right) preorder(p->right); } else return; } void BinarySearchTree::print_postorder() { postorder(root); } void BinarySearchTree::postorder(tree_node* p) { if (p != NULL) { if (p->left) postorder(p->left);
cout << " In-Order Traversal " << endl; cout << " -------------------" << endl; b.print_inorder(); break; case 3: cout << endl; cout << " Pre-Order Traversal " << endl; cout << " -------------------" << endl; b.print_preorder(); break; case 4: cout << endl; cout << " Post-Order Traversal " << endl; cout << " --------------------" << endl; b.print_postorder(); break; case 5: cout << " Enter data to be deleted : "; cin >> tmp1; b.remove(tmp1); break; case 6: return 0; } } }