-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27ae187
commit 074d178
Showing
3 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
#include <algorithm> | ||
|
||
// Implementation of binary search tree | ||
class Node { | ||
|
||
public: | ||
int value; | ||
int height; | ||
Node *left; | ||
Node *right; | ||
|
||
// constructor | ||
Node(int value) { | ||
this->value = value; | ||
this->left = nullptr; | ||
this->right = nullptr; | ||
this->height = 0; | ||
} | ||
}; | ||
|
||
// Creating new nodes, the first'll be the root | ||
Node *newNode(int n) { | ||
return new Node(n); | ||
} | ||
|
||
// insert nodes | ||
void insert(Node *&root, int n) { | ||
|
||
// if the root is empty we create a new tree, n being root | ||
if(root == nullptr) { | ||
newNode(n); | ||
return; | ||
} | ||
|
||
// else, check if n is greater than or lesser than actual node | ||
Node *tmp = root; | ||
|
||
// check the left case | ||
if(n < tmp->value) { | ||
if(tmp->left == nullptr) { | ||
tmp->left = newNode(n); | ||
return; | ||
} else | ||
insert(tmp->left,n); | ||
} else if(n > tmp->value) // check the right case | ||
if(tmp->right == nullptr) { | ||
tmp->right = newNode(n); | ||
return; | ||
} else | ||
insert(tmp->right,n); | ||
|
||
} | ||
|
||
// printing tree values in pre-order method | ||
void print(Node *root) { | ||
|
||
// base case | ||
if(root == nullptr) | ||
return; | ||
|
||
// recursion calls | ||
std::cout << root->value << " "; | ||
print(root->left); | ||
print(root->right); | ||
|
||
} | ||
|
||
// Balancing Binary tree | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
int main(void){ | ||
|
||
Node *tree = newNode(12); | ||
insert(tree,9); | ||
insert(tree,7); | ||
insert(tree,10); | ||
insert(tree,13); | ||
insert(tree,14); | ||
|
||
print(tree); | ||
|
||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
|
||
// i is the digit that want | ||
// the order is right to left | ||
int extract_digit(int n, int i) { | ||
|
||
if (i == 1) | ||
return n%10; | ||
|
||
return extract_digit(n/10,i-1); | ||
|
||
} | ||
|
||
int main(void){ | ||
|
||
std::cout << extract_digit(1223456789,2); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <list> | ||
|
||
// Singled linked list | ||
class Node { | ||
public: | ||
int data; | ||
int size = 0; | ||
Node *next; | ||
|
||
// methods | ||
void head(Node *head) { | ||
if(head != nullptr) | ||
std::cout << head->data << std::endl; | ||
else | ||
std::cout << "NULL" << std::endl; | ||
} | ||
|
||
void tail(Node *head) { | ||
|
||
if(head == nullptr) { | ||
std::cout << "NULL" << std::endl; | ||
return; | ||
} | ||
|
||
Node *tmp = head; | ||
while(tmp->next != nullptr) | ||
tmp = tmp->next; | ||
|
||
std::cout << tmp->data << std::endl; | ||
|
||
} | ||
|
||
int getSize(Node *head) { | ||
Node *tmp = head; | ||
while(tmp->next != nullptr) { | ||
head->size++; | ||
tmp = tmp->next; | ||
} | ||
return head->size; | ||
} | ||
|
||
|
||
// constructor | ||
Node(int data) { | ||
this->data = data; | ||
next = nullptr; | ||
} | ||
}; | ||
|
||
// Creating a new node | ||
// initialy, adding to the first position, but this function | ||
// is called to add at the final | ||
void addFinal(Node *&head, int n) { | ||
|
||
Node *newNode = new Node(n); | ||
Node *tmp = head; | ||
while(tmp->next != nullptr) | ||
tmp = tmp->next; | ||
|
||
tmp->next = newNode; | ||
|
||
} | ||
|
||
// Adding elements at the first position | ||
void addFirst(Node *&head, int n) { | ||
|
||
if(head == nullptr) { | ||
addFinal(head,n); | ||
return; | ||
} | ||
|
||
Node *newNode = new Node(n); | ||
Node *tmp = head; | ||
head = newNode; | ||
head->next = tmp; | ||
|
||
} | ||
|
||
// Adding element at an i position | ||
void addAt(Node *&head, int n, int i) { | ||
|
||
if(head == NULL) | ||
return; | ||
|
||
Node *tmp = head; | ||
int index = 0; | ||
while(index < i-1) { | ||
tmp = tmp->next; | ||
index++; | ||
} | ||
|
||
Node *tmp2 = tmp->next; | ||
Node *newNode = new Node(n); | ||
tmp->next = newNode; | ||
newNode->next = tmp2; | ||
|
||
} | ||
|
||
// Function to print list | ||
void printList(Node *head) { | ||
|
||
Node *tmp = head; | ||
while(tmp != nullptr) { | ||
std::cout << tmp->data << "->"; | ||
tmp = tmp->next; | ||
} | ||
std::cout << "NULL" << std::endl; | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
|
||
// Statically lists | ||
Node n1(1); | ||
Node n2(2); | ||
n1.next = &n2; | ||
|
||
// Dinamically lists | ||
Node *n3 = new Node(3); | ||
Node *n4 = new Node(4); | ||
n3->next = n4; | ||
|
||
//Creating a linked list with n nodes | ||
Node *node = new Node(10); | ||
addFinal(node,12); | ||
addFinal(node,122); | ||
addFinal(node,1112); | ||
addFinal(node,3412); | ||
addFinal(node,2); | ||
|
||
printList(node); | ||
|
||
std::cout << "Atual head: "; | ||
node->head(node); | ||
|
||
std::cout << "Atual tail: "; | ||
node->tail(node); | ||
|
||
addFirst(node,20); | ||
addAt(node,1,2); | ||
|
||
printList(node); | ||
std::cout << node->getSize(node) << std::endl; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|