![]() |
|
#1
|
|||
|
|||
Deleting a node from binary search treeOK, general ? here. If one is deleting a node from a binary search tree that is within the tree and has lots of descendants, say the node with value 40 from this example:
CPP / C++ / C Code:
CPP / C++ / C Code:
Hope I've made myself clear. |
|
#2
|
||||
|
||||
Re: Deleting a node from binary search treeWhen you delete a node, if the left subtree is null, replace the node you're deleting with the root of the right subtree. If the left subtree is not null, replace the node with largest element of the left subtree. You can to do it by moving few pointers around. There's no need for temporary stacks or rebuilding the tree.
|
|
#3
|
|||
|
|||
Re: Deleting a node from binary search treeI've love it if someone would critique my "dele" function. I'm not sure I've covered all the possible scenarios, and I feel like I've really fumbled my way through this. I find it hard to visualize the way the pointers get shuffled around when deleting a node with two subtrees. Here's the header:
CPP / C++ / C Code:
CPP / C++ / C Code:
TIA for your constructive criticism |
|
#4
|
||||
|
||||
Re: Deleting a node from binary search treeYou should be able to to the erase by reassigning pointers only; It can be done without ever deleting a node. If you do delete a node, make sure you make its children NULL first. Your destructor should delete both the left and right children if they are not NULL.
You need to define a destructor. It should look something like this: CPP / C++ / C Code:
Defining a successor function would make your code cleaner. Here's one I wrote awhile back: CPP / C++ / C Code:
And here's the erase function, which uses the successor function and never needs to delete a node: CPP / C++ / C Code:
You may find this helpfull too: http://www.cs.vassar.edu/~cs241/slides/bst.pdf |
Recent GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| can anyone help me with my tree :) | bioeng_mtm | C++ Forum | 5 | 22-Apr-2006 12:50 |
| Binary Search Tree in C++ | rpg3 | C++ Forum | 5 | 02-Apr-2006 09:53 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 14:13 |
| printing binary search tree | nkhambal | C Programming Language | 2 | 26-Mar-2005 03:01 |
| Search Engine Positioning 101 and 201 "How To" Tips... | 000 | Search Engine Optimization Forum | 0 | 29-May-2003 10:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The