I have some lines of code about a binary search tree (BSTreeBag) which I can not quite figure out.

The "operator +=(const BSTreeBag& addend)" requires to insert what's in the addend into the current tree we have. If the current tree we have is the same with "addend" we need to double our tree(to make duplicates of all items in the tree)

Here is my code

```
template <class ItemType>
void BSTreeBag<ItemType>::operator +=(const BSTreeBag& addend)
{
if(this==&addend)//This works
{
binary_tree_node<ItemType>* addroot_ptr=root_ptr;//create a pointer
//that points to the current tree
insert_all(addroot_ptr);//This is a helper function that insert
//every item of the addend into the current tree. It works fine.
}
else
{
insert_all(addend.root_ptr);
}
}
```

The lines of code works perfectly whenever it is not doing self-assignment. It always stops at the line

```
insert_all(addroot_ptr);
```

without giving any information about segmentation fault or other problem. Could someone explain what is going on?

A very likely problem is that you have an infinite loop when you add one tree to itself. Like in, you add nodes while iterating over the tree, but since there are new nodes being added you continue iterating and adding them, ad infinitum.

Lets give an example with a simple list. Lets say you have the following list:

root -> A

Now if you try to add the list to itself, you iterate over the list from the root pointer, finding the node `A`

, so you add that. Now your list looks like

root -> A -> A

You continue the iteration and find... node `A`

(again), and so you add it:

root -> A -> A -> A

And so on and so on.

You should probably create a completely new tree from `root_ptr`

and then add that.

This is what mine looks like (I think both the instructions AND the test file are a little wack):

```
template <class ItemType>
void BSTreeBag<ItemType>::operator+=(const BSTreeBag& addend)
{
if (this != &addend)
insert_all(addend.root_ptr);
else
{
BSTreeBag<ItemType> new_bst = addend;
insert_all(new_bst.root_ptr);
tree_clear(new_bst.root_ptr);
}
}
```

Similar Questions

Do you know, please, if C++ STL contains a Binary Search Tree (BST) implementation, or if I should construct my own BST object? In case STL conains no implementation of BST, are there any libraries av

I am getting an stack overflow when the program tries to add a number. It seems to be accessing a nonexistent binary search tree. Here is the error code: Unhandled exception at 0x01084DD9 in CIS 350 q

How do you compute the average height of a binary search tree when adding 1000 random ints? What is the average height?

I need help in understanding this interview question: Q: find an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its

What's the best way to create a balanced binary search tree from a sorted singly linked list?

I have a homework which ask from me to create a struct of binary search tree where its node of the binary search tree is another binary search tree. The first BST has the Surnames of Students and the

I'm a Python guy. Learning C language and I've been trying to implement Binary Search Tree in C. I wrote down the code, and I've been trying from few hours but, not able to get the output as expected.

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

Please help I've been trying to generate a random binary search tree of size 1024 and the elements needs to be random sortedset ... I'm able to write a code to create a binary search tree manually by

I have an theoretical question about Balanced BST. I would like to build Perfect Balanced Tree that has 2^k - 1 nodes, from a regular unbalanced BST. The easiest solution I can think of is to use a so

I'm creating Binary Search Tree class I got all my functions working accept the Successor. My tree input is 30 10 45 38 20 50 25 33 8 12 When I print Inorder traversal it come up as 8 10 12 20 25 30

I am suppose to write a method makePerfect that could be added to the IntTree class. The method should add nodes until the binary tree is a perfect tree. A perfect binary tree is one where all leave

Given an arbitrary set of values V and building a tree by inserting values left to right, what does it mean if I'm asked if my orderings of these values (to construct a minimum height and maximum heig

So, finding a key takes O(height) time, how much time does it take to find all nodes with a key greater than a given key? What are the constant factors?

I have been given two binary search trees. For example, A and B. Next, I was asked to delete the tree B from the tree A. By deletion, I mean delete all the nodes present in B from A. Note: B is not n

