Solved

Deleting Within A Tree

Posted on 1998-06-25
3
219 Views
Last Modified: 2010-04-01
Need help with code that will enable the user to delete a node within a tree of words.
0
Comment
Question by:John500
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 40 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

by:nietod
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->_numNodes--;  // adjust tree statistics
 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

by:John500
ID: 1166655
Much Thanks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 77
C Language combined operators 28 109
How to gracefully close the c++ 11 thread? 3 94
computer science syllabus 3 81
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question