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
  • 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);...

  • 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...

  • 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,...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

  • 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...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • 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; }

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
ADVERTISEMENT