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

  • SEARCH
  • Home
  • 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 6838379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:35:23+00:00 2026-05-26T23:35:23+00:00

I am trying to create a tree structure (a binary tree) that is capable

  • 0

I am trying to create a tree structure (a binary tree) that is capable of holding two different types of class (a sphere and a rectangle).

For obvious reasons my sphere and rectangle will have different methods for getting their size (getSize()) and I also intend to have a constructor (for both classes) thats takes two objects (two spheres OR two rectangles) and combines them to create a larger sphere or rectangle.

How should I approach coding a node so that it can store either a sphere or a rectangle at a node calling the appropriate methods when required?

Would a simple interface accomplish this if i cast objects to the type i need?

Thanks,

DMcB

  • 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-05-26T23:35:24+00:00Added an answer on May 26, 2026 at 11:35 pm

    I would create three classes.
    An abstract class Shape that contains all common code for rectangles and spheres.

    public abstract class Shape{
       // contains all common code related to shapes
       // such as child elements
       Shape parentNode; // This will help navigate up
       List<Shape> children; // This will help navigate down the tree
       // Define, merge, split methods which are common to all shapes
       // define shape specific methods
    }
    public class Rectangle : Shape{
       // Implement shape's abstract methods for this class
    }
    
    public class Sphere : Shape{
       // Implement shape's abstract methods for this class
    }
    

    All shape specific methods should be left abstract such as getSize(), drawShape(), mergeShape().
    Also, maybe not relevant but the Composite design pattern might be of good use for this problem

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

Sidebar

Related Questions

I'm trying to create an expression tree dynamicly. Let asume that I have two
I'm trying to create routes which follow the structure of a tree navigation system,
I am trying to create an expression tree that represents the following: myObject.childObjectCollection.Any(i =>
I am trying to create a tree structure with graphviz. I am open to
I'm trying to make an application that can store a tree structure with files
I am trying to create a custom Binary Search Tree, and I have everything
Today i was told to create tree data structure with the below class, public
So I'm trying to create a tree structure in PHP. I don't know if
I need create a tree structure recursively. In the tree each node has different
I have categories that are in a tree structure. I am trying to link

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.