Question

I am stuck on a data structure problem, I am just going off of Geeks for...

I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;

//binary tree has data & left and right child
struct node{
int data;
struct node *left;
struct node *right;
};

//create a new node
struct node* newNode (int data){
struct node* temp = (struct node *) malloc( sizeof(struct node) );

temp->data = data;
temp->left = temp->right = NULL;

return temp;
}

//recursively creates tree
struct node* constructTreeUtil( int pre[], int* preIndex, int key,
int min, int max, int size ){
if( *preIndex >= size )
return NULL;

struct node* root = NULL;

if( key > min && key < max ){
//allocate memory
root = newNode ( key );
*preIndex = *preIndex + 1;

if (*preIndex < size){
//create subtree under root
root->left = constructTreeUtil( pre, preIndex, pre[*preIndex],
min, key, size );

//right subtree
root->right = constructTreeUtil( pre, preIndex, pre[*preIndex],
key, max, size );
}
}

return root;
}

struct node *constructTree (int pre[], int size){
int preIndex = 0;
return constructTreeUtil ( pre, &preIndex, pre[0], INT_MIN,
INT_MAX, size );
}

void printPreorder(node* node){
if(node==NULL){
return;
}
printf("%d ",node->data);
printPreorder(node->left);
printPreorder(node->right);
}

void printInorder (struct node* node){
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}

void printPostorder(struct node* node){
if(node==NULL){
return;
}
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}


int main (){
int pre[100];
//filling pre with random numbers
for(int i=0;i<100;i++){
pre[i]=rand()%200+1;
}

int size= sizeof(pre)/sizeof(pre[0]);

struct node *root = constructTree(pre, size);
printf("Preorder traversal of the constructed tree: \n");
printPreorder(root);
printf("\n");
printf("Inorder traversal of the constructed tree: \n");
printInorder(root);
printf("\n");
printf("Postorder traversal of the constructed tree: \n");
printPostorder(root);
printf("\n");

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
 *  C - Program to illustrate Traversals in Tree
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>

//binary tree has data & left and right child
struct node
{
  int data;
  struct node *left;
  struct node *right;
};

//create a new node
struct node* newNode (int data)
{
  struct node* temp = (struct node *) malloc( sizeof(struct node) );

  temp->data = data;
  temp->left = temp->right = NULL;

  return temp;
}

void constructTreeUtil(struct node* root, int post[], int start, int end)
{
   if (start >= end)
      return;

   int i;
   for (i = start; i < end; ++i) 
      if (post[i] > root->data)
         break;
   
   // left
   if (i > start) {
      root->left = newNode(post[i - 1]);
      constructTreeUtil(root->left, post, start, i - 1);
   }

   // right
   if (i >= start) {
      root->right = newNode(post[end - 1]);
      constructTreeUtil(root->right, post, i, end - 1);
   }
}

struct node* constructTree(int post[], int size)
{
   struct node* root = newNode(post[size - 1]);

   constructTreeUtil(root, post, 0, size - 1);

   return root;
}

void printPreorder(struct node* node)
{
  if(node==NULL)
  {
    return;
  }

  printf("%d ",node->data);
  printPreorder(node->left);
  printPreorder(node->right);
}

void printInorder (struct node* node)
{
  if (node == NULL)
  return;
  printInorder(node->left);
  printf("%d ", node->data);
  printInorder(node->right);
}

void printPostorder(struct node* node)
{
  if(node == NULL)
  {
    return;
  }
  printPostorder(node->left);
  printPostorder(node->right);
  printf("%d ", node->data);
}


int main ()
{
  int pre[100];
  
  srand(time(NULL));

  //filling pre with random numbers
  for(int i = 0; i < 100; i++)
  {
    pre[i] = rand()%200 + 1;
  }

  int size = sizeof(pre)/sizeof(pre[0]);

  struct node *root = constructTree(pre, size);
  printf("Preorder traversal of the constructed tree: \n");
  printPreorder(root);
  printf("\n");
  printf("Inorder traversal of the constructed tree: \n");
  printInorder(root);
  printf("\n");
  printf("Postorder traversal of the constructed tree: \n");
  printPostorder(root);
  printf("\n");

  return 0;
}
/*  Program ends here */

Add a comment
Know the answer?
Add Answer to:
I am stuck on a data structure problem, I am just going off of Geeks for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • ^^^ Q3. I am trying to implement double linked list but I was failed to write...

    ^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function   THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node {      int info;      struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) {      DL *left,*right,*curr;      curr = start;      if( start == NULL)       {                 printf("\nDoubly...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • PLEASE implement all the empty methods in BTree.c (only four functions) in C. #include #include struct...

    PLEASE implement all the empty methods in BTree.c (only four functions) in C. #include #include struct Node { int value; struct Node* leftChild; struct Node* rightChild; }; struct bTree { struct Node * root; }; //please implement all the four functions struct bTree* newTree (int value) { return NULL } int add (struct bTree* root, int value) { return 1; } //you need to use free() to release the memory when a node is removed int removeNode (struct bTree* root,...

  • Please help me with FLOWCHART and UML diagram for class, thank you! #include <iostream> #include <fstream>...

    Please help me with FLOWCHART and UML diagram for class, thank you! #include <iostream> #include <fstream> #define MAX 10 using namespace std; class WordListTree { public:    // Structure of a node    struct Node {        string key;        // Create an array of up to MAX children        Node* child[MAX];    };    // Create a tree of strings    Node* newNode(string key) {        Node* temp = new Node;        (*temp).key =...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Here is a function for some code im working on in C. Right now it accepts...

    Here is a function for some code im working on in C. Right now it accepts 2 different user inputs and stores them as city and distance. What I want is for the user to input only once. For example "Enter the city name and distance from previous city: Arlington 200". I need to separate that string into values for city and distance. I think I need to use tokens but im not sure how. void addCity () { char...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • In c++, what alternative to malloc line? Such as struct Node* newNode(int data) { struct Node*...

    In c++, what alternative to malloc line? Such as struct Node* newNode(int data) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL;    return node; }

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT