Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tree traversal #452

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Empty file removed Data Structures/.gitkeep
Empty file.
Empty file modified Data Structures/Stack/C++/Test/test
100755 → 100644
Empty file.
76 changes: 38 additions & 38 deletions Data Structures/Stack/Python/stack.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
class Stack:
def __init__(self, size = 1000000):
self.stack = []
self.stack_size = size
# Add element to stack
def push(self, element):
# Checking stack is full or not
if len(self.stack) <= self.stack_size:
self.stack.append(element)
else:
print('Stack Full. Overflow')
# Delete element from stack
def pop(self):
if len(self.stack) == 0:
print('Empty Stack. Underflow')
else:
self.stack.pop()
# Returns top element of stack
def top(self):
return self.stack[-1]
# Driver Program
if __name__ == '__main__':
stack = Stack()
stack.pop()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.top())
stack.pop()
print(stack.top())


class Stack:

def __init__(self, size = 1000000):
self.stack = []
self.stack_size = size

# Add element to stack
def push(self, element):
# Checking stack is full or not
if len(self.stack) <= self.stack_size:
self.stack.append(element)
else:
print('Stack Full. Overflow')

# Delete element from stack
def pop(self):
if len(self.stack) == 0:
print('Empty Stack. Underflow')
else:
self.stack.pop()

# Returns top element of stack
def top(self):
return self.stack[-1]


# Driver Program
if __name__ == '__main__':
stack = Stack()
stack.pop()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.top())
stack.pop()
print(stack.top())
48 changes: 48 additions & 0 deletions Data Structures/treeTraversalRecur/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>

#include "stack.h"
#include "tree.h"

node *getNode(tree *c)
{
node *nn = (node *)malloc(sizeof(node));
nn->next = NULL;
nn->top = c;
return nn;
}

int isempty(node *st)
{
if (st == NULL)
return 1;
return 0;
}

void push(tree *adr, node **st)
{
node *nn = getNode(adr);
nn->next = (*st);
(*st) = nn;
}

void pop(node **st)
{
if (isempty(*st))
{
printf("Stack empty can't pop");
}
node *temp = (*st);
(*st) = (*st)->next;
free(temp);
}

void display(node *st)
{
while (!isempty(st))
{
printf("%c ", st->top);
st = st->next;
}
printf("\n");
}
18 changes: 18 additions & 0 deletions Data Structures/treeTraversalRecur/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef STACK_HEADER
#define STACK_HEADER

#include "tree.h"

typedef struct Node
{
tree *top;
struct Node *next;
} node;

node *getNode(tree *c);
void display(node *st);
int isempty(node *st);
void push(tree *adr, node **st);
void pop(node **st);

#endif
59 changes: 59 additions & 0 deletions Data Structures/treeTraversalRecur/tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>

#include "tree.h"

tree *getTreeNode(int data)
{
tree *nn = (tree *)malloc(sizeof(tree));
nn->left = nn->right = NULL;
nn->data = data;
return nn;
}

tree *insert(tree *root, int data)
{
if (!root)
{
return getTreeNode(data);
}
else if (root->data < data)
{
root->right = insert(root->right, data);
}
else
{
root->left = insert(root->left, data);
}
return root;
}

void inorder(tree *root)
{
if (root)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void preorder(tree *root)
{
if (root)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

void postorder(tree *root)
{
if (root)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
16 changes: 16 additions & 0 deletions Data Structures/treeTraversalRecur/tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TREE_HEADER
#define TREE_HEADER

typedef struct Tree
{
int data;
struct Tree *left, *right;
} tree;

tree *insert(tree *root, int data);
void inorder(tree *root);
void postorder(tree *root);
void preorder(tree *root);
void inorder(tree *root);

#endif
60 changes: 60 additions & 0 deletions Data Structures/treeTraversalRecur/treeTraversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// to run type "gcc treeTraversal.c tree.c stack.c"
// then run the executable file and enter your input

#include <stdio.h>

#include "tree.h"
#include "stack.h"

void printstack(node *stack)
{
printf("stack: ");
while (!isempty(stack))
{
printf("%p ", stack->top->data);
}
printf("\n");
}

int main()
{
tree *root = NULL;

int n, val;
printf("Enter number of nodes: ");
scanf("%d", &n);

for (int i = 0; i < n; i++)
{
printf("Enter node %d: ", i+1);
scanf("%d", &val);
root = insert(root, val);
}

//using binary search tree.
//for an input of 2 1 4 3 5
// 2
// / \
// 1 3
// / \
// 4 5

// inorder: 1 2 3 4 5
// preorder: 2 1 4 3 5
// postorder: 1 3 5 4 2


printf("inorder: ");
inorder(root);
printf("\n");

printf("preorder: ");
preorder(root);
printf("\n");

printf("postorder: ");
postorder(root);
printf("\n");

return 0;
}
56 changes: 28 additions & 28 deletions Dynamic Programming/Fibonacci Sequence/Fibonacci_series.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Program to display the Fibonacci sequence up to n-th term where n is provided by the user
# change this value for a different result
nterms = int (input("enter the no. of terms " ))
# uncomment to take input from the user
#nterms = int(input("How many terms? "))
# first two terms
n1 = 0
n2 = 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence upto",nterms,":")
while count < nterms:
print(n1,end=' , ')
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
# Program to display the Fibonacci sequence up to n-th term where n is provided by the user

# change this value for a different result
nterms = int (input("enter the no. of terms " ))

# uncomment to take input from the user
#nterms = int(input("How many terms? "))

# first two terms
n1 = 0
n2 = 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence upto",nterms,":")
while count < nterms:
print(n1,end=' , ')
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
10 changes: 5 additions & 5 deletions Dynamic Programming/jan-ken-puzzle/python/test/heavy/01.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
4 4
2 3 3 3
1 3 2 3
2 2 1 2
1 2 1 1
4 4
2 3 3 3
1 3 2 3
2 2 1 2
1 2 1 1
10 changes: 5 additions & 5 deletions Dynamic Programming/jan-ken-puzzle/python/test/heavy/02.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
4 5
1 1 2 2 1
1 3 1 1 1
3 3 1 3 3
0 0 3 2 0
4 5
1 1 2 2 1
1 3 1 1 1
3 3 1 3 3
0 0 3 2 0
10 changes: 5 additions & 5 deletions Dynamic Programming/jan-ken-puzzle/python/test/heavy/03.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
4 5
1 2 1 3 1
2 2 3 3 1
2 1 3 0 3
1 1 1 0 2
4 5
1 2 1 3 1
2 2 3 3 1
2 1 3 0 3
1 1 1 0 2
10 changes: 5 additions & 5 deletions Dynamic Programming/jan-ken-puzzle/python/test/heavy/04.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
4 5
2 3 1 3 1
3 1 2 2 2
2 3 1 3 2
1 3 2 1 0
4 5
2 3 1 3 1
3 1 2 2 2
2 3 1 3 2
1 3 2 1 0
10 changes: 5 additions & 5 deletions Dynamic Programming/jan-ken-puzzle/python/test/heavy/05.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
4 5
3 1 2 2 1
1 2 2 2 1
1 2 1 1 3
3 1 3 3 2
4 5
3 1 2 2 1
1 2 2 2 1
1 2 1 1 3
3 1 3 3 2
Loading