From f1bac0140366426c7be8fbcd47f442999512d8fb Mon Sep 17 00:00:00 2001 From: Rishabh <53911515+Rishabh672003@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:25:33 +0530 Subject: [PATCH] i like good formatting --- Data-Structures/Trees/01-intro.md | 4 +- Data-Structures/Trees/02-basics.md | 8 +-- Data-Structures/Trees/04-in-order-recur.md | 4 +- Data-Structures/Trees/05-pre-order-recur.md | 6 +-- Data-Structures/Trees/06-post-order-recur.md | 6 +-- Data-Structures/Trees/07-level-order.md | 38 +++++++------ .../Trees/08-in-order-iterative.md | 51 +++++++++--------- .../Trees/09-pre-order-iterative.md | 23 ++++---- .../Trees/10-post-order-twoStack.md | 26 ++++----- .../Trees/11-post-order-oneStack.md | 46 ++++++++-------- .../Trees/problems/count-leaves.cpp | 48 ++++++++--------- .../Trees/problems/sum-of-left-leaves.cpp | 54 +++++++++---------- 12 files changed, 161 insertions(+), 153 deletions(-) diff --git a/Data-Structures/Trees/01-intro.md b/Data-Structures/Trees/01-intro.md index 16ea5c1..7e30753 100644 --- a/Data-Structures/Trees/01-intro.md +++ b/Data-Structures/Trees/01-intro.md @@ -1,8 +1,8 @@ -## Tress - A hierarchical Data Struture +## Tress - A hierarchical Data Structure Trees are hierarchical data structures consisting of nodes. -A tree consits of root node, parent nodes, children nodes, leaf-nodes, sub-tree and ancestors. +A tree consists of root node, parent nodes, children nodes, leaf-nodes, sub-tree and ancestors. ``` 1 <-- root node diff --git a/Data-Structures/Trees/02-basics.md b/Data-Structures/Trees/02-basics.md index f50c285..07dc610 100644 --- a/Data-Structures/Trees/02-basics.md +++ b/Data-Structures/Trees/02-basics.md @@ -15,10 +15,10 @@ ```cpp struct Node { int data; - struct Node *left; - struct Node *right; + struct Node* left; + struct Node* right; // constructor - Node(int value){ + Node(int value) { data = value; left = right = NULL; // initially pointing to null } @@ -27,7 +27,7 @@ struct Node { // Tree Creation int main() { // calling constructor and passing root's value. - struct Node *root = new Node(1); + struct Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); diff --git a/Data-Structures/Trees/04-in-order-recur.md b/Data-Structures/Trees/04-in-order-recur.md index 8a0b5c6..c27f044 100644 --- a/Data-Structures/Trees/04-in-order-recur.md +++ b/Data-Structures/Trees/04-in-order-recur.md @@ -10,10 +10,10 @@ 8 9 10 ``` -### Implemantation : +### Implementation : ```cpp -void inorder(int node){ +void inorder(int node) { if (node == NULL) { // base case return; } diff --git a/Data-Structures/Trees/05-pre-order-recur.md b/Data-Structures/Trees/05-pre-order-recur.md index aeecec2..de296ed 100644 --- a/Data-Structures/Trees/05-pre-order-recur.md +++ b/Data-Structures/Trees/05-pre-order-recur.md @@ -10,15 +10,15 @@ 8 9 10 ``` -### Implemantation : +### Implementation : ```cpp -void preorder(int node){ +void preorder(int node) { if (node == NULL) { // base case return; } cout << node->data << endl; - preorder(node->left); // recursion + preorder(node->left); // recursion preorder(node->right); // recursion } ``` diff --git a/Data-Structures/Trees/06-post-order-recur.md b/Data-Structures/Trees/06-post-order-recur.md index 228f665..0bac0e2 100644 --- a/Data-Structures/Trees/06-post-order-recur.md +++ b/Data-Structures/Trees/06-post-order-recur.md @@ -10,14 +10,14 @@ 8 9 10 ``` -### Implemantation : +### Implementation : ```cpp -void postorder(int node){ +void postorder(int node) { if (node == NULL) { // base case return; } - postorder(node->left); // recursion + postorder(node->left); // recursion postorder(node->right); // recursion cout << node->data << endl; } diff --git a/Data-Structures/Trees/07-level-order.md b/Data-Structures/Trees/07-level-order.md index 81cbaa4..c9638f1 100644 --- a/Data-Structures/Trees/07-level-order.md +++ b/Data-Structures/Trees/07-level-order.md @@ -1,6 +1,6 @@ ## Level Order Traversal [BFS] C++ Implementation -==> We'll need two data sructures, A queue and a vector of vectors. +==> We'll need two data structures, A queue and a vector of vectors. --> The Queue will hold the roots of tree. @@ -94,8 +94,8 @@ The vector becomes : ### Approach : -- Declare a vector of vectors (ans). -- if root == NULL, return ans. +- Declare a vector of vectors (and). +- if root == NULL, return and. - declare a queue, push the root into it. - while the queue is not empty, - calculate it's size to traverse. @@ -105,44 +105,48 @@ The vector becomes : - pop the node out of the queue and check if it's left and right exists. - If exists, push them to queue - push the node's value into the (level) vector. -- push the (level) vector to (ans) vector. +- push the (level) vector to (and) vector. --- ### Implementation : ```cpp -/* Defination for binary tree : +/* Definition for binary tree : struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(x), left(nullptr), right(nullptr) {} TreeNode(int x): val(x), left(nullptr), right(nullptr) {} - TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), right(right) {} + TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), +right(right) {} } */ class Solution { -public: - vector> levelOrder(TreeNode *root){ - vector> ans; // vector of vector - if (root == NULL) return ans; + public: + vector> levelOrder(TreeNode* root) { + vector>and; // vector of vector + if (root == NULL) + return and; queue q; // queue q.push(root); - while(!q.empty()){ + while (!q.empty()) { int size = q.size(); vector level; - for (int i = 0; i < size; i++){ - TreeNode *node = q.front(); + for (int i = 0; i < size; i++) { + TreeNode* node = q.front(); q.pop(); - if (node->left != NULL) q.push(node->left); - if (node->right != NULL) q.push(node->right); + if (node->left != NULL) + q.push(node->left); + if (node->right != NULL) + q.push(node->right); level.push_back(node->value); } - ans.push_back(level); + and.push_back(level); } - return ans; + return and; } }; ``` diff --git a/Data-Structures/Trees/08-in-order-iterative.md b/Data-Structures/Trees/08-in-order-iterative.md index f03c506..10b4567 100644 --- a/Data-Structures/Trees/08-in-order-iterative.md +++ b/Data-Structures/Trees/08-in-order-iterative.md @@ -1,6 +1,6 @@ ## In order traversal ~ Iterative Approach ---> In this approach, we'll need an auxillary stack space and the stack will hold the upcoming nodes. +--> In this approach, we'll need an auxiliary stack space and the stack will hold the upcoming nodes. **Approach :** @@ -73,40 +73,41 @@ Stack becomes : ### Implementation : ```cpp -/* Defination for binary tree : +/* Definition for binary tree : struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(x), left(nullptr), right(nullptr) {} TreeNode(int x): val(x), left(nullptr), right(nullptr) {} - TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), right(right) {} + TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), +right(right) {} } */ class Solution { -public: - vector inorderTraversal(TreeNode* root){ - - stack st; // declare a stack - vector ans; // declare an answer vector - - TreeNode* node = root; // take the root node - - while(true){ - if(node != NULL){ // if not null - st.push(node); // push into the stack - node = node->left; // go to left - } - else { - if (st.empty() == true) break; - node = st.top(); // if null, - st.pop(); // pop the node out - ans.push_back(node->val); // push it to ans vector - node = node->right; // now check for right - } + public: + vector inorderTraversal(TreeNode* root) { + + stack st; // declare a stack + vectorand; // declare an answer vector + + TreeNode* node = root; // take the root node + + while (true) { + if (node != NULL) { // if not null + st.push(node); // push into the stack + node = node->left; // go to left + } else { + if (st.empty() == true) + break; + node = st.top(); // if null, + st.pop(); // pop the node out + and.push_back(node->val); // push it to and vector + node = node->right; // now check for right + } + } + return and; } - return ans; - } }; ``` diff --git a/Data-Structures/Trees/09-pre-order-iterative.md b/Data-Structures/Trees/09-pre-order-iterative.md index d4d2b17..f4bc8cd 100644 --- a/Data-Structures/Trees/09-pre-order-iterative.md +++ b/Data-Structures/Trees/09-pre-order-iterative.md @@ -6,39 +6,41 @@ 2. Start the Stack iteration, pop the top most element out and push it to the answer vector, then check if it has any left or right. 3. If left or right not NULL, first push right then left into the stack. 4. The stack iteration continues, pop the top most (left) element out and push it to the answer vector, then check if it has any left or right. -5. Continue the iteration untill the stak gets empty. +5. Continue the iteration until the stak gets empty. ![pre-order-itr](https://github.com/Rishabh672003/Programming-Notes/assets/53911515/eb403e15-29ca-410a-913b-b9f79822c59a) ### Implementation : ```cpp -/* Defination for binary tree : +/* Definition for binary tree : struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(x), left(nullptr), right(nullptr) {} TreeNode(int x): val(x), left(nullptr), right(nullptr) {} - TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), right(right) {} + TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), +right(right) {} } */ class Solution { -public: - vector preorder(TreeNode *root) { + public: + vector preorder(TreeNode* root) { - vectro ans; - if (root == NULL) return ans; + vectroand; + if (root == NULL) + return and; stack st; st.push(root); - while(!st.empty()){ + while (!st.empty()) { root = st.top(); st.pop(); - ans.push_back(root->val); + and.push_back(root->val); if (root->right != NULL) { st.push(root->right); @@ -46,9 +48,8 @@ public: if (root->left != NULL) { st.push(root->left); } - } - return ans; + return and; } }; ``` diff --git a/Data-Structures/Trees/10-post-order-twoStack.md b/Data-Structures/Trees/10-post-order-twoStack.md index e7cf321..f50c939 100644 --- a/Data-Structures/Trees/10-post-order-twoStack.md +++ b/Data-Structures/Trees/10-post-order-twoStack.md @@ -17,47 +17,49 @@ ### Implementation ```cpp -/* Defination for binary tree : +/* Definition for binary tree : struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(x), left(nullptr), right(nullptr) {} TreeNode(int x): val(x), left(nullptr), right(nullptr) {} - TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), right(right) {} + TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), +right(right) {} } */ class Solution { -public: - vector postOrderTwoStack(TreeNode *root) { + public: + vector postOrderTwoStack(TreeNode* root) { - vector ans; - if (root == NULL) return ans; + vectorand; + if (root == NULL) + return and; stack st1, st2; st1.push(root); - while(!st1.empty()){ + while (!st1.empty()) { root = st1.top(); st1.pop(); st2.push(root); - if (root->left != NULL){ + if (root->left != NULL) { st1.push(root->left); } - if (root->right != NULL){ + if (root->right != NULL) { st1.push(root->right); } } - while(!st2.empty()){ - ans.push_back(st2.top()->val); + while (!st2.empty()) { + and.push_back(st2.top()->val); st2.pop(); } - return ans; + return and; } } ``` diff --git a/Data-Structures/Trees/11-post-order-oneStack.md b/Data-Structures/Trees/11-post-order-oneStack.md index 8e84521..0cf36cf 100644 --- a/Data-Structures/Trees/11-post-order-oneStack.md +++ b/Data-Structures/Trees/11-post-order-oneStack.md @@ -1,47 +1,47 @@ ## Post Order Traversal ~ One Stack Iteration ```cpp -/* Defination for binary tree : +/* Definition for binary tree : struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(x), left(nullptr), right(nullptr) {} TreeNode(int x): val(x), left(nullptr), right(nullptr) {} - TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), right(right) {} + TreeNode(int x, TreeNode *left, TreeNode *right): val(x), left(left), +right(right) {} } */ class Solution { -public: - vector postOrderOneStack(TreeNode *root){ - vector ans; - if (root == NULL) return ans; + public: + vector postOrderOneStack(TreeNode* root) { + vectorand; + if (root == NULL) + return and; - TreeNode *curr = root; - TreeNode *temp = NULL; + TreeNode* curr = root; + TreeNode* temp = NULL; stack st; - while(curr != NULL || !st.empty()){ - if (curr != NULL){ + while (curr != NULL || !st.empty()) { + if (curr != NULL) { st.push(curr); curr = curr->left; - } - else { - temp = st.top()->right; - if (temp == NULL) { - temp = st.top(); - st.pop(); - ans.push_back(temp); - while (!st.empty() && temp = st.top()->right){ + } else { + temp = st.top()->right; + if (temp == NULL) { temp = st.top(); st.pop(); - ans.push_back(temp); + and.push_back(temp); + while (!st.empty()&& temp = st.top()->right) { + temp = st.top(); + st.pop(); + and.push_back(temp); + } + } else { + curr = temp; } - } - else { - curr = temp; - } } } } diff --git a/Data-Structures/Trees/problems/count-leaves.cpp b/Data-Structures/Trees/problems/count-leaves.cpp index 524573a..3ecd300 100644 --- a/Data-Structures/Trees/problems/count-leaves.cpp +++ b/Data-Structures/Trees/problems/count-leaves.cpp @@ -2,40 +2,40 @@ using namespace std; struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class Solution { -public: - int countLeaves(TreeNode *root) { + public: + int countLeaves(TreeNode* root) { - if (root == NULL) - return 0; - if (root->left == NULL && root->right == NULL) - return 1; + if (root == NULL) + return 0; + if (root->left == NULL && root->right == NULL) + return 1; - int leftLeaves = countLeaves(root->left); - int rightLeaves = countLeaves(root->right); + int leftLeaves = countLeaves(root->left); + int rightLeaves = countLeaves(root->right); - return leftLeaves + rightLeaves; - } + return leftLeaves + rightLeaves; + } }; int main() { - // Create a sample binary tree - TreeNode *root = new TreeNode(3); - root->left = new TreeNode(9); - root->right = new TreeNode(20); - root->right->left = new TreeNode(15); - root->right->right = new TreeNode(7); + // Create a sample binary tree + TreeNode* root = new TreeNode(3); + root->left = new TreeNode(9); + root->right = new TreeNode(20); + root->right->left = new TreeNode(15); + root->right->right = new TreeNode(7); - Solution solution; + Solution solution; - int result = solution.countLeaves(root); - cout << "Sum of left leaves: " << result << std::endl; + int result = solution.countLeaves(root); + cout << "Sum of left leaves: " << result << std::endl; - return 0; + return 0; } diff --git a/Data-Structures/Trees/problems/sum-of-left-leaves.cpp b/Data-Structures/Trees/problems/sum-of-left-leaves.cpp index 468ebb0..a632902 100644 --- a/Data-Structures/Trees/problems/sum-of-left-leaves.cpp +++ b/Data-Structures/Trees/problems/sum-of-left-leaves.cpp @@ -2,44 +2,44 @@ using namespace std; struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class Solution { -public: - int sumOfLeftLeaves(TreeNode *root) { - if (root == NULL) - return 0; + public: + int sumOfLeftLeaves(TreeNode* root) { + if (root == NULL) + return 0; - int sum = 0; + int sum = 0; - // checking if the root has a left leaf - if (root->left != NULL && root->left->left == NULL && - root->left->right == NULL) { - sum += root->left->val; - } + // checking if the root has a left leaf + if (root->left != NULL && root->left->left == NULL && + root->left->right == NULL) { + sum += root->left->val; + } - sum += sumOfLeftLeaves(root->left); - sum += sumOfLeftLeaves(root->right); + sum += sumOfLeftLeaves(root->left); + sum += sumOfLeftLeaves(root->right); - return sum; - } + return sum; + } }; int main() { - TreeNode *root = new TreeNode(3); - root->left = new TreeNode(9); - root->right = new TreeNode(20); - root->right->left = new TreeNode(15); - root->right->right = new TreeNode(7); + TreeNode* root = new TreeNode(3); + root->left = new TreeNode(9); + root->right = new TreeNode(20); + root->right->left = new TreeNode(15); + root->right->right = new TreeNode(7); - Solution solution; + Solution solution; - int result = solution.sumOfLeftLeaves(root); - std::cout << "Sum of left leaves: " << result << std::endl; + int result = solution.sumOfLeftLeaves(root); + std::cout << "Sum of left leaves: " << result << std::endl; - return 0; + return 0; }