Hello I need help on a binary tree and paintbox!!!!!
i have a fuction insertnode and i want to show the nodes that i insert in a gafical way on a paintbox.
Can anyone help me please i have stuck for 3 weeks!!!!
i can post the code for the function.
Hello I need help on a binary tree and paintbox!!!!!
Started by DemonFox, Nov 23 2007 11:05 AM
3 replies to this topic
#1
Posted 23 November 2007 - 11:05 AM
|
|
|
#2
Posted 23 November 2007 - 01:41 PM
What have you got so far?
#3
Posted 26 November 2007 - 09:35 AM
Ok here it comes :P
i have a form which contains 1 editbox one button insert and one paintbox
The h file tree.h is:
struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
};typedef struct treenode *PTR;
class tree
{
private:
//PTR treeT;
PTR theData;
public:
tree();
tree(PTR t);
//~tree();
void insert_node(PTR *pt,int x);
void destroy_tree(PTR *pt);
int countNodes(PTR t);
void preorder_traversal(PTR t);
void inorder_traversal(PTR t);
void postorder_traversal(PTR t);
void find_node(PTR t,int x,int i);
};
the cpp file tree.cpp is
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tree.h"
#include"form_test.h"
tree::tree()
{
}
tree::tree(PTR t1)
{
PTR t;
t->data=t1->data;
t->left=t1->left;
t->right=t1->right;
}
void tree::insert_node(PTR *pt,int x)
{
PTR t;
t=*pt;
int pclh,pclw;
static int nodeheigthy1=0,nodeheigthy2=0,prevnodeheigthy1,prevnodeheigthy2;
int nodewidth;
static int nodewidthx1=0,nodewidthx2=0;
int type=2;
static int test=0;
pclh=Form1->PaintBox1->ClientHeight;
pclw=Form1->PaintBox1->ClientWidth;
Form1->Edit4->Text=pclh;
Form1->Edit5->Text=pclw;
nodewidth=pclw/2;
if(test==0)
{
nodeheigthy1=nodeheigthy1+10;
nodeheigthy2=nodeheigthy2+50;
nodewidthx1=nodewidth+20;
nodewidthx2=nodewidth-20;
test=1;
}
//nodewidthx1=nodewidth+10;
//nodewidthx2=nodewidth+50;
//nodeheigthy1=nodeheigthy1+20;
//nodeheigthy2=nodeheigthy2-20;
if (t==NULL)
{
//t=(PTR)malloc(sizeof(struct treenode));
t=new treenode;
t->data=x;
t->left=NULL;
t->right=NULL;
Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
}
else
if (x<t->data)
{
insert_node(&(t->left),x);
type=0;
}
else
{
insert_node(&(t->right),x);
type=1;
}
if(type==0)
{
nodewidthx1=nodewidthx1-40;
nodeheigthy1=nodeheigthy1+80;
nodewidthx2=nodewidthx2-40;
nodeheigthy2=nodeheigthy2+80;
Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
}
else if(type==1)
{
nodewidthx1=nodewidthx1+40;
nodeheigthy1=nodeheigthy1+80;
nodewidthx2=nodewidthx2+40;
nodeheigthy2=nodeheigthy2+80;
Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
}
*pt=t;
}
and the code in form
#include "form_test.h"
#include "tree.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
PTR bt;
//bt=NULL;
tree theTree;
int maxcount;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
theTree.insert_node(&bt,Edit1->Text.ToInt());
Edit1->Clear();
}
//---------------------------------------------------------------------------
I want every time i press the insert button a rectangle of the node to be drawn in the right place int heigth of the paintbox which is related to canvas.
the result i want is to have a graphical view of the tree so i can show later preorder postorder etc.
the problem is that it draws more than one rectangle at one press of the button
and it takes only the last node into consideration is there an another function i should add????which one????
please help
thanks in advance sorry for my english.
i don't how to add tags.
:irritated::irritated::irritated::irritated:
i have a form which contains 1 editbox one button insert and one paintbox
The h file tree.h is:
struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
};typedef struct treenode *PTR;
class tree
{
private:
//PTR treeT;
PTR theData;
public:
tree();
tree(PTR t);
//~tree();
void insert_node(PTR *pt,int x);
void destroy_tree(PTR *pt);
int countNodes(PTR t);
void preorder_traversal(PTR t);
void inorder_traversal(PTR t);
void postorder_traversal(PTR t);
void find_node(PTR t,int x,int i);
};
the cpp file tree.cpp is
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tree.h"
#include"form_test.h"
tree::tree()
{
}
tree::tree(PTR t1)
{
PTR t;
t->data=t1->data;
t->left=t1->left;
t->right=t1->right;
}
void tree::insert_node(PTR *pt,int x)
{
PTR t;
t=*pt;
int pclh,pclw;
static int nodeheigthy1=0,nodeheigthy2=0,prevnodeheigthy1,prevnodeheigthy2;
int nodewidth;
static int nodewidthx1=0,nodewidthx2=0;
int type=2;
static int test=0;
pclh=Form1->PaintBox1->ClientHeight;
pclw=Form1->PaintBox1->ClientWidth;
Form1->Edit4->Text=pclh;
Form1->Edit5->Text=pclw;
nodewidth=pclw/2;
if(test==0)
{
nodeheigthy1=nodeheigthy1+10;
nodeheigthy2=nodeheigthy2+50;
nodewidthx1=nodewidth+20;
nodewidthx2=nodewidth-20;
test=1;
}
//nodewidthx1=nodewidth+10;
//nodewidthx2=nodewidth+50;
//nodeheigthy1=nodeheigthy1+20;
//nodeheigthy2=nodeheigthy2-20;
if (t==NULL)
{
//t=(PTR)malloc(sizeof(struct treenode));
t=new treenode;
t->data=x;
t->left=NULL;
t->right=NULL;
Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
}
else
if (x<t->data)
{
insert_node(&(t->left),x);
type=0;
}
else
{
insert_node(&(t->right),x);
type=1;
}
if(type==0)
{
nodewidthx1=nodewidthx1-40;
nodeheigthy1=nodeheigthy1+80;
nodewidthx2=nodewidthx2-40;
nodeheigthy2=nodeheigthy2+80;
Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
}
else if(type==1)
{
nodewidthx1=nodewidthx1+40;
nodeheigthy1=nodeheigthy1+80;
nodewidthx2=nodewidthx2+40;
nodeheigthy2=nodeheigthy2+80;
Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
}
*pt=t;
}
and the code in form
#include "form_test.h"
#include "tree.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
PTR bt;
//bt=NULL;
tree theTree;
int maxcount;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
theTree.insert_node(&bt,Edit1->Text.ToInt());
Edit1->Clear();
}
//---------------------------------------------------------------------------
I want every time i press the insert button a rectangle of the node to be drawn in the right place int heigth of the paintbox which is related to canvas.
the result i want is to have a graphical view of the tree so i can show later preorder postorder etc.
the problem is that it draws more than one rectangle at one press of the button
and it takes only the last node into consideration is there an another function i should add????which one????
please help
thanks in advance sorry for my english.
i don't how to add tags.
:irritated::irritated::irritated::irritated:
#4
Posted 27 November 2007 - 09:42 AM
I'm not sure (not having Visual C++), but it looks like what's happening is that in the function insert_node, a node can be responsible for drawing both itself and a child. You'll have to trace through, but what I would recommend is that you make the drawing of the node occur in the constructor, so that each node is strictly responsible for drawing itself when created.


Sign In
Create Account

Back to top









