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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:48:37+00:00 2026-06-01T18:48:37+00:00

I have a function that calculates the left and right node values for some

  • 0

I have a function that calculates the left and right node values for some collection of treeNodes given a simple node.id, node.parentId association. It’s very simple and works well enough…but, well, I am wondering if there is a more idiomatic approach. Specifically is there a way to track the left/right values without using some externally tracked value but still keep the tasty recursion.

/* 
 * A tree node
 */
case class TreeNode(val id:String, val parentId: String){
    var left: Int = 0
    var right: Int = 0
}

/* 
 * a method to compute the left/right node values 
 */
def walktree(node: TreeNode) = {
    /* 
     * increment state for the inner function 
     */
    var c = 0

    /*
     * A method to set the increment state
     */
    def increment = { c+=1; c } // poo

    /* 
     * the tasty inner method
     * treeNodes is a List[TreeNode]
     */
    def walk(node: TreeNode): Unit = {
      node.left = increment

      /* 
       * recurse on all direct descendants 
       */
      treeNodes filter( _.parentId == node.id) foreach (walk(_))

      node.right = increment
    }

    walk(node)
}

walktree(someRootNode)

Edit –
The list of nodes is taken from a database. Pulling the nodes into a proper tree would take too much time. I am pulling a flat list into memory and all I have is an association via node id’s as pertains to parents and children.

Adding left/right node values allows me to get a snapshop of all children (and childrens children) with a single SQL query.

The calculation needs to run very quickly in order to maintain data integrity should parent-child associations change (which they do very frequently).

In addition to using the awesome Scala collections I’ve also boosted speed by using parallel processing for some pre/post filtering on the tree nodes. I wanted to find a more idiomatic way of tracking the left/right node values. After looking at the answer from @dhg it got even better. Using groupBy instead of a filter turns the algorithm (mostly?) linear instead of quadtratic!

val treeNodeMap = treeNodes.groupBy(_.parentId).withDefaultValue(Nil)

def walktree(node: TreeNode) = {
    def walk(node: TreeNode, counter: Int): Int = {
        node.left = counter 
        node.right = 
          treeNodeMap(node.id) 
          .foldLeft(counter+1) {
            (result, curnode) => walk(curnode, result) + 1
        }
        node.right
    }
    walk(node,1)
}
  • 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-01T18:48:39+00:00Added an answer on June 1, 2026 at 6:48 pm

    Your code appears to be calculating an in-order traversal numbering.

    I think what you want to make your code better is a fold that carries the current value downward and passes the updated value upward. Note that it might also be worth it to do a treeNodes.groupBy(_.parentId) before walktree to prevent you from calling treeNodes.filter(...) every time you call walk.

    val treeNodes = List(TreeNode("1","0"),TreeNode("2","1"),TreeNode("3","1"))
    
    val treeNodeMap = treeNodes.groupBy(_.parentId).withDefaultValue(Nil)
    
    def walktree2(node: TreeNode) = {
      def walk(node: TreeNode, c: Int): Int = {
        node.left = c
        val newC = 
          treeNodeMap(node.id)         // get the children without filtering
            .foldLeft(c+1)((c, child) => walk(child, c) + 1)
        node.right = newC
        newC
      }
    
      walk(node, 1)
    }
    

    And it produces the same result:

    scala> walktree2(TreeNode("0","-1"))
    scala> treeNodes.map(n => "(%s,%s)".format(n.left,n.right))
    res32: List[String] = List((2,7), (3,4), (5,6))
    

    That said, I would completely rewrite your code as follows:

    case class TreeNode(        // class is now immutable; `walktree` returns a new tree
      id: String,
      value: Int,               // value to be set during `walktree` 
      left: Option[TreeNode],   // recursively-defined structure
      right: Option[TreeNode])  //   makes traversal much simpler
    
    def walktree(node: TreeNode) = {
      def walk(nodeOption: Option[TreeNode], c: Int): (Option[TreeNode], Int) = {
        nodeOption match {
          case None => (None, c)  // if this child doesn't exist, do nothing
          case Some(node) =>      // if this child exists, recursively walk
            val (newLeft, cLeft) = walk(node.left, c)        // walk the left side
            val newC = cLeft + 1                             // update the value
            val (newRight, cRight) = walk(node.right, newC)  // walk the right side
            (Some(TreeNode(node.id, newC, newLeft, newRight)), cRight)
        }
      }
    
      walk(Some(node), 0)._1
    }
    

    Then you can use it like this:

    walktree(
      TreeNode("1", -1,
        Some(TreeNode("2", -1,
          Some(TreeNode("3", -1, None, None)),
          Some(TreeNode("4", -1, None, None)))),
        Some(TreeNode("5", -1, None, None))))
    

    To produce:

    Some(TreeNode(1,4,
      Some(TreeNode(2,2,
        Some(TreeNode(3,1,None,None)),
        Some(TreeNode(4,3,None,None)))),
      Some(TreeNode(5,5,None,None))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have a Javascript function that calculates a value and re-inserts the value into
I have some code that looks like this (I left out the parts that
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1
I am using visual studio 2008, asp.net.vb I have various functions; 1 that calculates
In my Java code I have function that gets file from the client in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that accepts a class (not an instance) and, depending on
I have a function that is being called from different threads in the application.
I have a function that allows users to edit a row in a table

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.