Thread: I want to input all of tree's items by one function!!How!!please help me...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    Unhappy I want to input all of tree's items by one function!!How!!please help me...

    I have a class Tree, I want to input all of tree's items by 1 function...I typed code for them..but I have a problems...I couldn't create all Node of Tree because everytime I create new Node,old Node will be overwrite...So InputTreeTemp(root) return root for InputTree(),Tree only has one Node. I don't know how to solve this problem..please help me..thanks very much...
    //root is main pointer of Tree that points to main item
    //InputTree is a method of class Tree

    Code:
    template <class Entry>
    void Tree<Entry>::InputTree(){
    	cout<<"Input main  item of Tree : ";
    	InputTreeTemp(root);
    }
    //InputTreeTemp is a temporary function
    //User input a new Node of tree,data = -1 if they don't want to input left Node or right Node
    //pduyet to create a new Node.


    Code:
    template <class Entry>
    void Tree<Entry>::InputTreeTemp(NodeTree<Entry> *& ptemp){
    	Entry data;
    	cin>>data;
    	if (data != -1)
    	{
    	NodeTree<Entry> * pduyet = new NodeTree<Entry>(data);
           	cout<<"Left Node "<<pduyet->item<<": ";
    	InputTreeTemp(ptemp->left);
    	cout<<"Right Node "<<pduyet->item<<": ";;
            InputTreeTemp(ptemp->right);
    	}
    	else
    		return;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You never assign anything to ptemp in InputTreeTemp(). That looks wrong to me.
    You did not provide enough information to be shure about that.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    So,What I have to do...Anyone have ideas??

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Entry data;
    cin>>data;
    After data is entered and checked - it is lost, you do nothing with it... Maybe you should store it in the ptemp node?
    Also I don't see where do you allocate the memory for right and left nodes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by vart
    After data is entered and checked - it is lost, you do nothing with it... Maybe you should store it in the ptemp node?
    Also I don't see where do you allocate the memory for right and left nodes
    Guess allocation of nodes it's ok. He allocates a new node pduyet with that data and then recoursively calls InputTreeTemp() with the pointers to the left and right nodes. I still think that he should assign the new nodes back to ptemp.
    It would be a good idea to provide a minimal compilable example that shows the problem.
    Kurt

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    He allocates a new node pduyet
    Ahhh, OK missed that line... But pduyet pointer is effectively lost, as far as I see
    I think it should be
    Code:
    ptemp = new NodeTree<Entry>(data);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM