473,883 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary Search Tree link to parent

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 BinarySearchTre e<Comparable>;
};

//////////////////////////////////////////////////////////////

This is part of the remove function:

//////////////////////////////////////////////////////////////

/* more codes here */

BinaryNode<Comp arable> *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...

Oct 31 '05 #1
2 5312
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

Oct 31 '05 #2
<da*******@gmai l.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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 BinarySearchTre e<Comparable>;
};

//////////////////////////////////////////////////////////////

This is part of the remove function:

//////////////////////////////////////////////////////////////

/* more codes here */

BinaryNode<Comp arable> *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

Oct 31 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2539
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.
4
9025
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, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
10
9788
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;
1
4837
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 that i am not able to arrange three things simultaneously,group,users & there functionality simultaneously.dynamic means group & users come from database and groups & users can be increased in number at any time. i am sending code for that tree...
1
2957
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 <typename Tclass Tree{ private: Tree<T*left;
4
11020
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 into an infinite loop, i think im messing up with the pointers. Heres the code. //press 1, first, Do not press it a second time!!!
11
1191
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
36
5946
Ganon11
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 the whole code here - it's about 140 lines, and it would violate our homework policy. Basically, it looks like my remove method is faulty; I'll include the struct definition, the class definition, and my two remove method definitions: template...
7
3879
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 binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
9779
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10835
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7960
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5787
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.