Scala 如何在二叉树中插入新节点?

Scala 如何在二叉树中插入新节点?,scala,binary-tree,Scala,Binary Tree,关于此代码,如何将节点插入到二叉树中: import util.Random import collection.mutable.ArrayBuffer import scala.collection.mutable.Queue import scala.collection.mutable.Stack case class Node[K](value:K, var left:Option[Node[K]], var right:Option[Node[

关于此代码,如何将节点插入到二叉树中:

import util.Random
import collection.mutable.ArrayBuffer
import scala.collection.mutable.Queue
import scala.collection.mutable.Stack

case class Node[K](value:K, var left:Option[Node[K]], 
                   var right:Option[Node[K]],var parent:Option[Node[K]]) {
    def hasLeft:Boolean = if (left!=None) true else false
    def hasRight:Boolean = if (right!=None) true else false
    def hasParent:Boolean = if (parent!=None) true else false
    def isLeaf:Boolean = !hasLeft && !hasRight
    def isParent(n:Node[K]):Boolean = {
        if (!isLeaf) {
            val l = if (hasLeft) left.get else null
            val r = if (hasRight) right.get else null
            l==n || r==n
        }
        else false
    }
}

abstract class BinaryTree[K] {
    def add(value:K)
    def remove(value:K):Boolean
    def height:Int
    def size:Int
}

class Tree[K](implicit ord:K=>Ordered[K]) extends BinaryTree[K] {
    var root:Option[Node[K]] = None
    private var count = 0
    override def add(value:K) {
        root match {
            case None => root = Some(new Node[K](value,None,None,None))
            case Some(node) => if(insert(node,value)) count+=1
        }
    }

    def insert(node:Node[K],newVal:K):Boolean= {
        if(newVal<node.value) {
            node match{
                case Node(_,None,_,_) => node.left = 
                    Some(new Node[K](newVal,None,None,Some(node))); true
                case Node(_,Some(left),_,_) => insert(left,newVal)
            }
        } else if(newVal>node.value) {
            node match{
                case Node(_,_,None,_) => node.right = 
                    Some(new Node[K](newVal,None,None,Some(node))); true
                case Node(_,_,Some(right),_) => insert(right,newVal)
            }
        } else false
    }

    override def remove(value:K):Boolean= {
        root match {
            case None => false
            case Some(node) => {
                binarySearch(value,node) match {
                    case None => false
                    case Some(node) => {
                        count-=1
                        delete(node)
                        true
                    }
                }
            }
        }
    }

    def delete(node:Node[K]) {
        node match {
            case Node(value,None,None,Some(parent)) => updateParent(parent,value,None)
            case Node(value,Some(child),None,Some(parent)) => {
                updateParent(parent,value,Some(child))
                child.parent = Some(parent)
            }
            case Node(value,None,Some(child),Some(parent)) => {
                updateParent(parent,value,Some(child))
                child.parent = Some(parent)
            }
            case Node(_,Some(child),None,None) => {
                root = Some(child)
                child.parent = None
            }
            case Node(_,None,Some(child),None) => {
                root = Some(child)
                child.parent = None
            }
            case Node(_,Some(left),Some(right),_) => {
                var child = right
                while(child.left!=None) {
                    child=child.left.get
                } 
                node.parent match {
                    case Some(parent) => updateParent(parent,node.value,Some(child))
                    case None => root = Some(child)
                }
                child.left = node.left
                child.right = node.right
                left.parent = Some(child)
                right.parent = Some(child)
                if (child.hasParent && child.parent.get.hasLeft && child.parent.get.left.get == child) 
                    child.parent.get.left=None
                else child.parent.get.right=None
                    child.parent = node.parent
                }
            case _ =>
        }

        def updateParent(parent:Node[K],value:K,newChild:Option[Node[K]]) {
            if(value<parent.value) parent.left = newChild
            else parent.right = newChild
        }
    }

    def binarySearch(value:K,node:Node[K]):Option[Node[K]]= {
        if (value==node.value) 
            Some(node)
        else if (value<=node.value) {
            node match {
                case Node(_,None,_,_) => None
                case Node(_,Some(left),_,_) => binarySearch(value,left)
            }
        } else {
            node match{
                case Node(_,_,None,_) => None
                case Node(_,_,Some(right),_) => binarySearch(value,right)
            }
        }
    }

    def inorder:Seq[K]= {
        val nodes = new ArrayBuffer[K]()
        val stack = new Stack[Node[K]]()
        if (size!=0) {
            var cur = root
            while(!stack.isEmpty || cur!=None) {
                cur match {
                    case Some(node) => {
                        stack.push(node)
                        cur = node.left
                    }
                    case None=> {
                        val tmp = stack.pop()
                        nodes += tmp.value
                        cur = tmp.right
                    }
                }
            }
        }
        nodes
    }

    def preorder:Seq[K]= {
        val nodes = new ArrayBuffer[K]()
        val stack = new Stack[Node[K]]()
        if (size!=0) {
            var cur = root
            while(!stack.isEmpty || cur!=None) {
                cur match {
                    case Some(node) => {
                        stack.push(node)
                        nodes += node.value
                       cur = node.left
                    }
                    case None=> {
                        val tmp = stack.pop()
                        cur = tmp.right
                    }
                }
            }
        }
        nodes
    }

    def postorder:Seq[K]= {
        val nodes = new ArrayBuffer[K]()
        val stack = new Stack[Node[K]]()
        if (size!=0) {
            var prev:Option[Node[K]] = None
            stack.push(root.get)
            while(!stack.isEmpty) {
                val cur = stack.top
                prev match {
                    case None=> if (cur.hasLeft) stack.push(cur.left.get) else if (cur.hasRight) stack.push(cur.right.get)
                    case Some(node)=>{
                        if(!cur.isParent(node) && cur.hasLeft) stack.push(cur.left.get)
                        else if (!cur.isParent(node) && cur.hasRight) stack.push(cur.right.get)
                        else if (cur.isParent(node) && cur.hasRight && cur.hasLeft && cur.left.get==node) stack.push(cur.right.get)
                        else {
                            stack.pop()
                             nodes+=cur.value
                        }
                    }
                }
                prev=Some(cur)
           }
        } 
        nodes
    }

    def postorder(node:Option[Node[K]]) {
        node match{
            case None=>
            case Some(n)=>{
                postorder(n.left)
                postorder(n.right)
                println(n.value)
            }
        } 
    }

    override def toString:String= {
        postorder.mkString(" : ")
    }

    override def height:Int= depth(root)

    def depth(node:Option[Node[K]]):Int = {
        node match {
            case None => 0
            case Some(n) => 1+ scala.math.max(depth(n.left),depth(n.right))
        } 
    }

    def prettyPrint(node:Option[Node[K]]):String= {
        if (node == None) ""
        else if(node.get.isLeaf) "\n\\t"+node.get.value.toString
        else node.get.value.toString+"\n\\t"+prettyPrint(node.get.left)+"\n\\t"+prettyPrint(node.get.right)
    }

    def bfs {
        val queue = new Queue[Option[Node[K]]]()
        queue.enqueue(root)
        while(!queue.isEmpty) {
            queue.dequeue match {
                case Some(node)=>{
                    println(node.value)
                    queue.enqueue(node.left)
                    queue.enqueue(node.right)
                }
                case None =>
            }
        }
    }   
    def size = count
}

object App {
    def main(args:Array[String]) {
        val generator = new Random()
        val tree = new Tree[Int]()
    //    (1 until 8).foreach(_=>tree.add(generator.nextInt(100)))
        tree.add(6)
        tree.add(3)
        tree.add(8)
        tree.add(2)
        tree.add(4)
        tree.add(8)
        tree.add(7)
        tree.add(9)
        tree.add(1)
        tree.add(5)
        tree.postorder(tree.root)
        println(tree)
        tree.bfs     
    }
}
import util.Random
导入collection.mutable.ArrayBuffer
导入scala.collection.mutable.Queue
导入scala.collection.mutable.Stack
案例类节点[K](值:K,变量左:选项[Node[K]],
var-right:Option[Node[K]],var-parent:Option[Node[K]]{
def hasleet:Boolean=if(left!=None)true-else-false
def hasRight:Boolean=if(right!=None)true-else-false
def hasParent:Boolean=if(parent!=None)true否则false
def isLeaf:Boolean=!hasleet&&!haslright
def isParent(n:Node[K]):布尔={
如果(!isLeaf){
val l=if(hasleet)left.get else null
val r=if(hasRight)right.get else null
l==n | | r==n
}
否则错误
}
}
抽象类二进制树[K]{
def添加(值:K)
def删除(值:K):布尔值
定义高度:Int
定义大小:Int
}
类树[K](隐式ord:K=>Ordered[K])扩展了二进制树[K]{
变量根:选项[Node[K]]=None
私有变量计数=0
覆盖def添加(值:K){
根匹配{
case None=>root=Some(新节点[K](值,无,无,无))
案例部分(节点)=>如果(插入(节点,值))计数+=1
}
}
def insert(节点:节点[K],newVal:K):布尔={
如果(newVal node.left=
Some(新节点[K](newVal,None,None,Some(节点));true
案例节点(_,Some(左),_,_)=>insert(左,newVal)
}
}else if(newVal>node.value){
节点匹配{
案例节点(u,u,无,)=>Node.right=
Some(新节点[K](newVal,None,None,Some(节点));true
案例节点(u,u,Some(右),)=>insert(右,newVal)
}
}否则错误
}
覆盖def删除(值:K):布尔={
根匹配{
case None=>false
案例部分(节点)=>{
二进制搜索(值、节点)匹配{
case None=>false
案例部分(节点)=>{
计数-=1
删除(节点)
真的
}
}
}
}
}
def delete(节点:节点[K]){
节点匹配{
案例节点(值,无,无,某些(父级))=>updateParent(父级,值,无)
案例节点(值、部分(子)、无、部分(父))=>{
updateParent(父级、值、部分(子级))
child.parent=一些(父)
}
案例节点(值、无、部分(子)、部分(父))=>{
updateParent(父级、值、部分(子级))
child.parent=一些(父)
}
案例节点(389;,一些(子),无,无)=>{
根=一些(子)
child.parent=无
}
案例节点(无,部分(子),无)=>{
根=一些(子)
child.parent=无
}
案例节点(33;,一些(左),一些(右),33;)=>{
var child=右
while(child.left!=无){
child=child.left.get
} 
node.parent匹配{
case Some(parent)=>updateParent(parent,node.value,Some(child))
case None=>root=Some(子项)
}
child.left=node.left
child.right=node.right
left.parent=一些(子)
right.parent=一些(子)
if(child.hasParent&&child.parent.get.hasleet&&child.parent.get.left.get==child)
child.parent.get.left=无
else child.parent.get.right=None
child.parent=node.parent
}
案例=>
}
def updateParent(父级:节点[K],值:K,新子级:选项[Node[K]]){
if(值binarySearch(值,左)
}
}否则{
节点匹配{
案例节点(u,u,无,)=>无
case节点(u,u,Some(右),)=>binarySearch(value,右)
}
}
}
def顺序:顺序[K]={
val nodes=new ArrayBuffer[K]()
val stack=新堆栈[节点[K]]()
如果(大小!=0){
var cur=根
而(!stack.isEmpty | | cur!=无){
电流匹配{
案例部分(节点)=>{
stack.push(节点)
cur=node.left
}
案例无=>{
val tmp=stack.pop()
节点+=tmp.value
cur=tmp.right
}
}
}
}
节点
}
def预订单:序号[K]={
val nodes=new ArrayBuffer[K]()
val stack=新堆栈[节点[K]]()
如果(大小!=0){
var cur=根
而(!stack.isEmpty | | cur!=无){
电流匹配{
案例部分(节点)=>{
stack.push(节点)
nodes+=node.value
cur=node.left
}
案例无=>{
val tmp=stack.pop()
cur=tmp.right
}
}
}
}
节点
}
def后订单:序号[K]={
val nodes=new ArrayBuffer[K]()
val stack=新堆栈[节点[K]]()
如果(大小!=0){
var prev:选项[节点[K]]=无
stack.push(root.get)
而(!stack.isEmpty){
val cur=stack.top
上一场比赛{
case None=>if(cur.hasleet)stack.push(cur.left.get)else if(cur.haslright)stack.push(cur.right.get)
案例部分(节点)=>{
如果(!cur.isParent(node)&&cur.hasleet)stack.push(
tree.root match{
    case None => tree.root = Some(new Node[Int](10,None,None,None))
    case Some(node) => tree.insert(node,10)
}