Merge pull request #29 from Naman-Bhalla/naman-pr

Include Data Structures and start with Binary Trees
This commit is contained in:
Praveen 2017-08-08 13:46:46 +05:30 committed by GitHub
commit ac47eabc52
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/* Includes structure for a node and a newNode() function which
can be used to create a new node in the tree.
It is assumed that the data in nodes will be an integer, though
function can be modified according to the data type, easily.
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *leftNode;
int data;
struct node *rightNode;
};
struct node *newNode(int data)
{
struct node *node = (struct node *)malloc(sizeof(struct node));
node->leftNode = NULL;
node->data = data;
node->rightNode = NULL;
return node;
}
int main(void)
{
/* new node can be created here as :-
struct node *nameOfNode = newNode(data);
and tree can be formed by creating further nodes at
nameOfNode->leftNode and so on.
*/
return 0;
}

View File

@ -0,0 +1,45 @@
/* Includes the functions for Recursive Traversals
of a Binary Tree. It is assumed that nodes and
tree have been created as per create_node.c
*/
#include <stdio.h>
void inOrderTraversal(struct node *node)
{
if(node == NULL) //if tree is empty
return;
inOrderTraversal(node->leftNode);
printf("\t%d\t", node->data);
inOrderTraversal(node->rightNode);
}
void preOrderTraversal(struct node *node)
{
if(node == NULL) //if tree is empty
return;
printf("\t%d\t", node->data);
preOrderTraversal(node->leftNode);
preOrderTraversal(node->rightNode);
}
void postOrderTraversal(struct node *node)
{
if(node == NULL) //if tree is empty
return;
postOrderTraversal(node->leftNode);
postOrderTraversal(node->rightNode);
printf("\t%d\t",node->data);
}
int main(void)
{
/* traversals can be done by simply invoking the
function with a pointer to the root node.
*/
return 0;
}