Thread: Hello I need help on a binary tree and paintbox!!!!!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    Hello I need help on a binary tree and paintbox!!!!!

    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.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    i can post the code for the function.
    Sure, please do. Nothing much else is going to happen unless you do.

    Also post a description of your problem. Is it a compiler error? Which line do your error(s) occur on? (Highlight them with colour.) Is it a linker error? Do you get a runtime error?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    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:

    Code:
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It doesn't look like you're using anything from <conio.h> (such as getch(), getche(), or clrscr()), so consider not including it. conio.h is non-standard anyway, so you should avoid it if at all possible.

    (Though I see you're using other non-standard things like #pragma, so perhaps you don't care about portability too much.)

    I'm not familiar with whatever GUI library you're using, but it seems to me that this (Borland?) code
    Code:
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    	theTree.insert_node(&bt,Edit1->Text.ToInt());
    	Edit1->Clear();
    }
    inserts a new node into the list, with the data in the Edit1 textbox or whatever, and then clears the textbox. Which is presumably how you add data to the list. Right, good so far.

    Then it looks like calls to Form1->PaintBox1->Canvas->Rectangle() are what paints the rectangles. However, with every call to insert_node(), you only call Rectangle() once. Perhaps you'll need another function which paints every node in the list.

    I think you'll have to describe the problem a little better. You have a form, which has a textbox, a button, and a Canvas where you can paint stuff. Typing into the textbox and clicking the button adds a node to the list with the data that was in the textbox, right? And then every time you add a new node, you want to paint a rectangle on the canvas representing the node?

    It seems to me that the canvas must be persistent, or you'd be redrawing the rectangles over and over again. So that means that you could have all of the nodes on the canvas without remembering the previous ones. However, remember that you'd need to paint the new nodes in a different place on the canvas -- otherwise you'd overwrite the previous one and probably get an unreadable mess.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    i Thought want you told me and created e new fuction draw_node().
    which has the follwing code.(Yes I am working on borland )

    Code:
    void tree::insert_node(PTR *pt,int x)
    {
    	PTR t;
    	t=*pt;
                    if (t==NULL)
    	{
    	    t=new treenode;
    	    t->data=x;
    	    t->left=NULL;
                        t->right=NULL;
                      
    	else
    	if (x<t->data)
    	{
    		insert_node(&(t->left),x);
    		t->data=x;
    
    	}
    	else
    	{
    		insert_node(&(t->right),x);
    		t->data=x;
    	}
                    *pt=t;
    	draw_tree(&t);
    }
    
    
    void tree::draw_tree(PTR *pt)
    {
    	PTR t;
    	t=*pt;
    	int pclh,pclw,n,j;
    	static int nodeheigthy1=0,nodeheigthy2=0;
    	static int prevnodeheigthy1left[20],prevnodeheigthy2left[20],prevnodewidthx1left[20],prevnodewidthx2left[20];
    	static int prevnodeheigthy1right[20],prevnodeheigthy2right[20],prevnodewidthx1right[20],prevnodewidthx2right[20];
    	int nodewidth;
    	static int nodewidthx1=0,nodewidthx2=0;
    	int type=2;
    	static int test=0,jl=0,jr=0;
    	static int count=0,nodecountleft=0,nodecountright=0;
    	static int prevx[20];
    	//Form1->PaintBox1->Repaint();
    	//count=countNodes(*pt);
    	pclh=Form1->PaintBox1->ClientHeight;
    	pclw=Form1->PaintBox1->ClientWidth;
    	Form1->Edit4->Text=pclh;
    	Form1->Edit5->Text=pclw;
    	Form1->Edit8->Text=prevx[count];
    	Form1->Edit9->Text=t->data;
    	Form1->Edit10->Text=prevnodeheigthy1left[jl];
    	Form1->Edit11->Text=prevnodeheigthy2left[jl];
    	Form1->Edit12->Text=prevnodewidthx1left[jl];
    	Form1->Edit13->Text=prevnodewidthx2left[jl];
    	Form1->Edit18->Text=prevnodeheigthy1right[jr];
    	Form1->Edit19->Text=prevnodeheigthy2right[jr];
    	Form1->Edit20->Text=prevnodewidthx1right[jr];
    	Form1->Edit21->Text=prevnodewidthx2right[jr];
    	nodewidth=pclw/2;
    if(test==0)
    	{
    		nodeheigthy1=nodeheigthy1+10;
    		Form1->Edit14->Text=nodeheigthy1;
    		nodeheigthy2=nodeheigthy2+50;
    		Form1->Edit15->Text=nodeheigthy2;
    		nodewidthx1=nodewidth+20;
    		Form1->Edit16->Text=nodewidthx1;
    		nodewidthx2=nodewidth-20;
    		Form1->Edit17->Text=nodewidthx2;
    		Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
    		test=1;
    		prevx[count]=t->data;
    		//Form1->Memo1->Lines->Insert(0,prevx[count]);
    		jl++;
    		jr++;
    		prevnodeheigthy1left[jl]=nodeheigthy1;
    		prevnodeheigthy2left[jl]=nodeheigthy2;
    		prevnodewidthx1left[jl]=nodewidthx1;
    		prevnodewidthx2left[jl]=nodewidthx2;
    		prevnodeheigthy1right[jr]=nodeheigthy1;
    		prevnodeheigthy2right[jr]=nodeheigthy2;
    		prevnodewidthx1right[jr]=nodewidthx1;
    		prevnodewidthx2right[jr]=nodewidthx2;
    	}
    	if(t->left!=NULL&&jl!=count)
    	{
    		nodewidthx1=nodewidthx1-40;
    		nodeheigthy1=nodeheigthy1+80;
    		nodewidthx2=nodewidthx2-40;
    		nodeheigthy2=nodeheigthy2+80;
    		Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
    		Form1->Edit8->Text=prevx[count-1];
    		Form1->Edit10->Text=prevnodeheigthy1left[jl];
    		Form1->Edit11->Text=prevnodeheigthy2left[jl];
    		Form1->Edit12->Text=prevnodewidthx1left[jl];
    		Form1->Edit13->Text=prevnodewidthx2left[jl];
    		draw_tree(&(t->left));
    		count++;
    		prevx[count]=t->data;
    		jl++;
    		prevnodeheigthy1left[jl]=nodeheigthy1;
    		prevnodeheigthy2left[jl]=nodeheigthy2;
    		prevnodewidthx1left[jl]=nodewidthx1;
    		prevnodewidthx2left[jl]=nodewidthx2;
    
    	}
    	else if(t->right!=NULL&&jr!=count)
    	{
    		nodewidthx1=nodewidthx1+40;
    		nodeheigthy1=nodeheigthy1+80;
    		nodewidthx2=nodewidthx2+40;
    		nodeheigthy2=nodeheigthy2+80;
    		Form1->PaintBox1->Canvas->Rectangle(nodewidthx1,nodeheigthy1,nodewidthx2,nodeheigthy2);
    		Form1->Edit8->Text=prevx[count-1];
    		Form1->Edit10->Text=prevnodeheigthy1right[jr];
    		Form1->Edit11->Text=prevnodeheigthy2right[jr];
    		Form1->Edit12->Text=prevnodewidthx1right[jr];
    		Form1->Edit13->Text=prevnodewidthx2right[jr];
    		draw_tree(&(t->right));
    		count++;
    		prevx[count]=t->data;
    		jr++;
    		prevnodeheigthy1right[jr]=nodeheigthy1;
    		prevnodeheigthy2right[jr]=nodeheigthy2;
    		prevnodewidthx1right[jr]=nodewidthx1;
    		prevnodewidthx2right[jr]=nodewidthx2;
    	}
    	Form1->Edit6->Text=jl;
    	Form1->Edit7->Text=jr;
    	Form1->Edit14->Text=nodeheigthy1;
    	Form1->Edit15->Text=nodeheigthy2;
    	Form1->Edit16->Text=nodewidthx1;
    	Form1->Edit17->Text=nodewidthx2;
    	t->left=NULL;
    	t->right=NULL;
    	//for(j=0;j<1;j++)
    	//{
    		//Form1->Memo1->Lines->Insert(0,prevx[count]);
    	//}
    	*pt=t;
    
    }
    Now i have managed to keep all the x data which is the number(data) of the node of the tree and all left all the right x1,x2,y1,y2 the cordinated of the rectangle.
    the problem now is that i dont know how to paint them in the right order. what i mean:
    when you insert the number 5 it is painted in the middle.ok? then if you enter 3 it goes left then 2 it goes left of the 3 and if you enter 4 right if 3 ok?
    now in my proggram it goes left or right from the last node you have. this means that when you enter 4 it will go to the right of 2.
    how can i print them in the right order? any idea??? Should i separate the tree in levels????Do you know how it is done????You are correct what you say on your reply.
    Sorry for my english .
    if you have (borland) i can sent you the programm to see how it works.
    thanks for repliing and thanks in advance.
    if you have more questions ask.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, unfortunately, I don't have Borland. I shouldn't need it, though. You could attach the source code and executable (rename it to .txt or something).

    As far as I can tell from your response, here's what's happening. Even new node you enter must be inserted into the correct place numerically -- that is, 1 comes before 2 which comes before 3 and so on. However, this means that if 1 and 3 are the previous nodes entered, and you enter 2, the nodes will have to be shifted to accomodate this new one. Is that right?

    Well then, there are probably two things you need to do here. First, you need to keep the list in sorted order. With a list of 1 and 3, when you insert a 2, do it between 1 and 3; don't just stick it on the end or the beginning to end up with 2, 1, 3 or 1, 3, 2. How you do this is up to you. Since you seem to have a binary-tree-like data structure, you might be doing this automatically already. Now that I look at insert_node() again, it seems that you are.

    The second thing you need to do is to re-paint the entire canvas with every node every time a new node is inserted. (Or clear out only the nodes you need to, then shift them -- but that's probably too complicated for this application.) This seems to be what you're trying to do, except you probably need to search through your data structure to find the place where the new node goes, rather than assuming it goes left or right of the previous one. Or, perhaps a better idea: you could iterate over the tree in such a way that you access the leftmost (lowest) node first, and the rightmost (highest) node last. Are you familiar with the right-hand rule (the maze kind, nothing to do with chemistry)? It's the same basic principle. Take the left node until there are no more left nodes, and then backtrack. It's simple to implement with
    Code:
    void traverse_in_order(pointer p) {
        if(p->left) traverse_in_order(p->left);
        print_data(p);
        if(p->right) traverse_in_order(p->right);
    }
    I think that's what's going on in your code, although I'm not sure. It's a bit hard to understand. You might want to break up some of that code into functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed