D
dannielum
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...
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...