1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <SFML/Graphics.hpp>
#include <queue>
#include <iostream>
using namespace std;
sf::RenderWindow window(sf::VideoMode(1070, 920), "SFML works!");
struct node
{
int data;
node *left, *right, *parent;
sf::CircleShape shape;
sf::RectangleShape leftone;
sf::RectangleShape rightone;
}*root;
class Tree
{
public:
Tree();
void insert(int, node*, int, int, int, int);
void Display(node*);
void inOrder(node*);
void PostOrder(node*);
void PreOrder(node*);
bool isEmpty();
void Deletion(int);
node* SearchNode(int, node*);
};
bool Tree::isEmpty()
{
if (!root)
return true;
return false;
}
Tree::Tree() { root = NULL; }
void Tree::insert(int data, node* Node = root, int x = 250, int y = 50, int a = 260, int b = 85)
{
node* new_node = new node;
new_node->shape.setRadius(20);
new_node->shape.setFillColor(sf::Color::Green);
new_node->left = new_node->right = new_node->parent = NULL;
new_node->data = data;
new_node->shape.setPosition(sf::Vector2f(x, y));
if (!root)
{
root = new_node;
return;
}
if (data >= Node->data)
{
if (Node->right)
insert(data, Node->right, x + 50, y + 50, a + 50, b + 50);
else
{
Node->right = new_node;
new_node->parent = Node;
new_node->shape.setPosition(sf::Vector2f((x + 50), (y + 50)));
new_node->parent->rightone.setSize(sf::Vector2f(50, 5));
new_node->parent->rightone.setFillColor(sf::Color::Red);
new_node->parent->rightone.setRotation(230);
new_node->parent->rightone.setPosition(sf::Vector2f(a+50, b+40));
}
}
else if (data < Node->data)
{
if (Node->left)
insert(data, Node->left, x - 50, y + 50, a - 50, b + 50);
else
{
Node->left = new_node;
new_node->parent = Node;
new_node->shape.setPosition(sf::Vector2f((x - 50), (y + 50)));
new_node->parent->leftone.setSize(sf::Vector2f(50, 5));
new_node->parent->leftone.setFillColor(sf::Color::Red);
new_node->parent->leftone.setRotation(130);
new_node->parent->leftone.setPosition(sf::Vector2f(a, b));
}
}
}
void Tree::Display(node* Root = root)
{
if (!root)
{
cout << "Nothing to Display";
return;
}
queue<node*> obj;
obj.push(Root);
while (!obj.empty())
{
node* temp = obj.front();
window.draw(temp->shape);
if (temp->left)
window.draw(temp->leftone);
if (temp->right)
window.draw(temp->rightone);
//cout << temp->data << " ";
obj.pop();
if (temp->left)
obj.push(temp->left);
if (temp->right)
obj.push(temp->right);
}
window.display();
}
int main()
{
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
int n;
Tree obj;
while (window.isOpen())
{
cout << "Enter Number to Insert: ";
cin >> n;
obj.insert(n);
sf::Event event;
obj.Display();
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
}
return 0;
}
|