Relative Content

Tag Archive for cdata-structuresbinary-tree

Why using else if instead of if here when building a binary tree?

When building the insertion function in binary tree, I use two “if ” statement to track the node( if the value is smaller than the current node,go left;if bigger,go right) . I know the difference between “else if” and “if” before. So initially the result I suppose is both fine with “else if” and with “if” in this condition. I choose “if”. But eventually it turns out it only works for “else if”. When using “if” result shows “segmentation fault”.
Here is the code segment that is correct, the incorrect version changes the “elseif” to “if”.