Binary tree c remove node

Binary tree c remove node

By: turdus Date of post: 14.07.2017

I'm currently working on a school project where I have to write a few helper functions for Binary Search Trees. One of the functions removes a node from the tree.

Insertion & Deletion in a Binary Search Tree Using C#

I'm trying to run some test cases but I can't seem to get them to work. I know the problem has something do with how I'm using pointers, but I'm not quite sure where I'm going wrong. I didn't include the leftRoot function, but it's fairly simple and I know it does what it's supposed to return the leftmost root in a subtree Here is the part of the code my professor gave us that tests the remove function:. Since everything besides the '11' is no longer attached to tree, I'm fairly certain that something in my program is assigning pointers where they shouldn't be assigned and tree nodes are just getting lost in the void.

One piece of information I forgot to provide, the deleted node's left-most child is supposed to replace the deleted node. Your function returns an int , corroborated by a number of return 1 or return 0 statements Which means that you're assigning addresses 0 and 1 to the left and right nodes?

Binary Search Tree Delete Node() - C++ Forum

This seems very odd to me and is likely causing problems for you. Draw a diagram for your pointers.

I find diagrams like this or this help me most. It is not embarrassing and will help you understand what you're writing. It is important to visualize these things, particularly when you're just learning.

Binary Search Trees - Deleting Nodes - Part 2 - C++ - How to Delete Nodes from a Binary Search Tree

I've tried to fix the code up a little. I will admit that I didn't spend as much time as I possibly should have proof-reading it but it should be ok enough to give you an idea about the solution. But it should help you get onto the right path. By posting your answer, you agree to the privacy policy and terms of service.

Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list.

Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us. Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7.

binary tree c remove node

Join them; it only takes a minute: Deleting a node in a Binary Search Tree - C Ask Question. Here is the code: I didn't include the leftRoot function, but it's fairly simple and I know it does what it's supposed to return the leftmost root in a subtree Here is the part of the code my professor gave us that tests the remove function: Should all the children from that node drop off, or should the left-most child be brought up?

Ah, i apologize for not providing that.

binary tree c remove node

The node's in order successor should replace the deleted node. The same applies for all recursive calls. Otherwise, you are passing the address of current. Rankin Nov 17 '15 at 3: I recommend taking a look at the diagram in this tutorial and drawing it for yourself.

It is the best way to wrap your head around the code you're writing pointers to pointers.

Binary search tree - Wikipedia

Multiple indirection is tricky. Then read this and you should be sorted. I've outlined a couple things in my answer though I'm not sure that I've found all of the issues in your code but here is one major one: And yet you do this: Since you've opted for recursion let me see if I can help you fix this somewhat You bring up a good point.

Ah, that makes sense, can't believe I missed that. The problem now is probably how I'm handling the reassigning of children after a parent node has been deleted.

Sign up or log in StackExchange. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Stack Overflow works best with JavaScript enabled. What should happen when you delete a node? MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system