C++ :: Binary Search Tree Delete Node
Sep 5, 2014Below is my implementation of a Binary Search Tree, but there is a bug in the DeleteNode() function. It does not set the parent's pointer to NULL as I intended.
View 1 RepliesBelow is my implementation of a Binary Search Tree, but there is a bug in the DeleteNode() function. It does not set the parent's pointer to NULL as I intended.
View 1 RepliesThis is the delete function of binary search tree. However it won't enter the if-else statement that checks whether the node to be deleted is the left child or right child.
void DeleteNode(node* T, int number) {
node* x = new node;
node* current = new node;
node* dele = new node;
node* finder = new node;
finder = root;
[Code] ....
I have my tree, and I need to find the number in the tree (given by the user) and get the distance between the node found and the root.
What I've done so far is this, but I don't think it's correct.
void BinarySearchTree::find() {
system("cls");
BinarySearchTree b;
int num;
tree_node* cur;
[Code] ....
I founded the node, but I don't know how to determine the distance.
How would I remove the largest node in a Binary Search tree?
function prototype:
boolean remove_largest(node *& root)
{
}
I know that if there's no right child, then the root node has to be deleted. If there is a right child, then traverse that path for the largest node. But how to make this in to code that works?
It seems that I can't delete a node without children....
#define TRUE 1
#define FALSE 1
typedef struct binary_tree_nodes_t {
struct binary_tree_nodes_t* right;
struct binary_tree_nodes_t* left;
[Code] ....
Should my search_node function return this : binary_tree_nodes**
So I can get the address of pointer , is this causing my problem ?
how can a breadth-first-search-tree include every node have ancestor list. every node have multiple ancestor list(because every node may be multiple parents)
input: undirected graph
I am trying to delete a node(has no children). for some reason, it keeps giving me an error.
void AVLtree<T>::remove(const T& item) {
node* curr = root;
node* prev = nullptr;
node* next = nullptr;
if(curr == nullptr)
[Code] .....
the error is: Access violation reading location 0xfeeefeee.
I am having trouble with my deleteNode function, using recursion I am trying to delete a node from a binary tree. My tree is displayed as the following...
Tree: (node)->[left child, right child]
(k)->[g,p]
(g)->[c,h]
(c)->[ ,e]
[Code]....
The result did show that it is erased but when I try to add the erase node "t" back into the tree it still exist. I am stumped on what to do when using recursion way of deleting node.
I'm trying this to get the hang of boost::shared_prt and weak_ptr so far I have the following
Code:
#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/lambda/bind.hpp>
[Code] .....
My questions are, is the tree class defined correctly? How do I find the parent of a given node? And how does the insert part will work?
It has been a while since I built a binary tree from scratch so I decided to do it. Everything works fine but this one function. When I enter a number to search it just keeps running and allowing me to keep enter numbers.
Code:
void tree::search(int key,Node* leaf) {
if (leaf == NULL) {
std::cout<<"The tree is empty
[Code] ......
I am having an issue when i try to delete a node with 2 children it either doesn't delete anything, or wigs out in various manners deleting the wrong node or replacing a node with a various memory location. As follows, here is the delete function:
void BST::dele(){
bool found = false;//initialize a bool type to "find" the element to be deleted
if(root == NULL) return;//well if the tree's empty, nothing to be found right?
current = root;//set the current to the root to traverse the tree in search of the element
node* parent;//create a parent node for use once the node has been deleted
while(current != NULL){//traverse the tree
[Code] .....
I am trying to implement BST using templates. This is the code that i have written.
Code: template <class T>
struct node
{
struct node *left;
[Code].....
There are no errors but the program hangs. What is the mistake with this program ?
I am not sure how to use the parameters ostream and enum type in a print function.
enum TraversalOrderType {preorder, inorder, postorder};
template <class ItemType>
void Tree<ItemType>::print(ostream & Outstream, TraversalOrderType order) {
[Code] ....
Is this the correct way to call the print function?
int main() {
Tree<int> tree1;
tree1.print(cout, preorder);
return 0;
}
I don't know why, but my remove function doesn't seem to operate properly. The rest of my code is fine, so I am trying to pinpoint the exact location of my error. The else if statement remove(root->left, data) should've been called twice, but it only called once.
BST* smallestNode(BST* root)
// precondition: T is not null
// postcondition: return the node in the subtree rooted at T that
[Code]....
Traversal of binary search tree. In my header file there is a function setTraversal (public) and private print file. As I understood from teacher's explanation, my setTraversal function should call the recursive print function and print the list depending on selected order (pre,in or post-order). I still cannot get my head around what should be in setTraversal function definition. All resources I read last two days explain each order separately (preorder, inorder, postorder). How can I combine them? Here is my code:
#include "NodeTypeBST.h"
#include <iostream>
enum TravType {PRE, IN, POST};
template<class T>
class BST
[Code] ....
I was working on binary tree implementation and came up with this implementation. optimize this code?
/*
Binary Search Tree(BST)
06/27/2013
*/
#include "iostream"
[Code].....
I'm working on a programming homework that asks us to implement all the functions of a Binary Search Tree using templated classes. I'm almost done with it but I'm sort of stuck with the last part which is the `search` function. For this part the homework asks for the following requirements
Quote
Node<T>* search(T value, Node<T>* subtree)
if the current node's value is the one we're search for, return a pointer to itif the current node's left and right subtree's are empty (both m_left and m_right are looking at nullptr) then return the nullptr to indicate we didn't find the valueif the value is less than the current node's value, return the search of the left subtreeif the value is greater than or equal to the current node's value, return the search of the right subtreeMake sure to only traverse a subtree if it's not null
I had an assignment that I completed and it was just inserting values into a binary tree and then displaying it. I was wondering what the code would be if I wanted to delete a number in the binary tree, or if I wanted to search for a number. Here is my code for my assignment.
Code:
#include <stdio.h>
#include<stdlib.h>
typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;
[Code] ....
Standard example. I have a large text file and I wish to lex it into words. I tell the program that all words are delimited by ' ' ';' ':' and ''.
When I run the program it seems to be outputting the occurances of the letters and not the words. Im gobsmacked, I dont know what the hell is going on. Heres the function that lexes letters and not words. I want words dammit words!!
First youll see I define root node and point it to null; This forms the base of the BST. Then keep munching one character at a time until EOF reached. If the character is not a delimiter, assign it to "word" string, character by character. If it is a delimiter, take the so-far-constructed "word" and chuck it in the BST, then clear the word string through .clear().
Repeat until done. But its not working.
Code:
struct masternode* lexical_scanner(ifstream* inputfile) {
string word;
char c;
struct masternode* root = NULL;
while (c!=EOF){
[Code] .....
All the other functions in the source file are just fine, I've tested them in other apps and they are purpose built.
I was working with binary search tree and came up with the solution:
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct data {
int x;
struct data *left;
struct data *right;
[Code] .....
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct bst {
int val, level;
struct bst *left, *right, *parent;
[Code]....
The code above is supposed to be a binary search tree. The main outcome for this program is to display the tree in C each time the user inserts or deletes a value.
Since I am a newbie in C programming, I first tried creating a code that would simply display the values in the tree after a user inserts and deletes, before I proceed to displaying the exact tree.
But when I run it the following output shows:
And when I try to insert another value, It won't display anything and won't respond to any keys pressed.
I'm trying to create a template binary search tree and I'm getting all these vague errors that I have no clue how to solve. I've narrowed it down to my findMax and findMin functions but i can't figure it out any further than that.
template<class T>
class BinarySearchTree{
private:
struct BinaryNode{
T data;
BinaryNode *left;
BinaryNode *right;
[Code] .....
and here is are the errors I'm getting from this header file.
1>------ Build started: Project: Programming Assignment 2, Configuration: Debug Win32 ------
1> main.cpp
: error C2143: syntax error : missing ';' before '*'
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C2065: 'T' : undeclared identifier
[Code] ....
I'm writing the function as described in the title but it isn't quite working. It works as long as the value passed is less than the parent (going left) but when the value should be placed to the right, it doesn't actually insert the node.
template <typename T>
void BST<T>::insertHelper(BST<T>::BinNodePtr &subRoot, const T& item) {
BinNode *newNode;
BinNode *parent;
BinNode *child;
[Code] ....
FYI, I've commented out setting the children of the new leaf to NULL because the constructor already does that.
I want to display the numbers in a bst but all I get are 0s, what is wrong with my code that is causing this?
#include <iostream>
using namespace std;
class binarySearchTree {
private:
class TreeNode {
friend class binarySearchTree;
int value;
[Code] ....
Im working on a BST remove function. I think I'm on the right track but I'm not sure. From what I understand there are 3 possible cases. A Node with no children, one child, or 2 children(this being the most complex).
void BST::remove(int x) {
TreeNode *n;
TreeNode *v;
n= root;
while(n != NULL && n->key != x){
[Code] ....
How would I display the last two largest nodes in a binary search tree recursively?
View 1 Replies View Related