Thread: recursive check if a node can be accessed by two other nodes in a Binary Tree

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    10

    recursive check if a node can be accessed by two other nodes in a Binary Tree

    Hi again,

    This is the last function i need to consider.
    it needs to check each node if there are two or more nodes connected to it. (usually connected to 1 node only, or none if its the root node)
    I cannot think of an idea on how to do this check, any ideas?

    Code:
    bool checknode(CPPtr SP){
        bool chkl = false;
        bool chkr = false;
        bool chk = false;
        if(SP==NULL){
            return false;    
        }
        else {
    
    
            chkl = checknode(SP->left);
            chkr = checknode(SP->right);
            
            if()
            //if two nodes can access the same node
            //return true
    
             if(chkl||chkr){			chk = true;
    		}
    	}
    	return chk;
            
         
    
    
    }
    Last edited by Xu Zhang; 03-28-2013 at 03:41 PM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If you want to get the number of parents, you need a parent counter for each node, and then:

    1. Reset counter for every node to 0.
    2. Recursively visit nodes. For each visited node, increment its counter. If the counter reaches 1, continue visiting its children, otherwise return.

    Each node will hold the number of parent nodes (assuming that there are no cycles).

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by kmdv View Post
    If you want to get the number of parents, you need a parent counter for each node, and then:

    1. Reset counter for every node to 0.
    2. Recursively visit nodes. For each visited node, increment its counter. If the counter reaches 1, continue visiting its children, otherwise return.

    Each node will hold the number of parent nodes (assuming that there are no cycles).
    Hi thanks, is it achievable with 1 function?
    the code i came up with doesnt seems to go over 1 ever
    Code:
    bool checknode(CPPtr SP){    bool chkl = false;
        bool chkr = false;
        bool chk = false;
        if(SP==NULL){
            return false;    
        }
        else {
            
    
    
            chkl = checknode(SP->left);
            chkr = checknode(SP->right);
             
            
            SP->count++;
            
    
    
            if(SP->count>1){
                return true;        //return true if a node is called twice
            }
            
    
    
            if(chkl==true||chkr==true){
                chk = true;
            }
            }
        return chk;
        }

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    the code i came up with doesnt seems to go over 1 ever
    How would it? You are returning a boolean value.

    By the by, I don't think kmdv intended that you should make count a member of the structure. The information would need to be updated during every tree mutation or reset during your counting operation. You can just use the stack, the recursion, to process this information.

    Soma

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by phantomotap View Post
    How would it? You are returning a boolean value.

    By the by, I don't think kmdv intended that you should make count a member of the structure. The information would need to be updated during every tree mutation or reset during your counting operation. You can just use the stack, the recursion, to process this information.

    Soma
    yah.. i meant the counter doesnt go over 1
    i'll try without count in the structure

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    but i'm not sure i get the idea of count just being in the stack, my right sub tree may have connected to some node to my left sub tree. Very confused on how to implement this count even if the count is in the structure :S

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    I'm unable to increment my count after i set it to 0.
    Code:
    SP->count = 0;		
    		if(SP->count == 0){
    		cout<<SP->id<<"  "<<SP->data<<"  "<<SP->count<<endl;
    
    
    			SP->count++;
    		}
    
    
    
    
    		if(SP->count>1){
    			return true;		//return true if a node is called more than once
    		}
    	
    
    
    		if(chkl==true||chkr==true){
    			chk = true;
    		}
    		chkl = checknode(SP->left);
    		chkr = checknode(SP->right);

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    problem solved! make another function to set count to 0, the other one to increment and check howmany times it has been visitied

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print all nodes of the same level from binary tree
    By budala in forum C Programming
    Replies: 3
    Last Post: 09-08-2010, 06:31 AM
  2. Question about computing the number of nodes in a Binary Tree
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 12-12-2009, 01:10 PM
  3. Binary Tree - sum of nodes
    By Tiffanie in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2008, 08:49 AM
  4. Binary tree not inserting nodes correctly
    By jk1998 in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2007, 12:37 PM
  5. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 06:19 AM