I'm trying to count the number of node in a binary tree. I have a public count function that calls a private count function. I keep getting a linker error: Undefined symbols for architecture x86_64:
"BST<int>::nodesCount(BST_Node<int>*, int&)", referenced from:
BST<int>::nodesCount() in main.o
This is in Xcode. It doesn't seem to like calling the private function in the public one, but I'm not sure why since I'm doing that with several other functions.
//public function
template <typename T>
int BST<T>::nodesCount()
{
I am trying to count every node of a binary tree internally (not passing in the root), all my attemps have ended in memory access violation or an inaccurate count (4 or 5) when it should be 10.
This is what I have tried
And yes I am NOT suppose to pass in the root.
int CountChildren() { if ( !Left() && !Right()) return 1; if ( Left() ) return Left()->CountChildren() +1; if ( Right() ) return Right()->CountChildren() +1; }
I tested my count funtion. So my count function is not working properly, it should return 5 because 5 words have prefix "tal," but it is giving me 10. It's counting blank nodes.
This is my main.cpp file
int main() { string word; cout<<"Enter a word"<<endl; cin >> word; string filename;
I'm writing a class to store a tree I have a small issue.
I have a class which stores a node of the tree, something like this:
template<class T> class node { private: (stuff regarding parent node and child nodes); T __data; public: // more stuff (constructors and utility functions) here };
And I want to I want to be able to do the conversion T(node<T>) implicitly. For example, I want to be able to do this:
node<int> myNode; myNode=5; myNode+=10; myNode*=9; int x=int(myNode/4); cout << x+myNode;
And this:
class myClass { int x; int y; void print () {cout << x << ' ' << y << endl;}
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*)'
So I have this code that I wrote that pretty much makes a binary search tree with one node per element that holds the data int, one pointer for left, and one pointer for right. My tree is set that if you give it a number it starts as root, then afterwards any number you give it will either be placed left or right if it smaller than the current node or bigger respectively until it hits a pointer that points to NULL.
I was able to get my code to display the numbers in order no matter how bad you inserted the numbers to throw me off. My question now is this, how can I make it count how many levels there are? I'm not sure if this is clear or unclear but I want it to take all the paths and return to me the longest path and that should be how many levels there are.
#include <iostream> using namespace std; class binarySearchTree { private: class node {
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
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 {
I've almost got my code working. My issue is, if I type in either one of the first two id numbers, it finds the person and displays as it's supposed to, however if I type any of the last 6 id numbers it says id not found. I've been staring at this forever and can't see what I'm missing />/> ps, I haven't added in all my comments yet Binary tree template
//Binray tree template class #ifndef BINARYTREE_H #define BINARYTREE_H
I need to write a program to find the sum of depths of a binary tree, where a depth is by definition the shortest distance between a node and the root. I am required to code this using recursion.
I was thinking of first coding a helper recursion to find the depth for each node. What would be the best way to do that? If I could move from the node to the root, I believe programming this helper recursion would not be very difficult. Is there a way to progress from the node to the root?
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.
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`?
How to store values from a .txt file delimited with semicolons (;) into a class which is then stored into a Binary Search Tree. After browsing Google for a few hours, and trying various examples of people using Vectors, I just can't seem to get my program to work using Object Oriented Programming with an instance of the class Person.
My two classes are Person, and BinarySearchTree as follows:
class Person{ private: string first_surname; string second_surname; string name; int ID;
[Code] ....
Ok so my text file saves the data of each person in the same order as the class with each value separated by a semicolon.
i.e. First_Surname;Second_Surname;Name;ID;Telephone;Score;
void fillTree( BinarySearchTree *b) { string input[7]; Person p; fstream file("scores.txt", ios::in); // reads text file if(file.is_open()) {
[Code] ....
I understand that I get an error because a vector is saved as integers, and I am using strings, my question is, any other way to read the .txt file and save each data separated by a semicolon, into the Person class?
I'm trying to implement a binary tree using this class:
class btree { public: int key; btree *left; btree *right; btree(){left=NULL; right=NULL;} void insert(int key);
[Code] ....
gives me the following error:
albero_binario1.cpp: In member function ‘void btree::insert(int)’: albero_binario1.cpp:34:18: error: lvalue required as left operand of assignment albero_binario1.cpp:39:15: error: request for member ‘insert’ in ‘((btree*)this)->btree::right’, which is of non-class type ‘btree*’ albero_binario1.cpp:41:14: error: request for member ‘insert’ in ‘((btree*)this)->btree::left’, which is of non-class type ‘btree*’