binary tree begining, need help

This is a discussion on binary tree begining, need help within the C++ Programming forums, part of the General Programming Boards category; hi. I am working on binary tree, but jst want to see is everything looking good so far. code: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    binary tree begining, need help

    hi. I am working on binary tree, but jst want to see is everything looking good so far.
    code:
    Code:
    #include<iostream>
    #include<stdio.h>
    #include <string>
    using namespace std;
    struct node
    {
      string key_value;
      node *left;
      node *right;
    }*p_akt;
    class btree
    {
        public:
            btree();
            ~btree();
    		void create();
    		bool Lchild(node);
    		bool Rchild(node);
    		void make(node,string question,string animal);
    		void data(node *p);
    		void root();
    		void Parent(node *p,node * root);
    		int brojac;
            //void insert(string key);
            //node *search(string key);
            //void destroy_tree();
    	private:
    		
    		
    };
    
    btree::btree()
    {
    	p_akt=0;
    	brojac=0;
     // root=NULL;
    }
    
    void btree::create()
    {
    	node * temp=p_akt;
    	
    }
    
    void btree::make(node,string question,string animal)
    {
    	node * temp=p_akt;
    	temp=p_akt;
    	temp->left=new node;
    	temp->left->key_value= question;
    	temp->left->right=new node;
    	temp->left->right->key_value= animal;
    }
    
    bool Lchild(node)
    {
    	node * temp = p_akt;
    	if(temp->left!=NULL)
    	{
    		temp=temp->left;
    		return true;
    	}
    	else
    		return false;
    }
    bool Rchild(node *p)
    {
    	node * temp = p;
    	if(temp->right!=NULL)
    	{
    		temp=temp->right;
    		return true;
    	}
    	else
    		return false;
    }
    void btree::data(node *p)
    {
    	cout<< (string)p->key_value<<endl;
    }
    void btree::Parent(node *p,node *root)
    {
    	node * temp = root;
    	if (temp==NULL)
    		return;
    	else if  ((temp->left!=NULL) || (temp->right!=NULL))
    	{
    		if((temp->left->key_value==p->key_value) || (temp->right->key_value==p->key_value))
    			temp=p;
    	}
    	else
    	{
    		node * temp2=temp;
    		temp=temp->left;
    		Parent(p,temp);
    		temp2=temp->right;
    		Parent(p, temp2);
    	}
    
    }
    i need one more function, which will take me back to the root, but don't know how to write it.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Why not just save the root node in a dedicated pointer?

    Otherwise... it would sound like ye' would need something like a double-linked binary tree, where each node would point its leaf nodes... and also contain a pointer of the address of its parent node.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    thank you
    code is ok so far?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by ivan12yu2000 View Post
    thank you
    code is ok so far?
    No.
    Your create function doesn't do anything, and your Parent function doesn't do anything besides waste CPU cycles either.
    Your make function has an unused parameter and is not useful for building a tree in any useful manner.
    Lchild is broken.
    Lchild and Rchild have statements that have no effect.
    Many of your functions crash if given a NULL pointer.
    Rchild doesn't match the function prototype and the destructor has a prototype but no implementation, so this program will not successfully link.

    That's only the obvious problems. I haven't spent long looking at it.
    Don't try writing any more bits until you can get what you have working.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21