Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So for an assignment in C I need to make a Binart search tree, however each node is essentially the root for a binary search

So for an assignment in C I need to make a Binart search tree, however each node is essentially the root for a binary search tree that can be added to that. The example is that You have several shoe shops stored in a BST but each shop carries different shoes, so once the shop is made I don't understand how to use that shop root (node) and make a new tree under each shop that will hold the shoe name and a left and right pointer connecting other shoes. would I need to make another insert function? I know Ill have to free memory but for now I am trying to figure out the insertion of the other nodes. Please Add To this so that I am Able to understand how I would add subnodes for subtrees , Thank you

#include #include #include

typedef struct mainNode {

char* name;// Shop Name struct mainNode* left; struct mainNode* right; struct regNode* root; // Pointer To Sub Node } MainTreeNode;

typedef struct regNode{ char * name;// Shoe Name struct regNode *left; struct regNode *right; } regNode;

MainTreeNode* insert(MainTreeNode* root, char* name);

void inOrder(MainTreeNode* root);

int main() { // Create tree MainTreeNode* MainTreeRoot = NULL; MainTreeRoot = insert(MainTreeRoot ,"nike"); MainTreeRoot = insert(MainTreeRoot, "adidas"); MainTreeRoot = insert(MainTreeRoot, "underArmour"); MainTreeRoot = insert(MainTreeRoot, "rebbok");

inOrder(MainTreeRoot); printf(" ");

return 0; }

// Inserts a new node into the tree rooted at root with data set to value. // and returns a pointer to the root of the resulting tree.

MainTreeNode* insert(MainTreeNode* root,char* name) { // Inserting into an empty tree. if (root == NULL) { MainTreeNode* temp = malloc(sizeof(MainTreeNode)); temp->name = name; temp->left = NULL; temp->right = NULL; temp->root = NULL; return temp; } // Go left if (strcmp(name,root->name) < 0) root->left = insert(root->left,name); // Go right else root->right = insert(root->right, name); // Must return the root of this tree return root; }

void inOrder(MainTreeNode* root){ if (root != NULL) { inOrder(root->left); printf("%s ", root->name); inOrder(root->right); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

Explain the chemical properties of acids with examples.

Answered: 1 week ago

Question

Write the properties of Group theory.

Answered: 1 week ago