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 812955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:16:56+00:00 2026-05-15T01:16:56+00:00

I’m trying to make a binary tree in scala and need to make a

  • 0

I’m trying to make a binary tree in scala and need to make a method for it so I’m trying to make functions inside the class that deals with children and parent.
I want to make the parent a Tree so that I can recursively call it in another function called getPath but I can’t create a Tree inside the Tree class.
this is the code:

case class Tree[+T](value: T, left: Option[Tree[T]], right: Option[Tree[T]]) {
   var parent: Tree[T] = null

   //method for setting the parent of tree.
   //this method returns the parent TREE instead of the parent value
   //so if you want to use it to find the value, you need to get the parent.value
   def setParent(tree: Tree[T]) {
 parent = tree
   }

   //method for returning the parent
   //the parent is a tree so you have to .value it to get the root
   def getParent(): Tree[T] = parent

   //setting parents of left child and right child if they are not empty trees
   if(left != None) {
      left.get.setParent(this)
   }
   if(right != None) {
      right.get.setParent(this)
   }
}

def getPath[T](tree: Tree[T]):List[T] = {
   if(tree.getParent == null) List(tree.value)
   List(tree.value)++getPath(tree.getParent())
}

I can set the T to Any and it’ll work but then I can’t recursively call it if you do.
Can anyone help me or has another way to get a tree’s parent?

  • 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-15T01:16:57+00:00Added an answer on May 15, 2026 at 1:16 am

    Cleaning up your code a little, I get to:

    case class Tree[+T](value: T, left: Option[Tree[T]], right: Option[Tree[T]]) {
      @reflect.BeanProperty   
      var parent: Tree[T] = null
    
      //setting parents of left child and right child if they are not empty trees
      Seq(left, right).flatten.foreach(_.setParent(this))
    }
    
    object Tree {
      def getPath[T](tree: Tree[T]):List[T] = List(tree.value) ++
        (if(tree.getParent == null) 
          Nil
        else
          getPath(tree.getParent()))
    }
    

    This fails to compile with:

    tree-parent.scala:1: error: covariant type T occurs in contravariant position in type Tree[T] of parameter of setter parent_=

    The type parameter T appears in types produced (getter for parent) and consumed (setter for parent) by this interface. Accordingly, it must be invariant:

    case class Tree[T]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.