C/C++ :: Printing Binary Expression Trees
Mar 18, 2014
I am working on a program that needs to take any infix expression as an input. And the display the expression tree of it on the console as an output. For example the input goes (a+-(c*d) should output:
-
+ *
a b c d
View 1 Replies
ADVERTISEMENT
Apr 1, 2014
I had an assignment that I completed and it was just inserting values into a binary tree and then displaying it. I was wondering what the code would be if I wanted to delete a number in the binary tree, or if I wanted to search for a number. Here is my code for my assignment.
Code:
#include <stdio.h>
#include<stdlib.h>
typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;
[Code] ....
View 1 Replies
View Related
Jan 29, 2013
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 {
[Code] .....
View 6 Replies
View Related
Jun 8, 2013
I'm having some trouble with my binary tree for school. It is a data structures class so we are working on learning how to make our own binary trees and encrypt messages. Everything so far is working, except for my delete node function. I'm trying to do it recursively. Parts of my code.
/******** Node *********/
struct node {
char data;
node* right;
node* left;
};
/******** Binary Tree Class *********/
class BinaryTree
[Code]...
View 5 Replies
View Related
Dec 5, 2013
My colleague and I have been given a task to create a list with the employes of a company using binary trees. Each employee/node has to contain the following information:
- Employe name
- Rank
- ID Number
- monthly Salary
We have to write a C program (using Codeblocks) which builds the company tree with the information above and afterwards lists all the employes who have a salary bigger then a number specified by the user.
So far so good. We've had a look on the examples on the internet and after finding one which looked friendly enough we started working on that one to suit our needs. Credits for the original source code to: C Program to Implement Binary Search Tree Traversal - Basic C Programs | C Programming Examples
We've reached the point where we're able to insert the required information but being able to list all the employes who's salary is bigger then the number given by the user seems to be rather difficult to achieve. I'll include the code next. We have left almost the entire pieces of the original code and we are aware that the are still things which are not required for our assignment, which only requires to enter the 4 information above and afterwards display the employes with the before mentioned condition concerning their salary.
Code:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST
[Code] .....
View 1 Replies
View Related
Jul 19, 2014
I've created a class that works with vectors doing various calculations and what not. I have overloaded operators that I've created outside of the main in separate header and class files. Ive tested them and the overloaded operators work correctly when I paste them into a the main file but when I have them defined in the other class files and I try to access them in the main class I get an error saying invalid operands to a binary expression. I also have other classes with overloaded operators that work just fine in the main class so I'm not sure what I did wrong here?
This is how I have my header set up, the definitions to these are in a separate class file which I don't think I need to include considering I have them tested and working so I don't think that's the problem (correct me if I'm wrong).
class VectorClass{
friend vector<float> operator+( const vector<float>&, const vector<float>& );
friend vector<float> operator...
friend vector<float> operator...
private:
...
...
public:
...
And then in my main class looks vaguely like this
#include "Name of vector class"
int main(){
vector<float> vR, v1, v2;
v1.push_back('some value');
...
v2.push_back('some value');
...
vR = v1 + v2; // Invalid operands here
return 0;
}
And like I said, I have other classes with overloaded operators set up the same way which work fine being implemented the way I have these, so I'm not sure where the problem is at.
View 2 Replies
View Related
Jul 23, 2013
I want to add mathematical expression in a binary tree but I have some problems with the algorithm. I found this one:
-If the current token is a '(', add a new node as the left child of the current node, and descend to the left child.
-If the current token is in the list ['+','-','/','*'], set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child.
-If the current token is a number, set the root value of the current node to the number and return to the parent.
-If the current token is a ')', go to the parent of the current node.
Here is the code that I made so far:
template<class T>
void Tree<T>::Expr(Node<T> *node, char expr[], int &i) {
i++;
T x = expr[i];
if(x == '(') {
node = node->Left;
[Code] ....
I know that it is a big mess and it doesn't follow the algorithm but this is the problem. For example if the token is '(' I go to the left child of the current node. Then lets say that the next token in the expression is a number. I add this number to the current node and I must go back. But how can I go back to the parent? I will go back to line 13 and the program will end. What should be the structure that I must use?
View 1 Replies
View Related
Apr 23, 2013
The point of my code is to evaluate a mathematical expression that's already formed in a Binary Search Tree. : ((6+5)-3)*(2/1)=?
I've read other forums and they usually use cout, cin and << >> but I'm still in the learning process so this is basically primitive coding.
Anyway, The recursion is already correct (I think). My problem is how to return char* or should I say an element which has 2 or more digits.
typedef struct cell{
char elem[max];
struct cell *left;
struct cell *right;
}celltype, *node;
node A;
char* evaluate(node *A) //passed as evaluation(&A);
[Code] ....
And the reason that I'm using char is because the operators are also elements in different nodes.
The code above is actually the result when I remembered that you can't return 2-digit char.
The code below is before I remembered that. It works when all results are 1 digit numbers.
typedef struct cell{
char elem;
struct cell *left;
struct cell *right;
}celltype, *node;
node A;
[Code] ....
View 1 Replies
View Related
Dec 6, 2013
Following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere.
Code:
#include <stdio.h>
int main() {
int a[3][3],i,j;
float determinant=0;
int x;
FILE *fp = fopen ("file.bin", "wb");
[Code] .....
View 6 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
Apr 19, 2014
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;
}
View 2 Replies
View Related
Mar 21, 2013
So, if I'm right, computer store their data as binary values. So if I write int x = 5; , my computer converts the value of x from decimal (5) into binary (101) and stores it in memory as a a binary number. If I print that value on the screen that value is converted(by default) back into a decimal number before being printed on the screen.
Now, my question is if there is any way to print the value of x directly into binary(as it's stored in memory) without it being converted back into a decimal value?
View 10 Replies
View Related
Nov 21, 2013
Implementation of a simple binary tree. What I'm trying
#include <iostream>
using namespace std;
struct bintree {
int number ;
char letter ;
bintree *left = NULL ;
bintree *right = NULL ;
[Code] ....
It crashes when I try to print, I dont think it is inserting the last values into the tree and so when it trys to print a null value it crashes. I'm just curious if I'm heading in the right direction and perhaps how to fix my printing issue as well as some hints on deleting and searching.
View 3 Replies
View Related
Aug 13, 2013
I'm intrested in creating programs of games and such which learns while playing and saving the information for further use. For example a tic tac toe game where the program saves evry game it won or lost and creates a tree of some kind that store the information of the game and save it in a file when the program is being quit. The problem i'm having is how to save a tree in a file efficently.
View 2 Replies
View Related
Oct 19, 2014
I have made cancer prediction based on symptoms using decision trees but i am not able to run my code ...
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
struct dectree {
char symptom[50];
[Code] ....
View 11 Replies
View Related
Sep 20, 2013
I have Problems with deleting node in trees!my first problem is I don't understand the algorithm of deleting a node with two child! I write some kind of code that delete a node with two child but I think (maybe I should say I am sure ) that it has runtime or logical errors or something like that!
My second problem is my code doesn't work if my node is root of tree!because I don't know if it is root what should be the parentPtr in my code! I set parentPtr to NULL in this situation but I know it is wrong!
Here is my Code :
#include <iostream>
#include "TreeNode.h"
using namespace std;
template<typename NODETYPE> class Tree {
public:
Tree();
void insertNode(const NODETYPE &);
[Code] .....
View 3 Replies
View Related
Nov 26, 2013
I've been working on my deletion function for Red and Black trees but can't seem to get it to work. From what I can tell, the remove function, instead of deleting the single value that will my prompt by the user, will delete all the values that are in the red and black tree.
Code:
bool RedBlackTree::remove(int x) {
if(!search(x)){
return false;
} else {
Node* z = new Node(x);
rbremove(z);
[Code] ....
View 4 Replies
View Related
Oct 23, 2013
line 27 and line 88 Im having a hard time figuring it out what the error is.
#include<iostream>
#include <cmath>
#include<algorithm>
[Code]....
View 2 Replies
View Related
Mar 29, 2014
I keep getting an error here and cant quite figure out why,
Code:
else if (mainMenu == 3){
cout << "Please make a selection" << endl
<< " 1) Withdraw from account" << endl
<< " 3) Back to main menu" << endl;
cin >> withdrawMenu;
if (withdrawMenu == 1){
[Code] ....
View 1 Replies
View Related
Apr 3, 2014
I am making a program that allows you to add two big numbers that are larger then what int can handle together. I think I have done everything to accomplish this but when I try to instantiate the program I get a error Expression must have a class type.
Here is my main file that is supposed to instantiate the program.
Code: #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <conio.h>
#include "LargeIntegers.h"
using namespace std;
[code]....
View 10 Replies
View Related
Jun 18, 2014
I wrote a program, that generates 20 random integers, and stores them in an array. Then I decided, to build more functions to it, i.e why not have it to display the lowest integer of an array. I created my function,
Code:
int minValue( int field[ ] )
and got my code in side, which (technically) works. In my main() function I'm calling
Code:
printf( "The smallest value of an array is: %d
", minValue( field[] ) );
and I'm getting an error trying to compile it.
Code:
randomArray.c:62:74: error: expected expression before ']' token
printf( "The smallest value of an array is: %d
", minValue( field[] ) );
View 2 Replies
View Related
Oct 23, 2013
I am trying to create a NFA from a regular expression. I have a grasp on reading in the regular expression and being able to make a stack from it. The part I am struggling on is mapping the characters in the regular expression to an integer indicating the variables order in the expression. I am just not sure how to go about this.
My code so far...
Code:
#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
int main(void)
{
char expression[80];//array to store regular expression
[Code] .....
View 6 Replies
View Related
Feb 3, 2013
I am using code::blocks for c programming and when i take debugger in below code it show ..
a syntax error in expression near "if"..
I am just checking debugger ...
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[100];
[Code] ....
View 7 Replies
View Related
Mar 6, 2015
I would like to ask about how we calculates the following bitwise expression.
Code:
unsigned char ch[2] = {0x49,0x49};
ch[0] | ch[1] << 8; I'm thinking ch[1] << 8 as 0x00 ...
So, I think that the above expression converts to 0x49 | 0x00 ... and the complete expression should be 0x49 for me.
But, the compiler gives me the result of 0x4949 as two bytes.How does the compiler calculate this expression as two bytes?show me the steps included in the calculation of this expression?
View 2 Replies
View Related
May 26, 2014
This is what i have example code in c++:
#include <iostream>
class Foo{
public:
void bar(){
std::cout << "Hello" << std::endl;
[Code] ....
After compiling it is giving error as :
foo.cpp: In function ‘int Foo_max(Foo*)’:
foo.cpp:26:37: error: expected primary-expression before ‘int’
foo.cpp:26:46: error: expected primary-expression before ‘int’
View 6 Replies
View Related
Dec 17, 2014
How is the definition of the term "expression" affected by value returning functions, and why?
View 5 Replies
View Related