I am writing a constructor for a binary search tree, the problem is that the helper function within the tree is being called infinitely, this eventually generates a stack overflow.

```
void copyTree(myTreeNode* & copy, const myTreeNode* & originalTree)
{
if(originalTree==NULL)
{
copy=NULL;
}
else
{
copy=new myTreeNode();
cout<<"This is the data my friend: "<<endl<<copy->data.getCharacter()<<endl;
copy->data=originalTree->data;
copyTree(copy->left, originalTree->getLeft());
copyTree(copy->right,originalTree->getRight());
}
}
//this is the copy constructor for the tree
myTree (const myTree & copy)
{
this->copyTree(this->root,copy.getRoot());
}
//and this is the way I have written the getLeft and getRight Functions
//they both return references to the left and rightNodes
const myTreeNode *& getLeft() const
{
const myTreeNode* ptr=NULL;
if(this->left)
{
ptr=this->left;
}
return ptr;
}
```

P.S the data object is not a primitive data type but it has no dynamic memory allocation.

I'm not sure how this might be causing infinite recursion, but your `getLeft()`

function seems suspect. You're returning a reference to something on the stack. Who knows what's happening to that memory after that. It looks like you keep on using that same slot in memory over and over again, so you might be creating a loop instead of a tree.

Change it so that it returns a pointer, not a reference to a pointer (remove the '&').

@JCooper figured it out -- I'm just providing sample code. The getLeft() function should look more like this. Please note that I am NOT creating any NEW variables, so there is no stack lifespan problem.

```
const myTreeNode * getLeft() const
{
//may be NULL
return this->left;
}
```

(EDIT: made code more concise. Thanks @molbdnilo!)

Similar Questions

I have written a functioning binary search tree in java apart from one key function, the search. I am using the logic that i will check if the root is null, then if the term i want to search for is eq

I encountered a code snippet and thought that it would call copy-constructor but in contrast , it simply called normal constructor . Below is the code #include <iostream> using namespace std; cl

I'm trying to create a binary search tree from the following class: class Tree(object): def __init__(self): self.data = None self.left = None self.right = None The following code should work on a lis

I have a binary tree with the only condition that there is a single node in the deepest level. The nodes in the tree have the parent property (as well as left, right, data) Is it possible to determine

I wrote the following Ruby implementation of a basic binary search tree. I believe that rt = Node.new(data) is not actually modifying the underlying object but is in fact just a temporary variable tha

