AVL Tree in C with iterative insertion2019 Community Moderator ElectionIs it a good idea to typedef pointers?The best way to calculate the height in a binary search tree? (balancing an AVL-tree)Improve INSERT-per-second performance of SQLite?How to check if my AVL tree implementation is correct?python AVL tree insertionDeletion in AVL TreeBalancing an AVL tree C++C: Creating a AVL balanced TreeInsertion function into AVL tree won't insertroot node does not rebalance avl treeBalance procedure of AVL Tree
Do I need an EFI partition for each 18.04 ubuntu I have on my HD?
Recursively updating the MLE as new observations stream in
Why is this tree refusing to shed its dead leaves?
Did Nintendo change its mind about 68000 SNES?
Can "few" be used as a subject? If so, what is the rule?
Animating wave motion in water
Why didn’t Eve recognize the little cockroach as a living organism?
Why do I have a large white artefact on the rendered image?
How are passwords stolen from companies if they only store hashes?
How can an organ that provides biological immortality be unable to regenerate?
Error in master's thesis, I do not know what to do
How do researchers send unsolicited emails asking for feedback on their works?
Jem'Hadar, something strange about their life expectancy
10 year ban after applying for a UK student visa
Have any astronauts/cosmonauts died in space?
What kind of footwear is suitable for walking in micro gravity environment?
Interior of Set Notation
CLI: Get information Ubuntu releases
Exit shell with shortcut (not typing exit) that closes session properly
is this saw blade faulty?
Is VPN a layer 3 concept?
Why are there no stars visible in cislunar space?
Pre-Employment Background Check With Consent For Future Checks
Do people actually use the word "kaputt" in conversation?
AVL Tree in C with iterative insertion
2019 Community Moderator ElectionIs it a good idea to typedef pointers?The best way to calculate the height in a binary search tree? (balancing an AVL-tree)Improve INSERT-per-second performance of SQLite?How to check if my AVL tree implementation is correct?python AVL tree insertionDeletion in AVL TreeBalancing an AVL tree C++C: Creating a AVL balanced TreeInsertion function into AVL tree won't insertroot node does not rebalance avl treeBalance procedure of AVL Tree
I'm coding a generic AVL tree as both a challenge to myself and, if I can do it properly, a resource for other CS students out there.
As one usually would, I started by implementing a recursive insertion function, which works. However, due to efficiency, I tried to implement the same function, but iteratively. I searched online and found lots of implementations, but the rest of the code was always too different from mine, which led me to continue attempting to create an implementation from scratch.
These are the relevant defined types:
typedef struct info
int id;
char name[200];
data;
typedef struct avl_node
void * data;
struct avl_node * left;
struct avl_node * right;
int height;
* avl_node_t;
typedef struct avl_tree
avl_node_t root;
int num_nodes;
int (*less)(const void * first, const void * second);
int (*key)(const void * data);
* avl_t;
And as follows is the iterative insertion function (which doesn't work as intended):
avl_node_t
avl_insert(avl_node_t node, void * data)
long key1 = 0, key2 = 0;
avl_node_t aux = NULL;
while (1)
if (node == NULL)
node = avl_node_create(data);
break;
key1 = key((void*)&data);
key2 = key((void*)&(node->data));
aux = node;
if (less((void*)&key1, (void*)&key2))
node = node->left;
else
node = node->right;
if (aux != NULL)
if (less((void*)&key1, (void*)&key2))
aux->right = node;
else if (less((void*)&key2, (void*)&key1))
aux->left = node;
node = avl_balance(node);
return node;
In which the avl_balance
function is defined as such:
static short
balance(avl_node_t node)
if (node == NULL)
return 0;
return avl_node_height(node->left) - avl_node_height(node->right);
static avl_node_t
avl_balance(avl_node_t node)
short balance_factor;
if (node == NULL)
return node;
balance_factor = balance(node);
if (balance_factor > 1)
if (balance(node->left) >= 0)
node = avl_rotRight(node);
else
node = avl_rotLeftRight(node);
else if (balance_factor < -1)
if (balance(node->right) <= 0)
node = avl_rotLeft(node);
else
node = avl_rotRightLeft(node);
else
update_height(node);
return node;
This is the code I'm using to test the AVL tree:
int main()
data d1 = 1, "we are number one" ;
data d2 = 2, "boneless pizza" ;
data d3 = 3, "hehe" ;
data d4 = 4, "achoo" ;
data d5 = 5, "I like C" ;
data d6 = 6, "Assembly is cool too" ;
data d7 = 7, "SIGSEGV" ;
avl_t tree = avl_create();
avl_node_t root = tree->root;
root = avl_insert(root, (void*)&d1);
traverse(root);
root = avl_insert(root, (void*)&d2);
traverse(root);
root = avl_insert(root, (void*)&d3);
root = avl_insert(root, (void*)&d4);
root = avl_insert(root, (void*)&d5);
root = avl_insert(root, (void*)&d6);
root = avl_insert(root, (void*)&d7);
traverse(root);
free(tree);
exit(0);
In which traverse
is defined as such:
void visit(void * d)
data * my_data = (data*)d;
printf("I am element number %d named %sn", (*my_data).id, (*my_data).name);
fflush(stdout);
void traverse(avl_node_t node)
if (node == NULL)
return;
traverse(node->left);
traverse(node->right);
visit(node->data);
And finally, this is the output I'm getting from this test:
I am element number 1 named we are number one
I am element number 2 named boneless pizza
I am element number 7 named SIGSEGV
Thank you in advance.
c data-structures avl-tree
|
show 4 more comments
I'm coding a generic AVL tree as both a challenge to myself and, if I can do it properly, a resource for other CS students out there.
As one usually would, I started by implementing a recursive insertion function, which works. However, due to efficiency, I tried to implement the same function, but iteratively. I searched online and found lots of implementations, but the rest of the code was always too different from mine, which led me to continue attempting to create an implementation from scratch.
These are the relevant defined types:
typedef struct info
int id;
char name[200];
data;
typedef struct avl_node
void * data;
struct avl_node * left;
struct avl_node * right;
int height;
* avl_node_t;
typedef struct avl_tree
avl_node_t root;
int num_nodes;
int (*less)(const void * first, const void * second);
int (*key)(const void * data);
* avl_t;
And as follows is the iterative insertion function (which doesn't work as intended):
avl_node_t
avl_insert(avl_node_t node, void * data)
long key1 = 0, key2 = 0;
avl_node_t aux = NULL;
while (1)
if (node == NULL)
node = avl_node_create(data);
break;
key1 = key((void*)&data);
key2 = key((void*)&(node->data));
aux = node;
if (less((void*)&key1, (void*)&key2))
node = node->left;
else
node = node->right;
if (aux != NULL)
if (less((void*)&key1, (void*)&key2))
aux->right = node;
else if (less((void*)&key2, (void*)&key1))
aux->left = node;
node = avl_balance(node);
return node;
In which the avl_balance
function is defined as such:
static short
balance(avl_node_t node)
if (node == NULL)
return 0;
return avl_node_height(node->left) - avl_node_height(node->right);
static avl_node_t
avl_balance(avl_node_t node)
short balance_factor;
if (node == NULL)
return node;
balance_factor = balance(node);
if (balance_factor > 1)
if (balance(node->left) >= 0)
node = avl_rotRight(node);
else
node = avl_rotLeftRight(node);
else if (balance_factor < -1)
if (balance(node->right) <= 0)
node = avl_rotLeft(node);
else
node = avl_rotRightLeft(node);
else
update_height(node);
return node;
This is the code I'm using to test the AVL tree:
int main()
data d1 = 1, "we are number one" ;
data d2 = 2, "boneless pizza" ;
data d3 = 3, "hehe" ;
data d4 = 4, "achoo" ;
data d5 = 5, "I like C" ;
data d6 = 6, "Assembly is cool too" ;
data d7 = 7, "SIGSEGV" ;
avl_t tree = avl_create();
avl_node_t root = tree->root;
root = avl_insert(root, (void*)&d1);
traverse(root);
root = avl_insert(root, (void*)&d2);
traverse(root);
root = avl_insert(root, (void*)&d3);
root = avl_insert(root, (void*)&d4);
root = avl_insert(root, (void*)&d5);
root = avl_insert(root, (void*)&d6);
root = avl_insert(root, (void*)&d7);
traverse(root);
free(tree);
exit(0);
In which traverse
is defined as such:
void visit(void * d)
data * my_data = (data*)d;
printf("I am element number %d named %sn", (*my_data).id, (*my_data).name);
fflush(stdout);
void traverse(avl_node_t node)
if (node == NULL)
return;
traverse(node->left);
traverse(node->right);
visit(node->data);
And finally, this is the output I'm getting from this test:
I am element number 1 named we are number one
I am element number 2 named boneless pizza
I am element number 7 named SIGSEGV
Thank you in advance.
c data-structures avl-tree
2
Note what it says in Is it a good idea to typedef pointers — TL;DR, the answer is generally "No".
– Jonathan Leffler
Mar 6 at 21:21
1
You're not using theless
orkey
members while inserting the data; and you don't pass pointers to initialize them to thecreate
function. Inside theinsert()
function, you are doing some funky stuff withkey1
andkey2
, completely subverting the generic code framework you have, and you callless
andkey
directly — they aren't the function pointer members of the structure. (This is not C++!)
– Jonathan Leffler
Mar 6 at 21:26
1
You don't showtraverse()
oravl_balance()
, so this isn't an MCVE (Minimal, Complete, and Verifiable example). And yourfree(tree)
is bound to leak a lot of allocated memory if you've inserted any data into the tree. You might well need a function pointer to free the data, too. Or maybe you need anapply()
function a bit liketraverse()
that can be called to free the data in each node.
– Jonathan Leffler
Mar 6 at 21:28
1
Of the various examples, the Efficient AVL tree - rosettacode.com has a decent iterative insert.
– David C. Rankin
Mar 6 at 22:01
1
Please note that we don't have copies of your working functions, so it is hard for us to know how they work. MCVE expands to include Complete — which means "can be run".
– Jonathan Leffler
Mar 10 at 1:45
|
show 4 more comments
I'm coding a generic AVL tree as both a challenge to myself and, if I can do it properly, a resource for other CS students out there.
As one usually would, I started by implementing a recursive insertion function, which works. However, due to efficiency, I tried to implement the same function, but iteratively. I searched online and found lots of implementations, but the rest of the code was always too different from mine, which led me to continue attempting to create an implementation from scratch.
These are the relevant defined types:
typedef struct info
int id;
char name[200];
data;
typedef struct avl_node
void * data;
struct avl_node * left;
struct avl_node * right;
int height;
* avl_node_t;
typedef struct avl_tree
avl_node_t root;
int num_nodes;
int (*less)(const void * first, const void * second);
int (*key)(const void * data);
* avl_t;
And as follows is the iterative insertion function (which doesn't work as intended):
avl_node_t
avl_insert(avl_node_t node, void * data)
long key1 = 0, key2 = 0;
avl_node_t aux = NULL;
while (1)
if (node == NULL)
node = avl_node_create(data);
break;
key1 = key((void*)&data);
key2 = key((void*)&(node->data));
aux = node;
if (less((void*)&key1, (void*)&key2))
node = node->left;
else
node = node->right;
if (aux != NULL)
if (less((void*)&key1, (void*)&key2))
aux->right = node;
else if (less((void*)&key2, (void*)&key1))
aux->left = node;
node = avl_balance(node);
return node;
In which the avl_balance
function is defined as such:
static short
balance(avl_node_t node)
if (node == NULL)
return 0;
return avl_node_height(node->left) - avl_node_height(node->right);
static avl_node_t
avl_balance(avl_node_t node)
short balance_factor;
if (node == NULL)
return node;
balance_factor = balance(node);
if (balance_factor > 1)
if (balance(node->left) >= 0)
node = avl_rotRight(node);
else
node = avl_rotLeftRight(node);
else if (balance_factor < -1)
if (balance(node->right) <= 0)
node = avl_rotLeft(node);
else
node = avl_rotRightLeft(node);
else
update_height(node);
return node;
This is the code I'm using to test the AVL tree:
int main()
data d1 = 1, "we are number one" ;
data d2 = 2, "boneless pizza" ;
data d3 = 3, "hehe" ;
data d4 = 4, "achoo" ;
data d5 = 5, "I like C" ;
data d6 = 6, "Assembly is cool too" ;
data d7 = 7, "SIGSEGV" ;
avl_t tree = avl_create();
avl_node_t root = tree->root;
root = avl_insert(root, (void*)&d1);
traverse(root);
root = avl_insert(root, (void*)&d2);
traverse(root);
root = avl_insert(root, (void*)&d3);
root = avl_insert(root, (void*)&d4);
root = avl_insert(root, (void*)&d5);
root = avl_insert(root, (void*)&d6);
root = avl_insert(root, (void*)&d7);
traverse(root);
free(tree);
exit(0);
In which traverse
is defined as such:
void visit(void * d)
data * my_data = (data*)d;
printf("I am element number %d named %sn", (*my_data).id, (*my_data).name);
fflush(stdout);
void traverse(avl_node_t node)
if (node == NULL)
return;
traverse(node->left);
traverse(node->right);
visit(node->data);
And finally, this is the output I'm getting from this test:
I am element number 1 named we are number one
I am element number 2 named boneless pizza
I am element number 7 named SIGSEGV
Thank you in advance.
c data-structures avl-tree
I'm coding a generic AVL tree as both a challenge to myself and, if I can do it properly, a resource for other CS students out there.
As one usually would, I started by implementing a recursive insertion function, which works. However, due to efficiency, I tried to implement the same function, but iteratively. I searched online and found lots of implementations, but the rest of the code was always too different from mine, which led me to continue attempting to create an implementation from scratch.
These are the relevant defined types:
typedef struct info
int id;
char name[200];
data;
typedef struct avl_node
void * data;
struct avl_node * left;
struct avl_node * right;
int height;
* avl_node_t;
typedef struct avl_tree
avl_node_t root;
int num_nodes;
int (*less)(const void * first, const void * second);
int (*key)(const void * data);
* avl_t;
And as follows is the iterative insertion function (which doesn't work as intended):
avl_node_t
avl_insert(avl_node_t node, void * data)
long key1 = 0, key2 = 0;
avl_node_t aux = NULL;
while (1)
if (node == NULL)
node = avl_node_create(data);
break;
key1 = key((void*)&data);
key2 = key((void*)&(node->data));
aux = node;
if (less((void*)&key1, (void*)&key2))
node = node->left;
else
node = node->right;
if (aux != NULL)
if (less((void*)&key1, (void*)&key2))
aux->right = node;
else if (less((void*)&key2, (void*)&key1))
aux->left = node;
node = avl_balance(node);
return node;
In which the avl_balance
function is defined as such:
static short
balance(avl_node_t node)
if (node == NULL)
return 0;
return avl_node_height(node->left) - avl_node_height(node->right);
static avl_node_t
avl_balance(avl_node_t node)
short balance_factor;
if (node == NULL)
return node;
balance_factor = balance(node);
if (balance_factor > 1)
if (balance(node->left) >= 0)
node = avl_rotRight(node);
else
node = avl_rotLeftRight(node);
else if (balance_factor < -1)
if (balance(node->right) <= 0)
node = avl_rotLeft(node);
else
node = avl_rotRightLeft(node);
else
update_height(node);
return node;
This is the code I'm using to test the AVL tree:
int main()
data d1 = 1, "we are number one" ;
data d2 = 2, "boneless pizza" ;
data d3 = 3, "hehe" ;
data d4 = 4, "achoo" ;
data d5 = 5, "I like C" ;
data d6 = 6, "Assembly is cool too" ;
data d7 = 7, "SIGSEGV" ;
avl_t tree = avl_create();
avl_node_t root = tree->root;
root = avl_insert(root, (void*)&d1);
traverse(root);
root = avl_insert(root, (void*)&d2);
traverse(root);
root = avl_insert(root, (void*)&d3);
root = avl_insert(root, (void*)&d4);
root = avl_insert(root, (void*)&d5);
root = avl_insert(root, (void*)&d6);
root = avl_insert(root, (void*)&d7);
traverse(root);
free(tree);
exit(0);
In which traverse
is defined as such:
void visit(void * d)
data * my_data = (data*)d;
printf("I am element number %d named %sn", (*my_data).id, (*my_data).name);
fflush(stdout);
void traverse(avl_node_t node)
if (node == NULL)
return;
traverse(node->left);
traverse(node->right);
visit(node->data);
And finally, this is the output I'm getting from this test:
I am element number 1 named we are number one
I am element number 2 named boneless pizza
I am element number 7 named SIGSEGV
Thank you in advance.
c data-structures avl-tree
c data-structures avl-tree
edited Mar 6 at 23:16
Saucy Goat
asked Mar 6 at 21:12
Saucy GoatSaucy Goat
847
847
2
Note what it says in Is it a good idea to typedef pointers — TL;DR, the answer is generally "No".
– Jonathan Leffler
Mar 6 at 21:21
1
You're not using theless
orkey
members while inserting the data; and you don't pass pointers to initialize them to thecreate
function. Inside theinsert()
function, you are doing some funky stuff withkey1
andkey2
, completely subverting the generic code framework you have, and you callless
andkey
directly — they aren't the function pointer members of the structure. (This is not C++!)
– Jonathan Leffler
Mar 6 at 21:26
1
You don't showtraverse()
oravl_balance()
, so this isn't an MCVE (Minimal, Complete, and Verifiable example). And yourfree(tree)
is bound to leak a lot of allocated memory if you've inserted any data into the tree. You might well need a function pointer to free the data, too. Or maybe you need anapply()
function a bit liketraverse()
that can be called to free the data in each node.
– Jonathan Leffler
Mar 6 at 21:28
1
Of the various examples, the Efficient AVL tree - rosettacode.com has a decent iterative insert.
– David C. Rankin
Mar 6 at 22:01
1
Please note that we don't have copies of your working functions, so it is hard for us to know how they work. MCVE expands to include Complete — which means "can be run".
– Jonathan Leffler
Mar 10 at 1:45
|
show 4 more comments
2
Note what it says in Is it a good idea to typedef pointers — TL;DR, the answer is generally "No".
– Jonathan Leffler
Mar 6 at 21:21
1
You're not using theless
orkey
members while inserting the data; and you don't pass pointers to initialize them to thecreate
function. Inside theinsert()
function, you are doing some funky stuff withkey1
andkey2
, completely subverting the generic code framework you have, and you callless
andkey
directly — they aren't the function pointer members of the structure. (This is not C++!)
– Jonathan Leffler
Mar 6 at 21:26
1
You don't showtraverse()
oravl_balance()
, so this isn't an MCVE (Minimal, Complete, and Verifiable example). And yourfree(tree)
is bound to leak a lot of allocated memory if you've inserted any data into the tree. You might well need a function pointer to free the data, too. Or maybe you need anapply()
function a bit liketraverse()
that can be called to free the data in each node.
– Jonathan Leffler
Mar 6 at 21:28
1
Of the various examples, the Efficient AVL tree - rosettacode.com has a decent iterative insert.
– David C. Rankin
Mar 6 at 22:01
1
Please note that we don't have copies of your working functions, so it is hard for us to know how they work. MCVE expands to include Complete — which means "can be run".
– Jonathan Leffler
Mar 10 at 1:45
2
2
Note what it says in Is it a good idea to typedef pointers — TL;DR, the answer is generally "No".
– Jonathan Leffler
Mar 6 at 21:21
Note what it says in Is it a good idea to typedef pointers — TL;DR, the answer is generally "No".
– Jonathan Leffler
Mar 6 at 21:21
1
1
You're not using the
less
or key
members while inserting the data; and you don't pass pointers to initialize them to the create
function. Inside the insert()
function, you are doing some funky stuff with key1
and key2
, completely subverting the generic code framework you have, and you call less
and key
directly — they aren't the function pointer members of the structure. (This is not C++!)– Jonathan Leffler
Mar 6 at 21:26
You're not using the
less
or key
members while inserting the data; and you don't pass pointers to initialize them to the create
function. Inside the insert()
function, you are doing some funky stuff with key1
and key2
, completely subverting the generic code framework you have, and you call less
and key
directly — they aren't the function pointer members of the structure. (This is not C++!)– Jonathan Leffler
Mar 6 at 21:26
1
1
You don't show
traverse()
or avl_balance()
, so this isn't an MCVE (Minimal, Complete, and Verifiable example). And your free(tree)
is bound to leak a lot of allocated memory if you've inserted any data into the tree. You might well need a function pointer to free the data, too. Or maybe you need an apply()
function a bit like traverse()
that can be called to free the data in each node.– Jonathan Leffler
Mar 6 at 21:28
You don't show
traverse()
or avl_balance()
, so this isn't an MCVE (Minimal, Complete, and Verifiable example). And your free(tree)
is bound to leak a lot of allocated memory if you've inserted any data into the tree. You might well need a function pointer to free the data, too. Or maybe you need an apply()
function a bit like traverse()
that can be called to free the data in each node.– Jonathan Leffler
Mar 6 at 21:28
1
1
Of the various examples, the Efficient AVL tree - rosettacode.com has a decent iterative insert.
– David C. Rankin
Mar 6 at 22:01
Of the various examples, the Efficient AVL tree - rosettacode.com has a decent iterative insert.
– David C. Rankin
Mar 6 at 22:01
1
1
Please note that we don't have copies of your working functions, so it is hard for us to know how they work. MCVE expands to include Complete — which means "can be run".
– Jonathan Leffler
Mar 10 at 1:45
Please note that we don't have copies of your working functions, so it is hard for us to know how they work. MCVE expands to include Complete — which means "can be run".
– Jonathan Leffler
Mar 10 at 1:45
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032224%2favl-tree-in-c-with-iterative-insertion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032224%2favl-tree-in-c-with-iterative-insertion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Note what it says in Is it a good idea to typedef pointers — TL;DR, the answer is generally "No".
– Jonathan Leffler
Mar 6 at 21:21
1
You're not using the
less
orkey
members while inserting the data; and you don't pass pointers to initialize them to thecreate
function. Inside theinsert()
function, you are doing some funky stuff withkey1
andkey2
, completely subverting the generic code framework you have, and you callless
andkey
directly — they aren't the function pointer members of the structure. (This is not C++!)– Jonathan Leffler
Mar 6 at 21:26
1
You don't show
traverse()
oravl_balance()
, so this isn't an MCVE (Minimal, Complete, and Verifiable example). And yourfree(tree)
is bound to leak a lot of allocated memory if you've inserted any data into the tree. You might well need a function pointer to free the data, too. Or maybe you need anapply()
function a bit liketraverse()
that can be called to free the data in each node.– Jonathan Leffler
Mar 6 at 21:28
1
Of the various examples, the Efficient AVL tree - rosettacode.com has a decent iterative insert.
– David C. Rankin
Mar 6 at 22:01
1
Please note that we don't have copies of your working functions, so it is hard for us to know how they work. MCVE expands to include Complete — which means "can be run".
– Jonathan Leffler
Mar 10 at 1:45