diff --git a/Files_4_thesis/BST b/Files_4_thesis/BST new file mode 100755 index 0000000000000000000000000000000000000000..b1a488a93818a9ff352afdaf38fa98d875ec5ecc Binary files /dev/null and b/Files_4_thesis/BST differ diff --git a/Files_4_thesis/bst.cpp b/Files_4_thesis/bst.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b81a95643411982db49f6b81ff4196fc57622d4a --- /dev/null +++ b/Files_4_thesis/bst.cpp @@ -0,0 +1,294 @@ +//Binary Search Tree Program + +#include <iostream> +#include <cstdlib> +using namespace std; + +class BinarySearchTree +{ + private: + struct tree_node + { + tree_node* left; + tree_node* right; + int data; + }; + tree_node* root; + + public: + BinarySearchTree() + { + root = NULL; + } + + bool isEmpty() const { return root==NULL; } + void print_inorder(); + void inorder(tree_node*); + void print_preorder(); + void preorder(tree_node*); + void print_postorder(); + void postorder(tree_node*); + 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; + } + + if(t->data < parent->data) + parent->left = t; + else + parent->right = t; + } +} + +void BinarySearchTree::remove(int d) +{ + //Locate the element + bool found = false; + if(isEmpty()) + { + cout<<" This Tree is empty! "<<endl; + return; + } + + tree_node* curr; + tree_node* parent; + curr = root; + + while(curr != NULL) + { + if(curr->data == d) + { + found = true; + break; + } + else + { + 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; + } + + + //Node with 2 children + // replace node with smallest value in right subtree + if (curr->left != NULL && curr->right != NULL) + { + tree_node* chkr; + chkr = curr->right; + if((chkr->left == NULL) && (chkr->right == NULL)) + { + curr = chkr; + delete chkr; + curr->right = NULL; + } + else // right child has children + { + //if the node's right child has a left child + // Move all the way down left to locate smallest element + + if((curr->right)->left != NULL) + { + tree_node* lcurr; + tree_node* lcurrp; + lcurrp = curr->right; + lcurr = (curr->right)->left; + while(lcurr->left != NULL) + { + lcurrp = lcurr; + lcurr = lcurr->left; + } + curr->data = lcurr->data; + 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) +{ + 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); + if(p->right) postorder(p->right); + cout<<" "<<p->data<<" "; + } + else return; +} + +int main() +{ + BinarySearchTree b; + int ch,tmp,tmp1; + while(1) + { + cout<<endl<<endl; + cout<<" Binary Search Tree Operations "<<endl; + cout<<" ----------------------------- "<<endl; + cout<<" 1. Insertion/Creation "<<endl; + cout<<" 2. In-Order Traversal "<<endl; + cout<<" 3. Pre-Order Traversal "<<endl; + cout<<" 4. Post-Order Traversal "<<endl; + cout<<" 5. Removal "<<endl; + cout<<" 6. Exit "<<endl; + cout<<" Enter your choice : "; + + cin>>ch; + + switch(ch) + { + case 1 : cout<<" Enter Number to be inserted : "; + cin>>tmp; + b.insert(tmp); + break; + case 2 : cout<<endl; + 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; + + } + } +} diff --git a/Files_4_thesis/bst.cpp~ b/Files_4_thesis/bst.cpp~ new file mode 100644 index 0000000000000000000000000000000000000000..75d5deb8d625a8396f59751b8fd27d4a183a56fa --- /dev/null +++ b/Files_4_thesis/bst.cpp~ @@ -0,0 +1,292 @@ +//Binary Search Tree Program + +#include <iostream> +#include <cstdlib> +using namespace std; + +class BinarySearchTree +{ + private: + struct tree_node + { + tree_node* left; + tree_node* right; + int data; + }; + tree_node* root; + + public: + BinarySearchTree() + { + root = NULL; + } + + bool isEmpty() const { return root==NULL; } + void print_inorder(); + void inorder(tree_node*); + void print_preorder(); + void preorder(tree_node*); + void print_postorder(); + void postorder(tree_node*); + 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; + } + + if(t->data < parent->data) + parent->left = t; + else + parent->right = t; + } +} + +void BinarySearchTree::remove(int d) +{ + //Locate the element + bool found = false; + if(isEmpty()) + { + cout<<" This Tree is empty! "<<endl; + return; + } + + tree_node* curr; + tree_node* parent; + curr = root; + + while(curr != NULL) + { + if(curr->data == d) + { + found = true; + break; + } + else + { + 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; + } + + + //Node with 2 children + // replace node with smallest value in right subtree + if (curr->left != NULL && curr->right != NULL) + { + tree_node* chkr; + chkr = curr->right; + if((chkr->left == NULL) && (chkr->right == NULL)) + { + curr = chkr; + delete chkr; + curr->right = NULL; + } + else // right child has children + { + //if the node's right child has a left child + // Move all the way down left to locate smallest element + + if((curr->right)->left != NULL) + { + tree_node* lcurr; + tree_node* lcurrp; + lcurrp = curr->right; + lcurr = (curr->right)->left; + while(lcurr->left != NULL) + { + lcurrp = lcurr; + lcurr = lcurr->left; + } + curr->data = lcurr->data; + 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) +{ + 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); + if(p->right) postorder(p->right); + cout<<" "<<p->data<<" "; + } + else return; +} + +int main() +{ + BinarySearchTree b; + int ch,tmp,tmp1; + while(1) + { + cout<<endl<<endl; + cout<<" Binary Search Tree Operations "<<endl; + cout<<" ----------------------------- "<<endl; + cout<<" 1. Insertion/Creation "<<endl; + cout<<" 2. In-Order Traversal "<<endl; + cout<<" 3. Pre-Order Traversal "<<endl; + cout<<" 4. Post-Order Traversal "<<endl; + cout<<" 5. Removal "<<endl; + cout<<" 6. Exit "<<endl; + cout<<" Enter your choice : "; + cin>>ch; + switch(ch) + { + case 1 : cout<<" Enter Number to be inserted : "; + cin>>tmp; + b.insert(tmp); + break; + case 2 : cout<<endl; + 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; + + } + } +} diff --git a/MobileRobot/ObjectTrack/bst b/MobileRobot/ObjectTrack/bst index fbf7dfb48e654c0c3afd4cf0c70a0f624954e816..12384a64476fce3865672035f372e84a1085a6ad 100755 Binary files a/MobileRobot/ObjectTrack/bst and b/MobileRobot/ObjectTrack/bst differ diff --git a/MobileRobot/ObjectTrack/bst.cpp b/MobileRobot/ObjectTrack/bst.cpp index 841476f1e57be921ff8dcf7846fee6cde8302946..76906a4564ba010154e65fce7a5eaa9865aa5eba 100644 --- a/MobileRobot/ObjectTrack/bst.cpp +++ b/MobileRobot/ObjectTrack/bst.cpp @@ -1,5 +1,5 @@ #include <iostream> - +#include <stdio.h> using namespace std; struct BstNode { @@ -9,52 +9,128 @@ struct BstNode { BstNode* right; }; -BstNode* GetNewNode(int data){ - BstNode* newNode = new BstNode(); - newNode->data = data; - newNode->left = newNode->right = NULL; - return newNode; +BstNode* Insert(BstNode* node, int inserting_value) { + if (node == NULL) { // No node => make a new one! + node = new BstNode(); + node->data = inserting_value; + node->left = node->right = NULL; + + } + else if (inserting_value <= node->data) { + node->left = Insert(node->left,inserting_value); + } + else { + node->right = Insert(node->right,inserting_value); + } + + return node; } -BstNode* Insert(BstNode* root, int data) { - if (root == NULL) { - root = GetNewNode(data); - } else if (data <= root->data) { - root->left = Insert(root->left,data); + +bool Search(BstNode* node, int target) { + if (node == NULL){ + return false; + } else if (target == node->data) { // bingoo, we've got this! + return true; + } else if (target < node->data) { + return Search(node->left,target); } else { - root->right = Insert(root->right,data); + return Search(node->right,target); } + + } +BstNode* FindMin(BstNode* root) { + while(root->left != NULL){ + root = root->left; + } + return root; +} -bool Search(BstNode* root, int data) { +struct BstNode* Delete(struct BstNode* root, int data) { if (root == NULL){ - return false; - } else if (root->data == data) { - return true; - } else if (data <= root->data) { - return Search(root->left,data); - } else { - return Search(root->right,data); + return root; + } else if (data < root->data) + { + root->left = Delete(root->left,data); + } else if (data > root->data) + { + root->right = Delete(root->right,data); + } else // found the data to be deleted! + { + // case 1: No child + if (root->left == NULL && root->right == NULL) + { + delete root; + root = NULL; + } + // case 2: One child + else if (root->left == NULL) + { + struct BstNode *temp = root; + root = root->right; + delete temp; + } + else if (root->right == NULL) + { + struct BstNode *temp = root; + root = root->left; + delete temp; + } + // case 3: Two children + + else + { + struct BstNode *temp = FindMin(root->right); + root->data = temp->data; + root->right = Delete(root->right,temp->data); + } } + + return root; +} +// printing the tree +void Inorder(BstNode *root) { + if (root == NULL){ + return; + } + + Inorder(root->left); + printf("%d",root->data); + Inorder(root->right); } +int size(BstNode* node) { + + if (node = NULL) { + return 0; + } else { + return (size(node->left) + 1 /* 1 root */ + size(node->right)); + } + +} + int main() { + BstNode* root; // pointer to root node root = NULL; // tree is empty! - Insert(root,15); - Insert(root,10); - Insert(root,20); - Insert(root,25); - Insert(root,8); - Insert(root,12); - Insert(root,17); + root = Insert(root,15); + root = Insert(root,10); + root = Insert(root,20); + root = Insert(root,25); + root = Insert(root,8); + root = Insert(root,12); + root = Insert(root,17); + - int number; + + + /*int number; cout << "Please enter a number : \n"; cin >> number; @@ -62,7 +138,21 @@ int main() { cout << "Found! \n"; } else { cout << "Not Found! \n"; - } + }*/ + + /*root = Delete(root,15); + + + cout << "Inorder: "; + Inorder(root); + cout<<"\n"; + */ + + int num; + num = size(root); + size(root); + cout << "size : " << num << "\n"; + return 0; } diff --git a/MobileRobot/ObjectTrack/bst.cpp~ b/MobileRobot/ObjectTrack/bst.cpp~ index 020bcf0e1545504056a4c05b387eff795e0a2bc8..9e540f5550e05c7b6135500e3655927db6841f32 100644 --- a/MobileRobot/ObjectTrack/bst.cpp~ +++ b/MobileRobot/ObjectTrack/bst.cpp~ @@ -1,5 +1,5 @@ #include <iostream> - +#include <stdio.h> using namespace std; struct BstNode { @@ -9,60 +9,148 @@ struct BstNode { BstNode* right; }; -BstNode* GetNewNode(int data){ - BstNode* newNode = new BstNode(); - newNode->data = data; - newNode->left = newNode->right = NULL; - return newNode; +BstNode* Insert(BstNode* node, int inserting_value) { + if (node == NULL) { // No node => make a new one! + node = new BstNode(); + node->data = inserting_value; + node->left = node->right = NULL; + + } + else if (inserting_value <= node->data) { + node->left = Insert(node->left,inserting_value); + } + else { + node->right = Insert(node->right,inserting_value); + } + + return node; } -BstNode* Insert(BstNode* root, int data) { - if (root == NULL) { - root = GetNewNode(data); - } else if (data <= root->data) { - root->left = Insert(root->left,data); + +bool Search(BstNode* node, int target) { + if (node == NULL){ + return false; + } else if (target == node->data) { // bingoo, we've got this! + return true; + } else if (target < node->data) { + return Search(node->left,target); } else { - root->right = Insert(root->right,data); + return Search(node->right,target); } + + } +BstNode* FindMin(BstNode* root) { + while(root->left != NULL){ + root = root->left; + } + return root; +} -bool Search(BstNode* root, int data) { +struct BstNode* Delete(struct BstNode* root, int data) { if (root == NULL){ - return false; - } else if (root->data == data) { - return true; - } else if (data <= root->data) { - return Search(root->left,data); - } else { - return Search(root->right,data); + return root; + } else if (data < root->data) + { + root->left = Delete(root->left,data); + } else if (data > root->data) + { + root->right = Delete(root->right,data); + } else // found the data to be deleted! + { + // case 1: No child + if (root->left == NULL && root->right == NULL) + { + delete root; + root = NULL; + } + // case 2: One child + else if (root->left == NULL) + { + struct BstNode *temp = root; + root = root->right; + delete temp; + } + else if (root->right == NULL) + { + struct BstNode *temp = root; + root = root->left; + delete temp; + } + // case 3: Two children + + else + { + struct BstNode *temp = FindMin(root->right); + root->data = temp->data; + root->right = Delete(root->right,temp->data); + } } + + return root; +} - +void Inorder(BstNode *root) { + if (root == NULL){ + return; + } + + Inorder(root->left); + printf("%d",root->data); + Inorder(root->right); } +int size(BstNode* node) { + + if (node = NULL) { + return 0; + } else { + return (size(node->left) + 1 /* 1 root */ + size(node->right)); + } + +} + int main() { + BstNode* root; // pointer to root node root = NULL; // tree is empty! - Insert(root,15); - Insert(root,10); - Insert(root,20); - Insert(root,25); - Insert(root,8); - Insert(root,12); - Insert(root,17); + root = Insert(root,15); + root = Insert(root,10); + root = Insert(root,20); + root = Insert(root,25); + root = Insert(root,8); + root = Insert(root,12); + root = Insert(root,17); + - int number; + + + /*int number; cout << "Please enter a number : \n"; cin >> number; if (Search(root,number) == true) { - cout << "Found!"; + cout << "Found! \n"; } else { - cout << "Not Found!"; - } + cout << "Not Found! \n"; + }*/ + + /*root = Delete(root,15); + + + cout << "Inorder: "; + Inorder(root); + cout<<"\n"; + */ + + int num; + num = size(root); + size(root); + cout << "size : " << num << "\n"; + return 0; }