From baead15a5ead347adefd01f7b1447682086c2b60 Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Thu, 28 Oct 2021 21:25:03 +0530 Subject: [PATCH 01/10] Create QuadTree.cpp --- QuadTree.cpp | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 QuadTree.cpp diff --git a/QuadTree.cpp b/QuadTree.cpp new file mode 100644 index 00000000..887bd96f --- /dev/null +++ b/QuadTree.cpp @@ -0,0 +1,231 @@ +// C++ Implementation of Quad Tree +#include +#include +using namespace std; + +// Used to hold details of a point +struct Point +{ + int x; + int y; + Point(int _x, int _y) + { + x = _x; + y = _y; + } + Point() + { + x = 0; + y = 0; + } +}; + +// The objects that we want stored in the quadtree +struct Node +{ + Point pos; + int data; + Node(Point _pos, int _data) + { + pos = _pos; + data = _data; + } + Node() + { + data = 0; + } +}; + +// The main quadtree class +class Quad +{ + // Hold details of the boundary of this node + Point topLeft; + Point botRight; + + // Contains details of node + Node *n; + + // Children of this tree + Quad *topLeftTree; + Quad *topRightTree; + Quad *botLeftTree; + Quad *botRightTree; + +public: + Quad() + { + topLeft = Point(0, 0); + botRight = Point(0, 0); + n = NULL; + topLeftTree = NULL; + topRightTree = NULL; + botLeftTree = NULL; + botRightTree = NULL; + } + Quad(Point topL, Point botR) + { + n = NULL; + topLeftTree = NULL; + topRightTree = NULL; + botLeftTree = NULL; + botRightTree = NULL; + topLeft = topL; + botRight = botR; + } + void insert(Node*); + Node* search(Point); + bool inBoundary(Point); +}; + +// Insert a node into the quadtree +void Quad::insert(Node *node) +{ + if (node == NULL) + return; + + // Current quad cannot contain it + if (!inBoundary(node->pos)) + return; + + // We are at a quad of unit area + // We cannot subdivide this quad further + if (abs(topLeft.x - botRight.x) <= 1 && + abs(topLeft.y - botRight.y) <= 1) + { + if (n == NULL) + n = node; + return; + } + + if ((topLeft.x + botRight.x) / 2 >= node->pos.x) + { + // Indicates topLeftTree + if ((topLeft.y + botRight.y) / 2 >= node->pos.y) + { + if (topLeftTree == NULL) + topLeftTree = new Quad( + Point(topLeft.x, topLeft.y), + Point((topLeft.x + botRight.x) / 2, + (topLeft.y + botRight.y) / 2)); + topLeftTree->insert(node); + } + + // Indicates botLeftTree + else + { + if (botLeftTree == NULL) + botLeftTree = new Quad( + Point(topLeft.x, + (topLeft.y + botRight.y) / 2), + Point((topLeft.x + botRight.x) / 2, + botRight.y)); + botLeftTree->insert(node); + } + } + else + { + // Indicates topRightTree + if ((topLeft.y + botRight.y) / 2 >= node->pos.y) + { + if (topRightTree == NULL) + topRightTree = new Quad( + Point((topLeft.x + botRight.x) / 2, + topLeft.y), + Point(botRight.x, + (topLeft.y + botRight.y) / 2)); + topRightTree->insert(node); + } + + // Indicates botRightTree + else + { + if (botRightTree == NULL) + botRightTree = new Quad( + Point((topLeft.x + botRight.x) / 2, + (topLeft.y + botRight.y) / 2), + Point(botRight.x, botRight.y)); + botRightTree->insert(node); + } + } +} + +// Find a node in a quadtree +Node* Quad::search(Point p) +{ + // Current quad cannot contain it + if (!inBoundary(p)) + return NULL; + + // We are at a quad of unit length + // We cannot subdivide this quad further + if (n != NULL) + return n; + + if ((topLeft.x + botRight.x) / 2 >= p.x) + { + // Indicates topLeftTree + if ((topLeft.y + botRight.y) / 2 >= p.y) + { + if (topLeftTree == NULL) + return NULL; + return topLeftTree->search(p); + } + + // Indicates botLeftTree + else + { + if (botLeftTree == NULL) + return NULL; + return botLeftTree->search(p); + } + } + else + { + // Indicates topRightTree + if ((topLeft.y + botRight.y) / 2 >= p.y) + { + if (topRightTree == NULL) + return NULL; + return topRightTree->search(p); + } + + // Indicates botRightTree + else + { + if (botRightTree == NULL) + return NULL; + return botRightTree->search(p); + } + } +}; + +// Check if current quadtree contains the point +bool Quad::inBoundary(Point p) +{ + return (p.x >= topLeft.x && + p.x <= botRight.x && + p.y >= topLeft.y && + p.y <= botRight.y); +} + +// Driver program +int main() +{ + Quad center(Point(0, 0), Point(8, 8)); + Node a(Point(1, 1), 1); + Node b(Point(2, 5), 2); + Node c(Point(7, 6), 3); + center.insert(&a); + center.insert(&b); + center.insert(&c); + cout << "Node a: " << + center.search(Point(1, 1))->data << "\n"; + cout << "Node b: " << + center.search(Point(2, 5))->data << "\n"; + cout << "Node c: " << + center.search(Point(7, 6))->data << "\n"; + cout << "Non-existing node: " + << center.search(Point(5, 5)); + return 0; +} From 3b932a7616e889a856f8462a3a6d719284d55f08 Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:52:59 +0530 Subject: [PATCH 02/10] Create Tree. Cpp --- Tree. Cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Tree. Cpp diff --git a/Tree. Cpp b/Tree. Cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/Tree. Cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 478c13075618932fe8e0227a598efe7f64a5d869 Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:55:50 +0530 Subject: [PATCH 03/10] Create Linked List. cpp --- Linked List. cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Linked List. cpp diff --git a/Linked List. cpp b/Linked List. cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/Linked List. cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 7d9fab37d7dccaae2e0d91ff8b20f0c2288da4a1 Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:56:49 +0530 Subject: [PATCH 04/10] Create TowerOfHanooi. cpp --- TowerOfHanooi. cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 TowerOfHanooi. cpp diff --git a/TowerOfHanooi. cpp b/TowerOfHanooi. cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/TowerOfHanooi. cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 955a41eba207a1ac106c59f82ad1dcf837c7851c Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:57:51 +0530 Subject: [PATCH 05/10] Create JoitSector.cpp --- JoitSector.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 JoitSector.cpp diff --git a/JoitSector.cpp b/JoitSector.cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/JoitSector.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 3c5564308d4e5aeef886c7f569d6fd006dcf87d5 Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:58:17 +0530 Subject: [PATCH 06/10] Create ForceNew.cpp --- ForceNew.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ForceNew.cpp diff --git a/ForceNew.cpp b/ForceNew.cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/ForceNew.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 3a99080e502bf9ce9abb14ad1d293f08da5aed9a Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 20:59:08 +0530 Subject: [PATCH 07/10] Create kitucode.cpp --- kitucode.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 kitucode.cpp diff --git a/kitucode.cpp b/kitucode.cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/kitucode.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 49b4384a3fb21457cb6d50fcd25f2c4dd5374abc Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:06:30 +0530 Subject: [PATCH 08/10] Create Saurabh.cpp --- Saurabh.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Saurabh.cpp diff --git a/Saurabh.cpp b/Saurabh.cpp new file mode 100644 index 00000000..3c41dff5 --- /dev/null +++ b/Saurabh.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; + cout<<"bshs"; +} From b1e41ac8e303cc4b0a0cf5526d51831004085c7c Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:38:54 +0530 Subject: [PATCH 09/10] Create Zerox.cpp --- Zerox.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Zerox.cpp diff --git a/Zerox.cpp b/Zerox.cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/Zerox.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +} From 792a7a0b03b4c25bac0c554ede69f7acb6bf9699 Mon Sep 17 00:00:00 2001 From: nirbhaykumar780890 <93337897+nirbhaykumar780890@users.noreply.github.com> Date: Sun, 31 Oct 2021 22:42:23 +0530 Subject: [PATCH 10/10] Create Pkp.cpp --- Pkp.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Pkp.cpp diff --git a/Pkp.cpp b/Pkp.cpp new file mode 100644 index 00000000..e6770811 --- /dev/null +++ b/Pkp.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +/* A linked list node */ +struct Node { + int data; + struct Node* next; +}; + +// Utility function to create a new Node +struct Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->next = NULL; + return temp; +}