C++ :: Insert Function In Binary Tree - Stack Overflow Error

Mar 26, 2013

I am trying to implement the insert function of a binary tree. Atm when I try and insert 3 nodes, the program breaks and gives me a stack overflow error. The error points to a getter function for an identifier for the data in my node class.

void LinkedList::add(Product *myProduct) {
if (_length==0) {
_head = new Node(NULL, NULL, myProduct);
_end = _head;
_length=1;

[Code] ....

Here is my insert function, the error message is

"An unhandled exception of type 'System.NullReferenceException' occurred in SDI2.exe
Additional information: Object reference not set to an instance of an object. "

In my main I have declared an instance of product, "productToAdd = new Product(id,idPrice);" so I'm a bit confused as to what I need to include..

View 3 Replies


ADVERTISEMENT

C :: Binary Tree Insert Function

Mar 31, 2014

I am having trouble writing an insert function for a binary tree.

Here is the structure that I have to use.

I have the print function done, the insert function is the one I am having trouble with,

Code:

typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;
}BST;

[Code].....

View 10 Replies View Related

C :: Scanning Words Into Binary Search Tree - No Stack Error Diving Seg Fault

Apr 2, 2013

This is the first time I have encountered a "no stack." error giving me a seg fault. Anyhow, I am scanning words into a binary search tree and it is giving me a seg fault.

Here is the file (words.txt):
4
bravo
alpha
gamma
delta

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct node_t{
char *word;
struct node_t *left;
struct node_t *right;

[Code] .....

View 5 Replies View Related

C++ :: Binary Search Tree (Iterative Function To Insert)

Jun 23, 2013

I was studying BST and tried to make a iterative function to insert, the original recursive function is the following:

void insert(node *&tree, int value) {
if (!tree) {
tree = new node;
tree->num = value;
tree->left = tree->right = NULL;

[Code] ....

And the code that i did is (but doesn't work):

void insert(node *&tree, int value) {
if (!tree) {
tree = new node;
tree->num = value;
tree->left = tree->right = NULL;

[Code] ....

I don't see where the error is or why it doesn't work.

View 6 Replies View Related

C++ :: Binary Search Tree - How To Implement Insert Function Properly

Nov 18, 2013

I am unable to implement the insert function properly, every time i run the program i just get the first value and name, I am not getting other Id's and name.

"(Header File)"
#include <iostream>
#include <string>
using namespace std;
class node{
public:
int ID;
string name;
class node *left, *right, *parent;

[Code] .....

View 4 Replies View Related

Visual C++ :: Binary Search Tree - Implementing Insert Function

Nov 18, 2013

I am unable to implement the insert function properly,every time i run the program i just get the first value and name,i am not getting other Id's and name.

Code:
"(Header File)"
#include <iostream>
#include <string>
using namespace std;
class node {
public:
int ID;
node (string StudentName, int IDNumber) {

[Code] ....

View 4 Replies View Related

C++ :: Expecting Stack Overflow Error?

Jun 9, 2013

After not programming for sometimes I decided to program a small application. But I have some trouble according to what I remember and re-checking arrays online. I don't understand how the code I wrote below is possible, shouldn't give a stack overflow error or something to that extent? Instead it gives me the value of 3.

#include "stdafx.h"
#include<iostream>
int _tmain(int argc, _TCHAR* argv[]) {
int arr2 [2];
arr2 [3] = 3;

[Code] ....

View 1 Replies View Related

C :: Stack Overflow - Expected Expression Error

Sep 10, 2013

I'm pretty new to C, and keep getting an error. Basically I'm trying to convert a ppm image to all red. Here is my code that I can't get to work:

Code:
change(pixel_t *PIXEL, &w, &h,pixel_t *buffer);

And here is the beginning of my convert function:

Code:
void change(pixel_t *pixel, int w, int h, pixel_t *buffer) {

int average, sum;
int i;
pixel_t *pointer;

Everything else works fine. I keep getting an error when I call the convert function. It says "expected expression before pixel_t" and "too few arguments to function "change". I know that everything else in the main is working.

View 3 Replies View Related

C++ :: How To Insert Item Into Binary Tree

Jun 30, 2013

I am trying to write a function that inserts an item into this binary tree in C++.

template <typename T>
struct BTree {
T val;
BTree<T>* left;
BTree<T>* right;
BTree(T v, BTree<T>* l=nullptr, BTree<T>* r=nullptr) :
val(v), left(l), right(r) {}
};

Here's my attempt.

template <typename T>
void insert(BTree<T>* tree, T item) {
if (tree == nullptr) {
tree = new BTree<T>(item);
} else if (item < tree->val) {
insert(tree->left, item);
} else {
insert(tree->right, item);
} }

I think this function may not be working because I am modifying `tree`, which is a local variable. How do I actually modify the current pointer that `tree` represents, not just `nullptr`?

View 2 Replies View Related

C++ :: Binary Tree - Insert Or Find

Jun 10, 2014

So I am trying to create a simple binary tree that will ask the user for a command so that is either 'insert or find' and with insert they can put in a number to this tree and with find they can ask if 53 is in this tree and the program will output yes or no..

View 9 Replies View Related

C++ :: BST Iterative Insert For Binary Search Tree

May 13, 2014

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.

View 1 Replies View Related

C++ :: How To Check What Causes A Stack Overflow

Sep 22, 2013

I'm getting a stack overflow error because for large numbers, this code I'm working on allocates too much on the stack.

Is there a way of tracking stack allocations specifically?

Will multithreading solve my problem if each thread is doing the static allocations?

Would I really have to use malloc or new every time I wanted to use memory just to make my code scale to huge numbers?

View 11 Replies View Related

C :: Bit Checking - Stack Overflow

Sep 19, 2013

I usually check if a bit is set using:

Code: bit = (number >> n) & 1; where `n` is the bit I want to check...

But I came across a stackoverflow answer saying:

bit = number & (1 << x); will not put the value of bit x into bit unless bit has type _Bool (<stdbool.h>).

Otherwise, bit = !!(number & (1 << x)); will..

So why is this? why the double !?

View 5 Replies View Related

C++ :: Runtime Stack Overflow

Jun 5, 2013

Here is my error message: Warning1warning C4717: 'getx' : recursive on all control paths, function will cause runtime stack overflow

and here is my code I will Bold the part that is giving me problems!

class point
{
public:
point();

[Code]....

View 5 Replies View Related

C/C++ :: Binary Search Tree Program - Segmentation Fault Error

Mar 21, 2014

I'm writing a binary search tree program and I got it to compile but as soon as I input something it returns a "segmentation fault error" . I suspect the issue with the code is withing my `add` function.

template<typename T>
void BinarySearchTree<T>::add(T value) {
if (m_root == nullptr) {
Node<T>* node = new Node<T>;
node->setValue(value);
m_root = node;

[Code] ....

View 6 Replies View Related

C :: Function Max In Binary Tree

May 28, 2013

I have a problem with the C code . I created two functions, one that runs through the tree inorder, the other that returns the maximum value in the tree. The problem is when I use in the main the method "max", which goes in a loop and not print anything on the screen . If I remove the call to method "max" it works fine. Here's the code:

Code:

#include<stdio.h>
#include<stdlib.h>
#define bool int
/* A binary tree tNode has data, pointer to left child
and a pointer to right child */
struct tNode {

[Code]...

View 13 Replies View Related

C++ :: Binary Search Tree Remove Function

Feb 14, 2014

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] ....

View 4 Replies View Related

C++ :: Binary Search Tree Print Function?

Nov 2, 2014

1) how come root node is printed when the first call to function goes to left subtree?

2) How it resets to root to go to right subtree from last left tree leaf?

void bst:: printNodes(hwareItem*& root){
if(root){
printNodes(root->left);
cout<<root->barcode<<endl;
cout<<root->description<<endl;
cout<<root->price_per_unit<<endl;
cout<<root->stock<<endl;
printNodes(root->right);}
}

View 19 Replies View Related

C++ :: Binary Tree - Find Height With Recursive Function

Feb 21, 2015

For an assignment I have to finish prewritten code which consists of multiple source files and a header file.

Code:
#include "tree.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

[Code] .....

I understand that I have to find the height by using _left->height() and _right->height() as long as it is not a null-pointer , each time I do this the values of _left and _right change. That way you can check if it is possible to go further down in the tree. I also have to use a counter to keep track of the number of layers at each side of the root. I don't understand how to implement it.

View 3 Replies View Related

C :: How To Write One Binary Tree Function To Sort The Data

Dec 2, 2014

The Problem You are part of a company writing a spreadsheet program. As you know, spreadsheets can be sorted on any column. You're part of the project is to write one binary tree function to sort the data [Hint: use different fields when inserting nodes in the tree.] and a second function to list it in either an ascending or descending sequence. [Note: Each of these functions may actually need to be a set of related functions.]

For sample data you will have a disk file containing information about Shakespeare's plays. Your first function should create a tree based on the sort selected by the user and the second function to display the data in the sequence selected by the user. Regardless of the column being sorted, data in individual records always be displayed in the same line of the output.

Input : Each record will contain the following information: First Performed 9 characters Printed 5 characters Title 26 characters Type 7 characters

Output : Tabular output should be aligned in columns with two spaces between each. All columns should have headings. It should be sorted on the column specified by the user.

Example (This sample data provided so you can test your program.) If the data is:

1595-96 1600 A Midsummer Night's Dream Comedy
1594-95 1623 Two Gentlemen of Verona Comedy
1596-97 1623 King John History
1597-98 1598 Henry IV, Part 1 History
1611-12 1623 The Tempest Comedy
1602-03 1623 All's Well That Ends Well Comedy

[Code]...

Source: [URL]...

Possible outputs are

1 - for a sort by title: First
Performed Printed Title Type
--------- ------- -------------------------- -------
1595-96 1600 A Midsummer Night's Dream Comedy
1602-03 1623 All's Well That Ends Well Comedy
1606-07 1623 Antony and Cleopatra Tragedy
1599-1600 1623 As You Like It Comedy

[Code]....

2 - for a sort by first performed: First
Performed Printed Title Type
--------- ------- -------------------------- -------
1590-91 1594? Henry VI, Part 2 History
1590-91 1594? Henry VI, Part 3 History
1591-92 1623 Henry VI, Part 1 History
1592-93 1623 Comedy of Errors Comedy
1592-93 1597 Richard III History

[Code]....

View 1 Replies View Related

C++ :: Develop A Function Which Prints A Binary Tree Using Post-order Traversal?

Feb 26, 2013

I am trying to develop a function which prints a binary tree using post-order traversal (left, right, root) but I am experiencing some troubles. The code is compiled but when I run the program it crashes right before printing the post-order traversal.

Below you can find my code and the output from debugging.

Code:
/**
Program which represents a Binary Search Tree and is modified with the following functions:
- smallest() - member function which searches for the smallest element in the tree.
- preorder() - member function which prints the tree nodes using pre-order traversal
- postorder() - member function which prints the tree nodes using post-order traversal (to be completed)
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class TreeNode {
public:

[code]...

This is from the debugger log:

Child process PID: 5720
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:316
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:317
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:318
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:319
Program received signal SIGSEGV, Segmentation fault.
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:279

View 9 Replies View Related

C/C++ :: Find Maximum Element From A Tree (not A Binary Tree)

Oct 31, 2014

I want to find maximum element from a tree (not a binary tree ) ???? ...using c language...

View 1 Replies View Related

C++ :: Floating Point Error - Overflow (abnormal Program Termination)

Apr 22, 2012

Why I have a

HTML Code:

FLOATING POINT ERROR: OVERFLOW
ABNORMAL PROGRAM TERMINATION

in the code below?

Code:
#include<stdio.h>
#include <math.h>
#include<conio.h>
#define A 0
#define B 1
float F(float x0, float y0) {
return (0.2*x0)+(y0*y0);

[Code] .....

View 7 Replies View Related

C++ :: Floating Point Error - Overflow / Abnormal Program Termination

May 8, 2012

What is wrong with this code? I keep getting

HTML Code:
FLOATING POINT ERROR: OVERFLOW
ABNORMAL PROGRAM TERMINATION

Code:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<iostream.h>
#define N 4
float x[5]={0, 1, 2, 3, 4};
float y[5]={0.5, 2, 7.5, 20, 42.5};

[Code] ....

View 2 Replies View Related

C++ :: Binary Function Compile Error

Mar 8, 2013

I wrote a binary function based on existing template:

template <class T> struct percentage : binary_function <T,T,double> {
double operator() (const T& x, const T& y) const
{return x*1.0/(x+y);}
};

I call it in form of percentage<int>() When I compile, it's indicated: error C2143: syntax error : missing ',' before '<' I use VC 2012.

View 5 Replies View Related

C++ :: Create Binary Search Tree Template To Be Used To Create AVL Tree

Feb 10, 2013

For my data-structures class, I am attempting to create a binary search tree template to be used to create an AVL tree. I've written a Generic_Tree template for the BST to inherit from, and before I jump into implementing the AVL tree I'm testing the BST member functions. Everything was compiling fine until I added the BST insert() function. Now, I'm getting the following error message from my linker:

undefined reference to 'BST<void>::insert(int, void*)'

Stemming from the call in main line 16.

my makefile compiles with:
g++ -Wall -g Generic_Tree.cpp BST.cpp barfing.cpp main.cpp

Generic_Tree:

template < typename NODE_DATA, unsigned MAX_KIDS >
class Tree {
protected:
struct NODE {
NODE_DATA* contents;
Tree* child[MAX_KIDS];
Tree* parent;

[Code] ....

I'm use to c and havn't used classes or templates before (except for declaring instances of them). The whole syntax is mystifying to me,.

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved