C++ :: Binary Tree Source?
Jul 12, 2013I just want to ask if some of you know good source for learning about Binary Trees.
View 2 RepliesI just want to ask if some of you know good source for learning about Binary Trees.
View 2 RepliesI want to find maximum element from a tree (not a binary tree ) ???? ...using c language...
View 1 Replies View RelatedHow to compile this source code and make a binary file
I think this need linux and run make command or gcc
This binary is a super user for android
source code attached
Attached File(s) : su.zip (7.59K)
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,.
It only prints out the root in an infinite loop.
Code: void Btree::printTree(Node* tree)
{
tree=root;
if (tree != NULL)
{
std::cout<<" "<< tree->key;
printTree(tree->left);
printTree(tree->right);
}
}
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 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]...
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
[Code].....
I want to print binary tree in this form on screen
1
/
2 3
/ /
4 5 6 7
/ /
8 9 10 11
This is the code for creating Binary Tree:
#include<stdio.h>
struct node
{
int data;
struct node *right;
struct node *left;
};
struct node *root = NULL;
int main ()
{
struct node *child, *temp;
[Code]...
I am not sure how to build the tree with values:
Code:
struct Node *root = 1 root->left = 2
root->right = 3
root->left->left = 4;
root->left->right = 5;
Everything should be right except the main(). How to demonstrate this program correctly?
Code:
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include <stack> // std::stack
[Code] .....
I have implemented a copy control version of binary tree.. As I am a Beginner. I think i have made my mistake.
Code:
#include <iostream>
#include <string>
class TreeNode{
TreeNode(const std::string &val):value(val),count(new int(1)){}
TreeNode(const TreeNode& rhs):value(rhs.value),count(count),left(rhs.left),right(rhs.right){++*count; }
[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 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 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].....
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 in interested to know what will be the height of binary tree with say 5 nodes (consider it balanced) is it 2 or 3?
9
/
5 13
/
3 7
and for following tree also:
2
/
7 5
/
2 6 9
/ /
5 11 4
is it 3 or 4?
i'm asking because, not able to find correct/one answer.
the algorithm i find is:
depth(struct tree*t) {
if ( t == NULL) return 0;
return max( depth(tree->left), depth(tree->right) ) + 1;
}
According to it, ans for second will be 4
But as per image on wikipedia [URL] .... the ans is 3
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`?
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?
How would I store a 234 tree in a binary file?
I've never worked with trees or binary files before so it is very confusing.
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*’
what can i do?
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]....
i have come across this code for calculating the depth of a binary tree.
int maxDepth(Node *&temp) {
if(temp == NULL)
return 0;
else {
int lchild = maxDepth(temp->left);
int rchild = maxDepth(temp->right);
[Code] ....
In these recursive calls i am really clue less about how the statements i numbered from 1 to 4 would be executed...in every recursive call...???
Lets temp has left depth of 3 and right depth of 100 then...in last 97 recursive calls max(temp->left) would be doing...????? how these recursive mechanism work here... I want to know specifically how these left node and right node recursive calls are co-ordinating with each other????
I used a BFS to simulate graph-join structure in a C++ code, and had a binary tree that asks the user e.g. to insert a node in the tree among other options, anyhow, I reached a point where I should pass a set of vertices from the graph to the tree, but how to do so. So I wanted to ask is such a thing possible? i.e. passing vertices from a graph to a tree automatically (no user involvement is needed)?
View 4 Replies View RelatedTraversal 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 got problem in binary tree code. below is my code. When i select pre, post or inorder, the .exe is not responding.. Since in my binary tree theres no roots yet.. who do i solve it? It need condition or what?
View 1 Replies View Related