Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions ayesha/Basic Data Structures/Binary Tree/is_binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <iostream>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

class Node
{
public:
int data = 0;
Node *left = nullptr;
Node *right = nullptr;
Node(int data)
{
this->data = data;
}
};

class Pair
{
public:
Node *node = nullptr;
int state = 0;

Pair(Node *node, int state)
{
this->node = node;
this->state = state;
}
};

int idx = 0;
Node *constructTree(vector<int> &arr)
{

if (idx == arr.size() || arr[idx] == -1)
{
idx++;
return nullptr;
}
Node *node = new Node(arr[idx++]);
node->left = constructTree(arr);
node->right = constructTree(arr);
return node;
}

// Display function
void display(Node *node)
{
if (node == nullptr)
return;
string str = "";
str += node->left != nullptr ? to_string(node->left->data) : ".";
str += " <- " + to_string(node->data) + " -> ";
str += node->right != nullptr ? to_string(node->right->data) : ".";
cout << str << endl;

display(node->left);
display(node->right);
}

// Height function
int height(Node *node)
{
return node == nullptr ? -1 : max(height(node->left), height(node->right)) + 1;
}

int checkBST(Node *root, Node *min, Node *max)
{
if (root == NULL)
{
return 1;
}
if (min != NULL && root->data <= min->data)
{
return 0;
}
if (max != NULL && root->data >= max->data)
{
return 0;
}
int leftBST = checkBST(root->left, min, root);
int rightBST = checkBST(root->right, root, max);
return leftBST && rightBST;
}

int isbalance(Node *node)
{
// write your code here
return checkBST(node, NULL, NULL);
}

int main()
{
int n;
cin >> n;

vector<int> arr(n, 0);
for (int i = 0; i < n; i++)
{
string tmp;
cin >> tmp;
if (tmp == "n")
{
arr[i] = -1;
}
else
{
arr[i] = stoi(tmp);
}
}

Node *root = constructTree(arr);

int r = isbalance(root);
if (r == 1)
cout << "true";
else
cout << "false";
}
87 changes: 87 additions & 0 deletions ayesha/Basic Data Structures/Linked Lists/add-last-linked-list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <iostream>
using namespace std;

class node
{
public:
int data;
node *next;
};

class linked_list
{
public:
node *head, *tail;
int size = 0;

public:
linked_list()
{
head = NULL;
tail = NULL;
}

void addLast(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;

if (head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
size++;
}
void display()
{
for (node *tmp = head; tmp != NULL; tmp = tmp->next)
{
cout << tmp->data << " ";
}
}

void testList()
{
for (node *temp = head; temp != NULL; temp = temp->next)
{
cout << temp->data << endl;
}
cout << size << endl;

if (size > 0)
{
cout << tail->data << endl;
}
}
};

int main()
{

string str;
linked_list l;
while (true)
{
getline(cin, str);
if (str[0] == 'q')
{
break;
}
if (str[0] == 'a')
{
string ss = str.substr(8, 2);
int n = stoi(ss);
l.addLast(n);
}
}

l.testList();
return 0;
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;

int factorial(int n)
{
// write your code here
if (n == 0 || n == 1)
{
return 1;
}
return n * factorial(n - 1);
}

int main()
{
int n;
cin >> n;
cout << factorial(n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;

int power(int x, int n)
{
// write your code here
if (n == 0)
{
return 1;
}
return x * power(x, n - 1);
}

int main()
{
int n, x;
cin >> x >> n;
cout << power(x, n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;

int powerLogarithmic(int x, int n)
{
// write your code here
if (n == 0)
{
return 1;
}
int x1 = powerLogarithmic(x, n / 2);
int x2 = x1 * x1;
if (n % 2 == 1)
{
x2 = x2 * x;
}
return x2;
}

int main()
{
int x, n;
cin >> x >> n;
cout << powerLogarithmic(x, n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;

void printDecreasing(int n) {
// write your code here
if(n==0){
return;
}
cout<<n<<endl;
printDecreasing(n-1);
}

int main() {
int n;
cin >> n;
printDecreasing(n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
using namespace std;

void printIncDec(int n)
{
// write your code here
if (n == 0)
{
return;
}
cout << n << endl;
printIncDec(n - 1);
cout << n << endl;
}

int main()
{
int n;
cin >> n;
printIncDec(n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
using namespace std;

void printIncreasing(int n)
{
// write your code here
if (n == 0)
{
return;
}
printIncreasing(n - 1);
cout << n << endl;
}

int main()
{
int n;
cin >> n;
printIncreasing(n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;

void pzz(int n)
{
// write your code here
if (n == 0)
{
return;
}
cout << n << " ";
pzz(n - 1);
cout << n << " ";
pzz(n - 1);
cout << n << " ";
}

int main()
{
int n;
cin >> n;
pzz(n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;

void toh(int n, int t1id, int t2id, int t3id)
{
// write your code here
if (n == 1)
{
cout << "1[" << t1id << " -> " << t2id << "]" << endl;
return;
}
toh(n - 1, t1id, t3id, t2id);
cout << n << "[" << t1id << " -> " << t2id << "]" << endl;
toh(n - 1, t3id, t2id, t1id);
}

int main()
{

int n;
cin >> n;
int n1, n2, n3;
cin >> n1 >> n2 >> n3;
toh(n, n1, n2, n3);
}
Loading