I am trying to quickly implement a Binary search tree in Java. What is the best class to use that has methods for in-order traversal? (I have heard of TreeMap class. But it looks like the class does n

What is the best way to create a method that determines the height of a Binary Search Tree class? For instance: bst.height() would return 1 if it contains only 1 item; return 2 if it contains 3 items

When implementing a Hashtable using an array, we inherit the constant time indexing of the array. What are the reasons for implementing a Hashtable with a Binary Search Tree since it offers search wit

Given my recent (somewhat successful) question: http://stackoverflow.com/questions/3387274/algorithmic-issue-determining-user-sessions I'm pretty sure that the way to solve it cleanly is to use a bal

I'm not entirely sure what amortized complexity means. Take a balanced binary search tree data structure (e.g. a red-black tree). The cost of a normal search is naturally log(N) where N is the number

I need to create a recursive method that takes as a parameter the root node of a binary search tree. This recursive method will then return the int value of the total number of nodes in the entire bin

I define a binary tree as a search table. The tree is a linked list binary tree, the node is like typedef struct node{ int key; char *buf; ... }node_t; typedef table_t node_t *; so I have functions l

I am studying Breadth First Search , and wanted to ask that Is the Tree constructed by Breadth First Search ( the BFS tree, wherein we store each node's predecessor) a Binary Tree?

Using Java, is it possible to write a recursive method to find an element in a binary search tree? I say no because of the nature of recursive re-tracing back unless I implemented incorrectly? I have

I am trying to run my Binary Search Tree, I am creating objects of type Employee in my main program which does not seem to give me problems, but when I choose to search for an item in my BST, the prog

Need help to figure out why the following code for basic Binary Search Tree insertion is not working. Since I have been working on C# for sometime now, I'm afraid I forgot some of my C++. Also, any su

i am constructing the operations of binary search tree in OCaml. type ('a, 'b) bst = | Node of 'a * 'b * ('a, 'b) bst * ('a, 'b) bst | Leaf;; let rec insert k v = function | Leaf -> Node (k, v, Lea

I'm putting together functions for a binary search tree and ran into a wall. I'm working on each situation that might be encountered when a node holding a specified value needs to be removed from the

So this is my first java program, but I've done c++ for a few years. I wrote what I think should work, but in fact it does not. So I had a stipulation of having to write a method for this call: tree.i

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'm trying to create a program to work with binary search trees and my instructor has this function set up with this declaration, which must stay the same. void BSTType<T>::traverse(std::ostream

According to my coding class, the left child is 2 * i and the right child is 2 * i + 1. With this in mind, I coded the following: void max_heap( int key ){ int leftkey = ( 2 * key ) + 1; int rigtkey =

In binary search trees most of the operations' average computational complexity is given as O(NlogN). Below is a text snippet from Algo's book: Average of internal path length is D(n) = O(n log n). T

So far I have the algorithm figured out to add to my binary search tree, but I'm having a bit of difficulty translating it into code. The algorithm is as follows: public void add(int v) { Create a new

This is my program, but I don't know how to show the binary search tree results in array (one dimensional). I use random as inputs. How to show binary search tree results in arrays? Help me please. #i

This is not an homework. I am just totally blocked on this. I know what to do but I am having difficulty manipulating the tree. please help. It seems like the below code works for leaf nodes but does

I am trying to define a simple binary search tree. It is stored in lists like so: [Key, Left Tree, Right Tree]. I'm having issue with my logic. This is what I have bstadd(K, [], [K,[],[]]). bstadd(K,

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 want to find minimum value in a Binary Search Tree. I have written below code. But when i call the function from main and I pribt the return value, it is printing as 0 always. cal you please help. i

Is there a built in binary search tree in .NET 4.0, or do I need to build this abstract data type from scratch?

I am trying to write a delete for a binary tree and this is the function I wrote along with my data structure: class node{ public: int value; node* left; node* right; ~node(); }; class tree{ public:

Working on implementing my own BST in C++ for the experience of dealing with such structures. I've had trouble implementing a destructor. I found in my studies, that one can't really have a recursive

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(myTre

I am working on a school project where we have to implement a polymorphic binary search tree that instead of using null references to empty parts of the tree, uses two classes (NonEmptyTree and Empt

I was just trying to write a simple binary search tree program where the user can insert nodes and view all the nodes in the tree in either inorder,preorder or postorder mode. My code is #include <

this is my height function in bst. cpp int IntBinaryTree::getHeight(TreeNode * nodePtr) { if(nodePtr = NULL) return 0; else return (max(1+getHeight(nodePtr->left), 1+getHeight(nodePtr->right)));

Is the depth of a node in a Binary Search Tree (BST) the same as it's distance from the root? I think so, but I'm not certain. I believe distance is the concept of trees in general and depth is that c

Is there a method to build a balanced binary search tree? Example: 1 2 3 4 5 6 7 8 9 5 / \ 3 etc / \ 2 4 / 1 I'm thinking there is a method to do this, without using the more complex self-balancing t

I am still fairly new to generics and binary search trees, and I am trying to add a custom Student object to a BST, and don't know exactly how to go about implementing it. I have my Student class decl

I'm working in a Binary Search tree class, and when I run my insert method I get this error: AttributeError: 'BSearch_tree' object has no attribute 'key' I don't understand what did I do wrong, and I