Skip to content

Instantly share code, notes, and snippets.

@dertst
Created August 7, 2019 10:14
Show Gist options
  • Save dertst/e87c0d420b5f159a18dc6ca67915660d to your computer and use it in GitHub Desktop.
Save dertst/e87c0d420b5f159a18dc6ca67915660d to your computer and use it in GitHub Desktop.
#include "pch.h"
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node {
int value;
struct Node *right;
struct Node *left;
};
void Show(Node *tree)
{
if (tree != NULL)
{
Show(tree->left);
cout << tree->value;
Show(tree->right);
}
}
void AddNode(int value, Node* MyTree)
{
if (MyTree == NULL)
{
MyTree = new Node;
MyTree->value = rand() % 100 + 1;
MyTree->left = MyTree->right = NULL;
}
if (value < MyTree->value)
{
if (MyTree->left != NULL)
{
AddNode(value, MyTree->left);
}
else
{
MyTree->left = new Node;
MyTree->left->left = MyTree->left->right = NULL;
MyTree->left->value = rand() % 100 + 1;
}
if (value > MyTree->value)
{
if (MyTree->right != NULL)
{
AddNode(value, MyTree->right);
}
else
{
MyTree->right = new Node;
MyTree->right->right = MyTree->right->right = NULL;
MyTree->right->value = rand() % 100 + 1;
}
}
}
}
int main()
{
Node *tree = NULL;
for (int i = 5; i > 0; i--)
{
AddNode(value, Tree);
Show(tree);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment