Question
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started