non recursive binary tree Hello I dont know what is wrong with my bst insert Any
ID: 3556825 • Letter: N
Question
non recursive binary tree
Hello I dont know what is wrong with my bst insert
Any suggestions it has too be non recursive it adds to the right always
Thanks
void InsertBST(LZWCmp cmp, TreeNode **root, int code) {
TreeNode tmp = *root;
TreeNode current = NULL;
Code temp;
Code temp1;
int size;
int comp;
int direction = -1;
if(*root == NULL)
root = CreateNode(code)
while(tmp != NULL) {
temp = GetCode(cmp->cst, tmp->cNum);
temp1 = GetCode(cmp->cst, code);
size = temp.size;
if (temp1.size < temp.size)
size = temp1.size;
comp = memcmp(temp1.data, temp.data, size);
if(temp1.size < temp.size && comp == 0)
comp = -1;
else if(temp1.size < temp.size && comp == 0)
comp = 1;
if(comp < 0) {
current = tmp;
direction = FALSE;
tmp = tmp->left;
}
else (
current = tmp;
direction = TRUE;
tmp = tmp->right;
}
}
if(direction == FALSE
current->left = CreateNode(code);
else
current->right = CreateNode(code);
}
Hello I dont know what is wrong with my bst insert
Any suggestions it has too be non recursive it adds to the right always
Thanks
void InsertBST(LZWCmp cmp, TreeNode **root, int code) {
TreeNode tmp = *root;
TreeNode current = NULL;
Code temp;
Code temp1;
int size;
int comp;
int direction = -1;
if(*root == NULL)
root = CreateNode(code)
while(tmp != NULL) {
temp = GetCode(cmp->cst, tmp->cNum);
temp1 = GetCode(cmp->cst, code);
size = temp.size;
if (temp1.size < temp.size)
size = temp1.size;
comp = memcmp(temp1.data, temp.data, size);
if(temp1.size < temp.size && comp == 0)
comp = -1;
else if(temp1.size < temp.size && comp == 0)
comp = 1;
if(comp < 0) {
current = tmp;
direction = FALSE;
tmp = tmp->left;
}
else (
current = tmp;
direction = TRUE;
tmp = tmp->right;
}
}
if(direction == FALSE
current->left = CreateNode(code);
else
current->right = CreateNode(code);
}
Hello I dont know what is wrong with my bst insert
Any suggestions it has too be non recursive it adds to the right always
Thanks
void InsertBST(LZWCmp cmp, TreeNode **root, int code) {
TreeNode tmp = *root;
TreeNode current = NULL;
Code temp;
Code temp1;
int size;
int comp;
int direction = -1;
if(*root == NULL)
root = CreateNode(code)
while(tmp != NULL) {
temp = GetCode(cmp->cst, tmp->cNum);
temp1 = GetCode(cmp->cst, code);
size = temp.size;
if (temp1.size < temp.size)
size = temp1.size;
comp = memcmp(temp1.data, temp.data, size);
if(temp1.size < temp.size && comp == 0)
comp = -1;
else if(temp1.size < temp.size && comp == 0)
comp = 1;
if(comp < 0) {
current = tmp;
direction = FALSE;
tmp = tmp->left;
}
else (
current = tmp;
direction = TRUE;
tmp = tmp->right;
}
}
if(direction == FALSE
current->left = CreateNode(code);
else
current->right = CreateNode(code);
}
Explanation / Answer
#include #include struct tree_el { int val; struct tree_el * right, * left; }; typedef struct tree_el node; void insert(node ** tree, node * item) { if(!(*tree)) { *tree = item; return; } if(item->valval) insert(&(*tree)->left, item); else if(item->val>(*tree)->val) insert(&(*tree)->right, item); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.