Skip to content

Commit

Permalink
i like good formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Oct 29, 2023
1 parent ff2b971 commit f1bac01
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 153 deletions.
4 changes: 2 additions & 2 deletions Data-Structures/Trees/01-intro.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions Data-Structures/Trees/02-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Data-Structures/Trees/04-in-order-recur.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
8 9 10
```

### Implemantation :
### Implementation :

```cpp
void inorder(int node){
void inorder(int node) {
if (node == NULL) { // base case
return;
}
Expand Down
6 changes: 3 additions & 3 deletions Data-Structures/Trees/05-pre-order-recur.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down
6 changes: 3 additions & 3 deletions Data-Structures/Trees/06-post-order-recur.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
38 changes: 21 additions & 17 deletions Data-Structures/Trees/07-level-order.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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.
Expand All @@ -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<vector<int>> levelOrder(TreeNode *root){
vector<vector<int>> ans; // vector of vector
if (root == NULL) return ans;
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>>and; // vector of vector
if (root == NULL)
return and;
queue<TreeNode*> q; // queue
q.push(root);
while(!q.empty()){
while (!q.empty()) {
int size = q.size();
vector<int> 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;
}
};
```
51 changes: 26 additions & 25 deletions Data-Structures/Trees/08-in-order-iterative.md
Original file line number Diff line number Diff line change
@@ -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 :**

Expand Down Expand Up @@ -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<int> inorderTraversal(TreeNode* root){

stack<TreeNode*> st; // declare a stack
vector<int> 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<int> inorderTraversal(TreeNode* root) {

stack<TreeNode*> st; // declare a stack
vector<int>and; // 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;
}
};
```
23 changes: 12 additions & 11 deletions Data-Structures/Trees/09-pre-order-iterative.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,50 @@
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<int> preorder(TreeNode *root) {
public:
vector<int> preorder(TreeNode* root) {

vectro<int> ans;
if (root == NULL) return ans;
vectro<int>and;
if (root == NULL)
return and;

stack<TreeNode*> 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);
}
if (root->left != NULL) {
st.push(root->left);
}

}
return ans;
return and;
}
};
```
26 changes: 14 additions & 12 deletions Data-Structures/Trees/10-post-order-twoStack.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> postOrderTwoStack(TreeNode *root) {
public:
vector<int> postOrderTwoStack(TreeNode* root) {

vector<int> ans;
if (root == NULL) return ans;
vector<int>and;
if (root == NULL)
return and;

stack<TreeNode*> 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;
}
}
```
Loading

0 comments on commit f1bac01

Please sign in to comment.