I made my first binary tree a second ago. You can put values in, search for them, and destroy and create them froma class btree. There's also a struct called node that represents each part of the tree, it has the value, and the pointers that point the the left and right nodes, here's the code.
#include <iostream>
using namespace std;
struct node
{
int key_value;
node *left;
node *right;
};
class btree
{
public:
btree();
~btree();
void insert(int key);
node *search(int key);
void destroy_tree();
private:
void destroy_tree(node *leaf);
void insert(int key, node *leaf);
node *search(int key, node *leaf);
node *root;
};
btree::btree()
{
root=NULL;
}
btree::~btree()
{
destroy_tree();
}
void btree::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void btree::insert(int key, node *leaf)
{
if (key< leaf->key_value)
{
if (leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL;
leaf->left->right=NULL;
}
}
else if (key>= leaf->key_value)
{
if (leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL;
leaf->right->right=NULL;
}
}
}
node *btree::search(int key, node *leaf)
{
if (leaf!=NULL)
{
if (key==leaf->key_value)
return leaf;
if (key>=leaf->key_value)
return search(key, leaf->right);
if (key<leaf->key_value)
return search(key, leaf->left);
}
else return NULL;
}
void btree::insert(int key)
{
if (root!=NULL)
insert(key,root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
node *btree::search(int key)
{
return search(key,root);
}
void btree::destroy_tree()
{
if (root!=NULL)
destroy_tree(root);
}
int main()
{
btree tree;
tree.insert(15);
tree.insert(13);
tree.insert(17);
if (tree.search(12)!=NULL)
cout<<tree.search(12)->key_value;
cin.get();
tree.destroy_tree();
}
I tested it with the main part by putting in the first value, 15, and the left and right values, 13 and 17 respectively. The cout part prints the key value in the node that the search points to. However, if it can't find the value, it returns NULL. Which it should, but how would you test the existence of the node before cout without having to perfrom the function twice? In other words, what kind of variable do I need to put the result of the function in? I used coutt<<tree.search(12)->key_value, but since it returns NULL, it crashes the program. I tried making a pointer with int *ptr, and putting the result of the function in it, but it says, cannot convert from node to int. For giggles, I defined it with node instead of int, and I got more errors, but I didn't expect that to work anyway. So what should I do? Also, anytime I type '~', or '"', or '`' or even ''', I have to hit another key before it appears on the screen. I live in Germany, and when I started my computer for the first time, it asked me about a language zone or something. I thought it meant time zone, so I put Germany, but then I realize it had to do with stuff like the keyboard format and the language the date shows up on my computer. I can change it back to english, but anytime I minimize something or change windows, it switches back to German, making these weird keyboard things happen
Anyone ever had this problem. Thanks in advance for both questions
cout<<"I'm learning C++, and this is all I know
\n"