Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Binary Search Tree

Uploaded by

Thận Ng
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Binary Search Tree

Uploaded by

Thận Ng
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Binary Search Tree

1.Khai báo
struct Node{
int val;
Node* left;
Node* right;
Node(int val) : val(val), left(NULL), right(NULL) {}
};

struct BinarySearchTree{
Node* root;
};
2.Them node
// Recursive function to insert a key into a BST
Node* insert(Node* root, int key)
{
// if the root is null, create a new node and return it
if (root == nullptr) {
return new Node(key);
}

// if the given key is less than the root node, recur for the left subtree
if (key < root->data) {
root->left = insert(root->left, key);
}
// if the given key is more than the root node, recur for the right subtree
else {
root->right = insert(root->right, key);
}

return root;
}
3.Delete node for 3 cases
//xoa node khoi cay
Node* Delete(Node* &root, int key){
// base case
if(root == NULL){
return root;
}
// Node can xoa la node leaf
if(key < root->val){
root->left = Delete(root->left, key);
}
else if(key > root->val){
root->right = Delete(root->right, key);
}
else{
// Node can xoa co 1 children
if(root->left == NULL){
Node* temp = root->right;
delete root;
return temp;
}
else if(root->right == NULL){
Node* temp = root->left;
delete root;
return temp;
}
//Node can xoa co 2 children
Node* temp = minValueNode(root->right);
root->val = temp->val;
root->right = Delete(root->right, temp->val);
}
return root;

// attribute cua Delete function de tim Node leftmost


Node* minValueNode(Node* node) {
Node* current = node;
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;

return current;
}
}
4.Search key node on tree
void BinarySearch(Node* root, int key, Node* parent){
// if the key is not present in the key
if (root == NULL){
cout << "Key not found";
return;
}
// if the key is found
if (root->data == key){
if (parent == NULL){
cout << "The node with key " << key << " is root node";
}
else if (key < parent->data){
cout << "The given key is the left node of the node with key " <<
parent->data;
}
else{
cout << "The given key is the right node of the node with key " <<
parent->data;
}
return;
}
// if the given key is less than the root node, recur for the left subtree;
// otherwise, recur for the right subtree
if (key < root->data){
search(root->left, key, root);
}
else{
search(root->right, key, root);
}
}
5.Check Binary Search Tree
bool isBST(Node* node, int minKey, int maxKey)
{
// base case
if (node == nullptr) {
return true;
}

// if the node's value falls outside the valid range


if (node->data < minKey || node->data > maxKey) {
return false;
}
// recursively check left and right subtrees with an updated range
return isBST(node->left, minKey, node->data) &&
isBST(node->right, node->data, maxKey);
}

// Function to determine whether a given binary tree is a BST


void isBST(Node* root)
{
if (isBST(root, INT_MIN, INT_MAX)) {
printf("The tree is a BST.");
}
else {
printf("The tree is not a BST!");
}
}

6.Delete the node with key value outside valid range


Node* truncate(Node* root, int low, int high)
{
// base case
if (root == nullptr) {
return root;
}

// recursively truncate the left and right subtree first


root->left = truncate(root->left, low, high);
root->right = truncate(root->right, low, high);

Node* curr = root;

// if the root's key is smaller than the minimum allowed, delete it


if (root->data < low)
{
root = root->right;
delete curr;
}
// if the root's key is larger than the maximum allowed, delete it
else if (root->data > high)
{
root = root->left;
delete curr;
}

return root;
}
7. Find pair of a given sum in BST
void inOrder(Node* root, int* array, int index){
if(root == NULL) return;
inOrder(root->left, array, k);
array[k++] = root->val;
inOrder(root->right, array, k);
}
void convertToArray(Node* root, int&*array, int &size){
int index = 0;
inOrder(root, array, index);
size = index;
}
bool findPair(int* array, int size, int key){
for (int i = 0; i < size - 1; i++){
for (int j = i + 1; j < n; j++){
if (array[i] + array[j] == key)
{
return true;
}
}
}
return false;
}
8.Check if tree is balanced
int isHeightBalanced(Node* root, bool &isBalanced){
if (root == nullptr || !isBalanced) {
return 0;
}
int left_height = isHeightBalanced(root->left, isBalanced);
int right_height = isHeightBalanced(root->right, isBalanced);
if (abs(left_height - right_height) > 1) {
isBalanced = false;
}
return max(left_height, right_height) + 1;
}

// The main function to check if a given binary tree is height-balanced or not


bool isHeightBalanced(Node* root){
bool isBalanced = true;
isHeightBalanced(root, isBalanced);
return isBalanced;
}

9.Find floor and find ceil from a binary search tree


(ceil = hoặc lớn hơn key; floor = hoặc nhỏ hơn key)
int findCeil(Node* root, int key){
int ceil = -1;
while(root != NULL){
if(root->value = key){
ceil = root->value;
return ceil;
}
if(key > root->value){
root = root->right;
}
else{
ceil = root->data;
root = root->left;
}
}
return ceil;
}

int findFloor(Node* root, int key){


int floor = -1;
while(root != NULL){
if(root->value == key){
floor = root->value;
return floor;
}
if(key > root->value){
floor = root->value
root = root->right;
}
else{
root = root->left;
}
}
return floor;
}

You might also like