This works because binary search trees have more structure than ordinary binary trees; here, I'm just using the binary search tree property of the left child being smaller, and the right child being larger than the current node's key. After doing some further research on the topic, I found that reconstructing the tree in preorder traversal order is much simpler. The recursive function doing that is implemented here and here , respectively.
The current minimum and maximum values are defined by the position inside the tree left child: less than parent, right child: greater than parent. If you want to store the tree node in a array,you had better to start from 1 position of your array!
So the relation between the parent and its children should be simple:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 5 months ago. Active 2 years, 7 months ago. Viewed 20k times. Am I overthinking this problem? Can there be a Recursion? Improve this question.
Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Arrays. Most visited in Java. We use cookies to ensure you have the best browsing experience on our website. Start Your Coding Journey Now!
You can always avoid using recursion by providing a dynamic stack data structure and an appropriate loop. Your indentation is broken and the scope of statements is unclear at several places my comments starting with!!!
Here's the fully fixed version I'm not sure that will provide exactly the same logic as you currently have it :. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Making a binary tree using an array in C Ask Question. Asked 4 years, 8 months ago. Active 4 years, 8 months ago. Viewed 5k times. Would very much appreciate the help. This is what I had so far and it's working but I don't really like it.
When you pass tree[2] you really want the function to use either tree[5] or tree[6] in the next recursive call. However, since you start by setting parent to zero, your next recursive call will us either tree[3] or tree[4]. Conclusion If you want to use recursion, you need to track the actual array index of the current node. Simply passing a pointer to the current node is not sufficient. That said - you should also spend some time considering whether recursion is really a good solution for this task.
You can avoid recursion and do it iteratively. Note tree is actually an integer array of size size. In the function insert we pass a pointer to the array.
Assuming the array is initialized with 0 s:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Implementing Binary search tree using array in C Ask Question.
0コメント