I am trying to create a deep copy of my binary tree data structure in C++. The problem is the code I am using only seems to be giving me a shallow copy (which seems to cause problems with my deconstru

Hi I have a BST binary search tree typedef struct Treenode *SearchTree; struct Treenode { int Element; SearchTree Left; SearchTree Right; }; and I want to create a function FillArray(int sizeoftree

I am implementing a Binary Search tree in C++ I have the following code to add an entry to the tree: void tree::add(int n) { int found; leaf *t,*parent; findparent(n,found,parent); if(found==YES) cout

I am having a huge issue with my recursive function in my binary search tree. My project is due in a few hours and I cannot for the life of me get ahold of my instructor. My function seems to only be

I am currently learning java by writing my programs from C++ to Java. I am trying to print the data using recursive binary search tree, but its not printing Here is my code: public class PersonRec { i

Possible Duplicate: Why copy constructor is not called in this case? What are copy elision and return value optimization? Can anybody explain to me why the following program yields output cpy: 0 (

class WithCC { public: WithCC() {} WithCC(const WithCC&) { cout << in WithCC's copy constructor << endl; } }; class Composite { WithCC withcc; // Embedded objects public: Composite()

As our term project, we're implementing a binary search tree. The thought behind it is as follows: Assume a bst with 3 nodes: 10 / \ / \ 8 14 Its address representation is as follows (value, left no

I am having a difficult time understanding how binary search trees are built. Do they require the initial data to be sorted in order to find the top most root?

well i was creating a binary search tree using this code..as far as i as see using this code i find it to be correct but some how its creating error i dont know why #include<stdio.h> #include<

As mentioned in the Title , I have a Binary search tree. I want to convert it to sorted doubly linklist using recursion. My code for each node in tree find max of left sub-tree and assign its right t

I'm trying to write a size() method for a polymorphic binary search tree. I.e. A BST that has classes EmptyTree and NonEmptyTree that both implement a tree interface. EmptyTree is being used to repr

Are there efficient methods for finding the number of all elements less than an element in a binary search tree? I've been trying to find a procedure or implementation in C++, but I haven't been able

I have seen a lot of search algorithms to search in a binary sorted tree, but all of them are using the same way: recursion. I know recursion is expensive in comparison to loops, because every time we

I am completely new to Scheme and functional languages in general. I am trying to create a binary search tree. The format of a node is a list of three elements, the first being the value at the node,

I'm getting a problem with my display functions dealing with binary search trees. The main problem I'm having is getting the count variable to increment correctly in the recursive function so that I c

I am having an issue adding to my binary search tree, my program seems to be adding to a temporary structure instead. I think that in order for it to work correctly I have to call malloc for the left

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

I am building a binary search tree. Now I am having a problem with adding a node to the tree. void BinaryTree::add(int value, Node* node) { if(!node) node = new Node(value); else if(node->key <

Which is better implementation of search operation in binary search tree (recursion or looping)?

I'm currently working on a school project. The assignment is to write the implementation of a templated binary search tree given the corresponding header file. My issue is that since it's templated,

First, I have already read C++ Binary Search Tree status access violation error with adding nodes and Inserting in a binary search tree access violation error but am still having trouble fully impleme

using a binary search tree I need to add to a vector all int elements of the heaviest path of the tree. for example I have 20,7,6,9,11,21 the values that should be added to the vector would be 20 ,7,9

I was reading more on the binary search tree and then I found out about another variant of Binary Search Tree which is Splay tree and I am trying to implement it but somehow I got stuck. So the algori

I'm working on a problem which requires me to copy a binary search tree recursively and to return the tree. I am coding in the binary search tree class, so it will copy whatever binary search tree it

I'm having trouble interpreting a certain question about inserting elements to a binary search tree. I'm familiar with preorder, postorder, and inorder traversals, but I'm unfamiliar with the followin

There are many algorithms for finding optimal binary search trees given a set of keys and the associated probabilities of those keys being chosen. The binary search tree produced this way will have th

In Scheme, I can use define-struct to make a binary search tree, but how do you do it in Clojure?

When removing a node with two children in a binary search tree, how many times can the recursive removal of the child node be called?

I've been searching for how to calculate the height of a Binary Search Tree and my research has lead me to the following implementation. I'm still trying to wrap my head around why it should work, but

I'm learning C++ language and I'm trying to write BST, but something goes wrong. I try to add element to empty tree, root is NULL, but after adding element root is still NULL despite of the fact that

Please explain the difference between a binary search tree and m-way tree?

A friend was recently interviewed for a position at a tech company and was given 4 programming tasks. One of the tasks was to implement a Binary Search Tree class using a Linked List implementation w

What is the difference between binary search and binary search tree? Are they the same? reading the internet it seams the second is only for trees(up to 2 children nodes) and binary search doesn't fol

Two BSTs (Binary Search Trees) are given. How to find largest common sub-tree in the given two binary trees? EDIT 1: Here is what I have thought: Let, r1 = current node of 1st tree r2 = current node o

I am trying to implement a method to create a node to insert into a binary tree (not bst). struct node{ struct node *left; int data; struct node *right; }; typedef struct node *n; void createNewNode()

I need a general formula to calculate the minimum height of the binary tree and the maximum height of the binary tree. (not the binary search tree)

I kinda have to put my previous C questions on hold cause this one is more important now... I have already coded the insert and delete functions on my binary search tree but the delete function is inc

I would like to calculate the summation of the depths of each node of a Binary Search Tree. The individual depths of the elements are not already stored.

I've been trying to implement a bst, in C. I think I'm almost there, but in my add node function, I create a temporary node called current to store the current node which is visited in the tree. Then

Welcome! I have a recursive public static method named less that takes a tree node (an original binary tree, not really a search tree) and a int parameter that returns if all the values in the tree ar

The following is the code to converted a preorder traversal of a Binary Search Tree to the original tree. The following code takes an array of integers, which represent the pre order traversal of a a

I have got a question, and it says calculate the tight time complexity for the process of inserting n numbers into a binary search tree. It does not denote whether this is a balanced tree or not. So

I was working on Binary Tree and want to figure out if is there any algorithm to shuffle the tree and to sort level wise? Say for example I have an array as follows: int[] values = new int[16] {1,2,3,

I'm using a binary search tree that collects strings and then orders them in post order. I also use a list that shows the line number where the string appears. I can make the BST work properly however