Posts: 266
Threads: 239
Joined: Apr 2011
Sir, 1)My question is why we have declared these two TreeNode pointers left , right within the same TreeNode class, can we use other pointers like integer or etc? 2)If we have declared TreeNode pointers within the same class then these pointers also holds the value like Object , left and right? All I am asking cause behind doing this I have a sort of confusion with this, plz explain briefly.... Class TreeNode { private: Object * object; TreeNode * left; TreeNode * right; 62. }; // end class TreeNode };
Posts: 9,583
Threads: 8,089
Joined: Apr 2011
1. As the data type of left and right pointer is same as the parent class TreeNode. Therefore we declare left and right pointer with TreeNode data type. Therefore we can not use any other type of data like integer, float, char etc.
2. Object is a private data element of Object *, used to store the tree element (value) inside the node of the tree. Left is a private data member of type TreeNode*, it is used to store a pointer to the left sub-tree. Right is a private data member of type TreeNode *, employed to store a pointer to the right sub-tree.