Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:48:38+00:00 2026-06-10T17:48:38+00:00

I am learning algorithms and data structures and to train I am trying to

  • 0

I am learning algorithms and data structures and to train I am trying to design and implement a binary tree using objective-c.

So far I have the following Classes:

  • main – for testing
  • Node – node of tree
  • BinaryTree – for all methods related to the tree

One of the first methods in BinaryTree class I implemented is insertNode:forRoot:.

- (void)insertNodeByRef:(Node **)node forRoot:(Node **)root{

    if (head == NULL) {
        head = *node;
    }
    // Case 2 root is null so can assign the value of the node to it
    if (root == NULL) {
        root = node;
    } else {
        if (node.data > root.data) { // to the right
            [self insertNode:node forRoot:root.right];
        } else if (node.data < root.data) { //or to the left
            [self insertNode:node forRoot:root.left];
        }
    }
}

Where the interface of Node class looks like:

@interface Node : NSObject

@property(nonatomic, assign) int data;
@property(nonatomic, strong) Node * right;
@property(nonatomic, strong) Node * left;

@end

My problem is that I don’t know how to access the Node class member variables if I am passing Node as a reference. Whenever I try to access the node properties (like data, left or right) I am getting the following error message:

Member reference base type 'Node *__autoreleasing *' is not a structure or union

So my questions is:
how can I access those properties (data, left or right) and use them to store either int data or reference to another Node?

Hope it makes sense. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:48:39+00:00Added an answer on June 10, 2026 at 5:48 pm

    Your code is mixing two common approaches to the task, hence the problem. You are also using an abstract data type (ADT) type approach, rather than an object-oriented one, so there are three approaches to consider.

    In both ADT approaches your tree is represented by a reference to its root, in Objective-C this is probably stored in an instance variable:

    Node *TreeRoot;
    

    Note also that both of these algorithms use field references, a->b, rather than property references, a.b – this is because the former references a variable and the second algorithm requires passing a reference to a variable.

    Functional ADT: Pass-by-value and assign result

    In this approach a node is inserted into a tree and a modified tree is returned which is assigned back, e.g. the top-level call to insert a Node nodeToInsert would be:

    TreeRoot = insertNode(nodeToInsert, TreeRoot);
    

    and the insertNode function looks like:

    Node *insertNode(Node *node, Node *root)
    {
       if(root == nil)
       {  // empty tree - return the insert node
          return node;
       }
       else
       {  // non-empty tree, insert into left or right subtree
          if(node->data > root->data) // to the right
          {
            root->right = insertNode(node, root->right);
          }
          else if(node->data < root->data)//or to the left
          {
            root->left = insertNode(node, root->left);
          }
          // tree modified if needed, return the root
          return root;
       }
    }
    

    Note that in this approach in the case of a non-empty (sub)tree the algorithm performs a redundant assignment into a variable – the assigned value is what is already in the variable… Because of this some people prefer:

    Procedural ADT: Pass-by-reference

    In this approach the variable holding the root of the (sub)tree is passed-by-reference, rather than its value being passed, and is modified by the called procedure as needed. E.g. the top-level call would be:

    insertNode(nodeToInsert, &TreeRoot); // & -> pass the variable, not its value
    

    and the insertNode procedure looks like:

    void insertNode(Node *node, Node **root)
    {
       if(*root == nil)
       {  // empty tree - insert node
          *root = node;
       }
       else
       {  // non-empty tree, insert into left or right subtree
          Node *rootNode = *root;
          if(node->data > rootNode->data) // to the right
          {
            insertNode(node, &rootNode->right);
          }
          else if(node->data < rootNode->data)//or to the left
          {
            insertNode(node, &root->left);
          }
       }
    }
    

    You can now see that your method is a mixture of the above two approaches. Both are valid, but as you are using Objective-C it might be better to take the third approach:

    Object-Oriented ADT

    This is a variation of the procedural ADT – rather than pass a variable to a procedure the variable, now called an object, owns a method which updates itself. Doing it this way means you must test for an empty (sub)tree before you make a call to insert a node, while the previous two approaches test in the call. So now we have the method in Node:

    - (void) insert:(Node *)node
    {
       if(node.data > self.data) // using properties, could also use fields ->
       {
          if(self.right != nil)
             [self.right insert:node];
          else
             self.right = node;
       }
       else if(node.data < rootNode.data)
       {
          if(self.left != nil)
             [self.left insert:node];
          else
             self.left = node;
       }
    }
    

    You also need to change the top level call to do the same test for an empty tree:

    if(TreeRoot != nil)
       [TreeRoot insert:nodeToInsert];
    else
       TreeRoot = nodeToInsert;
    

    And a final note – if you are using MRC, rather than ARC or GC, for memory management you’ll need to insert the appropriate retain/release calls.

    Hope that helps you sort things out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Data Structures & Algorithms now. My lecture notes have an implementation of
I was learning Adam Drozdek's book Data Structures and Algorithms in C++, well, I
I'm learning about linear congruential generator in an algorithms and data structures course. After
I am tasked with detecting anomalies (known or unknown) using machine-learning algorithms from data
I'm learning data structures and algorithms, and here is a question that I'm stuck
I am a beginner to programming. Should I learn data structures and algorithms, and
I`m writing binary search tree template for two reasons - learning C++ and learning
Machine learning algorithms in OpenCV appear to use data read in CSV format. See
I am a student trying to use some of the machine learning algorithms for
I'm learning about data structures and abstract data types, and I keep getting stuck

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.