Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Deleting Within A Tree

Posted on 1998-06-25
Medium Priority
230 Views
Need help with code that will enable the user to delete a node within a tree of words.
0
Question by:John500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 22

Accepted Solution

nietod earned 160 total points
ID: 1166653
I assume this if for me.  right?  If you send me a final copy.  (If we are there yet.)  I'll paste it in here.
0

LVL 22

Expert Comment

ID: 1166654
The final code:
*********************
void Probe::hook(Node *ptr,BinTree *tree, Node *par, Node *cur)
{
if(_top == 0)
{
tree->_root = ptr;
}
else if(cur == par->_left)
{
par->_left = ptr;
}
else
{
par->_right = ptr;
}
_trail[_top] = ptr;
}

void Probe::deleteCurrent(BinTree *tree)
{ // delete current node: trail [top]
if(_top < 0)
return;  // empty tree

Node *par, *cur = _trail[_top];  // nicer name for current node

tree->_numWords -= cur->_count;

if(_top > 0)
par = _trail[_top - 1];  //parent if current node has one

if(!cur -> _left) // has no left child, might have right child
{
hook(cur->_right,tree,par,cur);
if(!_trail[_top]) _top--;  // node gone, back up
}

else if (!cur->_right)  // has no right child, does have left child
{
hook(cur->_left,tree,par,cur);
}

else  // cur has both children present
{
// find inorder successor to cur
Node * p = cur->_right;
if(!p->_left)  // already at inorder successor
{
p->_left = cur->_left;  // make p's left ptr same as cur's
hook(p,tree,par,cur);
}
else
{
while(p->_left && p->_left->_left)
{ p = p->_left;
// now p points to parent of inorder successor to cur
Node *succ = p->_left;
p->_left = succ->_right;  // hook up subtree

succ->_left = cur->_left;
succ->_right = cur->_right;
hook(succ,tree,par,cur);
}
}
}
cur->_left = NULL;
cur->_right = NULL;
delete cur;
}

0

Author Comment

ID: 1166655
Much Thanks!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month8 days, 2 hours left to enroll