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
77 changes: 77 additions & 0 deletions BinaryTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Program to print binary tree in 2D
#include<stdio.h>
#define COUNT 10

// A binary tree node
struct Node
{
int data;
Node* left, *right;
};

// Helper function to allocates a new node
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}

// Function to print binary tree in 2D
// It does reverse inorder traversal
void print2DUtil(Node *root, int space)
{
// Base case
if (root == NULL)
return;

// Increase distance between levels
space += COUNT;

// Process right child first
print2DUtil(root->right, space);

// Print current node after space
// count
printf("\n");
for (int i = COUNT; i < space; i++)
printf(" ");
printf("%d\n", root->data);

// Process left child
print2DUtil(root->left, space);
}

// Wrapper over print2DUtil()
void print2D(Node *root)
{
// Pass initial space count as 0
print2DUtil(root, 0);
}

// Driver program to test above functions
int main()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);

root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);

root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(10);
root->left->right->right = newNode(11);
root->right->left->left = newNode(12);
root->right->left->right = newNode(13);
root->right->right->left = newNode(14);
root->right->right->right = newNode(15);

print2D(root);

return 0;
}
71 changes: 71 additions & 0 deletions LinkedListLoop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// C++ program to detect loop in a linked list
#include<bits/stdc++.h>
using namespace std;

/* Link list node */
struct Node
{
int data;
struct Node* next;
};

void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node;

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

// Returns true if there is a loop in linked list
// else returns false.
bool detectLoop(struct Node *h)
{
unordered_set<Node *> s;
while (h != NULL)
{
// If this node is already present
// in hashmap it means there is a cycle
// (Because you we encountering the
// node for the second time).
if (s.find(h) != s.end())
return true;

// If we are seeing the node for
// the first time, insert it in hash
s.insert(h);

h = h->next;
}

return false;
}

/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;

push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);

/* Create a loop for testing */
head->next->next->next->next = head;

if (detectLoop(head))
cout << "Loop found";
else
cout << "No Loop";

return 0;
}