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
ADVERTISEMENT
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
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
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
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
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
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
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
View Related
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
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
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
Oct 31, 2014
I want to find maximum element from a tree (not a binary tree ) ???? ...using c language...
View 1 Replies
View Related
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
Feb 1, 2014
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);
}
}
View 5 Replies
View Related
Aug 12, 2014
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] ......
View 3 Replies
View Related
Jul 12, 2013
I just want to ask if some of you know good source for learning about Binary Trees.
View 2 Replies
View Related
Mar 1, 2015
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].....
View 14 Replies
View Related
Jan 14, 2015
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]...
View 12 Replies
View Related
Dec 29, 2013
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] .....
View 8 Replies
View Related
May 17, 2013
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] ......
View 11 Replies
View Related
Oct 27, 2014
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 ?
View 2 Replies
View Related
Jun 18, 2013
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?
View 11 Replies
View Related
Feb 18, 2014
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.
View 4 Replies
View Related
Feb 23, 2014
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
View 6 Replies
View Related
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
May 30, 2013
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?
View 2 Replies
View Related