implementation of to draw tree using graphics in c++.
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <sstream>
using namespace std;
void printTree(int x, int y, int* array,
int index,
int total_elements)
{
if (index >= total_elements)
return NULL;
ostringstream str1;
str1 << array[index];
string str2 = str1.str();
char* str = &str2[0u];
setcolor(GREEN);
circle(x, y, 15);
floodfill(x, y, GREEN);
outtextxy(x - 2, y - 3, str);
setcolor(GREEN);
int left = 2 * index + 1;
int right = 2 * index + 2;
printTree(x - y / (index + 1), y + 50,
array, left, total_elements);
printTree(x + y / (index + 1), y + 50,
array, right, total_elements);
if (left < total_elements) {
line(x, y, x - y / (index + 1), y + 50);
}
if (right < total_elements) {
line(x, y, x + y / (index + 1), y + 50);
}
return NULL;
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "None");
int array[] = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
getch();
closegraph();
}
output:
Leave a comment