Hi all,
I am trying to write a Binary Search Tree that each of its node will
have 3 node pointers: left, right and parent. I need a parent pointer
for some the purpose of my project. Without the pointer to the parent
node, the program will be inefficient and slow. It works fine at first.
However, when I started to build the remove function, it destroys the
tree when I delete a node. I already changed the parent pointer
whenever I delete a node, but still doesnt work correctly. This is the
tree node class:
/////////////////////////////////////////////////////////////
template <class Comparable>
class BinaryNode
{ //Binary Tree Node
Comparable element; //element
int frequency; //frequency
int height; //height
BinaryNode * left; //left child
BinaryNode * right; //right child
BinaryNode * parent; //parent
/* more functions here */
friend class BinarySearchTree<Comparable>;
};
//////////////////////////////////////////////////////////////
This is part of the remove function:
//////////////////////////////////////////////////////////////
/* more codes here */
BinaryNode<Comparable> *oldNode = t;
if (NULL != t->left)
{ //the node has a left subtree
t->left->parent = t->parent;
t = t->left;
}
else if (NULL != t->right)
{ //the node has a right subtree
t->right->parent = t->parent;
t = t->right;
}
else
{ //no children
}
delete oldNode;
/* more codes here */
//////////////////////////////////////////////////////////////
Can someone tell me how can I solve the problem of deletion? Or is it
possible? Thanks in advance... 2 5215
I can't really tell you what's wrong without knowing more information,
for starters, what does 't' refer to?
if 't' is the very top of the tree than your remove function wouldn't
work because the top doesn't have a "parent".
if (NULL != t->left)
{ //the node has a left subtree
t->left->parent = t->parent;
t = t->left;
}
You're connecting the child nodes to t's parent, but not t's parent to
the child nodes.
Also, it's more aesthetically pleasing if the data you're comparing is
on the left side to the data being compared.
i.e.
(t->left != NULL) instead of (NULL != t->left)
-Chinchilla
<da*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a node, but still doesnt work correctly. This is the tree node class:
///////////////////////////////////////////////////////////// template <class Comparable> class BinaryNode { //Binary Tree Node Comparable element; //element int frequency; //frequency int height; //height
BinaryNode * left; //left child BinaryNode * right; //right child BinaryNode * parent; //parent
/* more functions here */
friend class BinarySearchTree<Comparable>; };
//////////////////////////////////////////////////////////////
This is part of the remove function:
//////////////////////////////////////////////////////////////
/* more codes here */
BinaryNode<Comparable> *oldNode = t;
if (NULL != t->left) { //the node has a left subtree t->left->parent = t->parent; t = t->left; } else if (NULL != t->right) { //the node has a right subtree t->right->parent = t->parent; t = t->right; }
Your code is too incomplete for me to really know what is going on. I am
guessing that t is a pointer stored in the parent of the node to be deleted
that points to the node that is to be deleted. I will refer to the parent of
the node to be deleted as the grandparent.
What if both t->left and t->right are non-zero? Because of the "else if"
form, you only hook up the left node to the grandparent and lose the right
node. Of course, hooking up both is going to be a problem if the grandparent
started out with two children, because you would be deleting one and adding
two, giving the grandparent three children.
else { //no children }
delete oldNode;
/* more codes here */ //////////////////////////////////////////////////////////////
Can someone tell me how can I solve the problem of deletion? Or is it possible? Thanks in advance...
--
John Carson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jova |
last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so
can someone please explain.
Thank You.
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working,...
|
by: free2cric |
last post by:
Hi,
I have a single link list which is sorted.
structure of which is like
typedef struct mylist
{
int num;
struct mylist *next;
|
by: Satish.Talyan |
last post by:
hi,
i want to create a dynamic tree hierarchy in javascript.there are two
parts in tree, group & user.when we click on group then users come
under that's tree category will be opened.problem is...
|
by: hn.ft.pris |
last post by:
I have the following code:
Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template...
|
by: whitehatmiracle |
last post by:
Hello
I have written a program for a binary tree. On compiling one has to
first chose option 1 and then delete or search. Im having some trouble
with the balancing function. It seems to be going...
|
by: Defected |
last post by:
Hi,
How i can create a Binary Search Tree with a class ?
thanks
|
by: Ganon11 |
last post by:
OK, first of all, thanks to everyone who helped me out with my Isomorphism problem - it finally works. Now, the other part of my homework I'm having trouble with is this:
I don't want to post...
|
by: Vinodh |
last post by:
Started reading about Binary Trees and got the following questions in
mind. Please help.
Definition of a Binary Tree from "Data Structures using C and C++ by
Tanenbaum" goes like this,
"A...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |