Write your question here.
hy guya i have a problem with binary trees(im total beginer in binary trees today is first time when i do something with that stuff) i konw that my question is litle wierd but i realy dont know how can i call function displayinorder in main program can someone please help me??? thanks guys
hy guys i have one question now i play little bit with threes does couple new functions in program in now doesent work(shocking) :)....can pleae anyone help me with that problem...problem is when i deleting a number and then wont to display new numbers in order just threw me next fail:Exception thrown: read access violation.
First, You really need two classes here, a class for a node within a tree, and a class for the tree itself. The node class contains just the left and right pointers. The tree class contains a root pointer and all the methods. It's possible to write this so that the node class is within the tree class, but since you're a beginner, I'll assume that you don't know how to do that:
Second, I'd do displayInOrder() differently. Since it's a method, you can just have it operate on the object upon which it was called. The only challenge is that you then must check for nullptr in the code:
1 2 3 4 5 6
void displayInOrder() const
{
if (left) left->displayInOrder();
cout << value << endl;
if (right) right->displayInOrder();
}
and in main, you call it with: tree.displayInOrder();
Third, the constructor is wrong. You're passing intbinarySearch instances by value. That means it's pointing left and right to the parameters that immediately get destroyed, causing memory corruption. It should be :
1 2 3 4 5 6
intbinarySearch(intbinarySearch *l, intbinarySearch *r, int v)
{
left = l;
right = r;
value = v;
}
class node
{
private:
node *left;
node *right;
class tree
{
private:
int valuee;
tree *root;
};
public:
node(node *l, node*r)
{
left = l;
right = r;
}
};
Yes, but the other way around. The node class should be within the tree class, no tthe other way around. The node class is just part of the tree's implementation so the user shouldn't need to see it.
1 2 3 4 5 6 7 8
class Tree {
private:
class Node {
public:
...
};
....
};
hy guys i ve been on vacation to today....so today a play little with this trees and i just get it i know that was not that hard but i dont just dont know why i dont get it(sory for my english im not form UK or US)....can anyone tell me what i did wrong here??
class tree {
private:
// A node in the tree has a value, and left & right pointers
class Node {
public:
Node * left, *right;
int val=0;
// The constructor takes a value, left and right pointers,
// all of them are optional
Node(int v=0, Node *l=nullptr, Node *r=nullptr) :
left(l), right(r), val(v)
{}
};
// The only data member of the tree itself is the root pointer
Node * root=nullptr;
// The main insert method.
void insert(int val)
{
// Create a new node and call the recursive insert method
insert(root, new Node(val));
}
private:
// Here is the recursive insert method. It takes a REFERENCE to
// a Node ptr where the node should be inserted.
// If that ptr is null then insert there, otherwise insert in one
// of its children.
void insert(Node *& ptr, Node *newNode)
{
if (ptr == nullptr) {
ptr = newNode;
} elseif (newNode->val < ptr->val) {
insert(ptr->left, newNode);
} else {
insert(ptr->right, newNode);
}
}
};
hey does anoyone know a good(great) book aoubt data structures and that stuff?? because im doing the same program that i do in my first post here but in the first post i do with one class and now i do with class within class and i dont know how to do(abviusly)....beaside that i have just one question...how do you initialize two classes??
You don't have to. Tree::Node is just the name of a class. When you create a Tree object, it does not create a Node object also. This is the difference between declaring a class within a class, and have one class (tree) derive from another. If tree derived from Node then each instance of tree would basically contain an instance of Node within it. That instance would have to be initialized.
A tree does not have a value, or a left or right pointer. The only data in a tree is the root pointer.
Line 45: < should be >
Line 143: Since you want to change the value of newnode that was passed in,
you have to pass newNode by reference: void makeDeletion(Node * &newnode)