Search Results

Search found 1 results on 1 pages for 'user188995'.

Page 1/1 | 1 

  • Return parent of node in Binary Tree

    - by user188995
    I'm writing a code to return the parent of any node, but I'm getting stuck. I don't want to use any predefined ADTs. //Assume that nodes are represented by numbers from 1...n where 1=root and even //nos.=left child and odd nos=right child. public int parent(Node node){ if (node % 2 == 0){ if (root.left==node) return root; else return parent(root.left); } //same case for right } But this program is not working and giving wrong results. My basic algorithm is that the program starts from the root checks if it is on left or on the right. If it's the child or if the node that was queried else, recurses it with the child.

    Read the article

1