Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Implement all challenges with the given file name a) Tree: Preorder Traversal

ID: 3595646 • Letter: 1

Question

1. Implement all challenges with the given file name

a) Tree: Preorder Traversal : preorder.cpp

/* you only have to complete the function given below.  
Node is defined as  

struct node
{
int data;
node* left;
node* right;
};

*/

void preOrder(node *root) {

}

b) Tree: Postorder Traversal : postorder.cpp

/* you only have to complete the function given below.  
Node is defined as  

struct node
{
int data;
node* left;
node* right;
};

*/


void postOrder(node *root) {

}

c) Tree: Inorder Traversal : inorder.cpp

/* you only have to complete the function given below.  
Node is defined as  

struct node
{
int data;
node* left;
node* right;
};

*/

void inOrder(node *root) {

}

d)  Binary Search Tree : Insertion : insertion.cpp

/*
Node is defined as

typedef struct node
{
int data;
node * left;
node * right;
}node;

*/


node * insert(node * root, int value) {


return root;
}

Tree Implementation

1. Implement all challenges with the given file name

a) Tree: Preorder Traversal : preorder.cpp

/* you only have to complete the function given below.  
Node is defined as  

struct node
{
int data;
node* left;
node* right;
};

*/

void preOrder(node *root) {

}

b) Tree: Postorder Traversal : postorder.cpp

/* you only have to complete the function given below.  
Node is defined as  

struct node
{
int data;
node* left;
node* right;
};

*/


void postOrder(node *root) {

}

c) Tree: Inorder Traversal : inorder.cpp

/* you only have to complete the function given below.  
Node is defined as  

struct node
{
int data;
node* left;
node* right;
};

*/

void inOrder(node *root) {

}

d)  Binary Search Tree : Insertion : insertion.cpp

/*
Node is defined as

typedef struct node
{
int data;
node * left;
node * right;
}node;

*/


node * insert(node * root, int value) {


return root;
}

Explanation / Answer

a) Preorder

void preOrder(node *root)

{

if(root == NULL) return;

cout<<root->data <<" ";

preOrder(root->left);

preOrder(root->right);

}

b) Postorder

void postOrder(node *root)

{

if(root == NULL) return;

postOrder(root->left);

postOrder(root->right);

cout<<root->data <<" ";

}

c) Inorder

void inOrder(node *root)

{

if(root == NULL) return;

inOrder(root->left);

cout<<root->data <<" ";

inOrder(root->right);

}

d) Binary Search Tree : Insertion

node *addNode(int val) //this function creates a node

{

node* node = (node*)malloc(sizeof(node));

node->data = val;

node->left = NULL;

node->right = NULL;

return node;

}

node * insert(node *root, int value)

{

if(root == NULL)

return addNode(value); //if root is null then creates a node

if(value < root->data)

root->left = nodeInsert(root->left, value); //creates node at left subtree if value is less than root

else

root->right = nodeInsert(root->right, value); //creates node at right subtree if value is greater than root

return root;

}