Created
October 14, 2021 13:35
-
-
Save PKOfficial/1040193754ac74898f79f63f3a1bcb22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class BinaryTree(info: String, left: Option[BinaryTree], right: Option[BinaryTree]) | |
object Application extends App { | |
val firstLeftFirstLeft = BinaryTree("firstLeftFirstLeft", None, None) | |
val firstLeftFirstRight = BinaryTree("firstLeftFirstRight", None, None) | |
val firstRightFirstLeft = BinaryTree("firstRightFirstLeft", None, None) | |
val firstLeft = BinaryTree("firstLeft", Some(firstLeftFirstLeft), Some(firstLeftFirstRight)) | |
val firstRight = BinaryTree("firstRight", Some(firstRightFirstLeft), None) | |
val rootNode = BinaryTree("rootNode", Some(firstLeft), Some(firstRight)) | |
def countNode(rootNode: BinaryTree) = { | |
def countNodeReccur(rootNode: BinaryTree): Int = rootNode match { | |
case BinaryTree(name, Some(left), Some(right)) => | |
1 + countNodeReccur(left) + countNodeReccur(right) | |
case BinaryTree(name, None, Some(right)) => | |
1 + countNodeReccur(right) | |
case BinaryTree(name, Some(left), None) => | |
countNodeReccur(left) + 1 | |
case BinaryTree(name, None, None) => | |
1 | |
} | |
countNodeReccur(rootNode) | |
} | |
val count = countNode(rootNode) | |
println("My Count " + count) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment