scala版数据结构----二叉搜索树

点个关注吧,球球啦!
scala数据结构:
数组环形队列:https://blog.csdn.net/Mr_kidBK/article/details/105026438
二叉搜索树:   https://blog.csdn.net/Mr_kidBK/article/details/105501805

前言

       博主发现网上对于scala版本的数据结构代码非常少,甚至有些nc营销号挂着标题复制粘贴别人的python,java代码,也是给我看醉了。
       为了方便学弟学妹们学习(chaozuoye),我准备做一个scala版本数据结构系列,如果觉的有帮助的话,关注下博主呗
/**
 * @author daizihao
 * @create 2020-04-13 20:22
 **/
class BinarySearchTree[T: Ordering] {

  private val ord: Ordering[T] = implicitly[Ordering[T]]
  var root: TreeNode[T] = _
  var height: Int = 0

  def add(v: T) = {
    if (root == null) {
      root = new TreeNode[T](v)
      if (root != null) height += 1
    } else {
      val flag = root.add(v)
      if (flag) height += 1
    }
  }
/**
 *
 * 中序遍历
 */
  def infixForeach(op: T => Unit): Unit = {
    if (root != null) {
      if (root.left != null) root.left.infixForeach(op)
      op(root.value)
      if (root.right != null) root.right.infixForeach(op)
    }
  }

  def search(v: T): TreeNode[T] = {
    if (root == null) {
      null
    } else {
      root.search(v)
    }
  }


  def delete(v: T) = {
    val targetNode: TreeNode[T] = search(v)
    if (targetNode != null) {
      targetNode.delete()
    }
  }
}

case class TreeNode[T: Ordering](var value: T) {
  private val ord: Ordering[T] = implicitly[Ordering[T]]

  private[binarytree] var father: TreeNode[T] = _
  private[binarytree] var left: TreeNode[T] = _
  private[binarytree] var right: TreeNode[T] = _


  def rightMinNode: TreeNode[T] = {
    var temp: TreeNode[T] = this.right
    var min: TreeNode[T] = temp
    while (temp.left != null) {
      min = temp.left
      temp = temp.left
    }
    min
  }

  def leftMaxNode: TreeNode[T] = {
    var temp: TreeNode[T] = this.left
    var max: TreeNode[T] = temp
    while (temp.right != null) {
      max = temp.right
      temp = temp.right
    }
    max
  }

  def add(v: T): Boolean = {
    if (ord.lt(this.value, v)) {
      if (right == null) {
        right = TreeNode(v)
        right.father = this
        true
      } else {
        right.add(v)
      }
    } else if (ord.gt(this.value, v)) {
      if (left == null) {
        left = TreeNode(v)
        left.father = this
        true
      } else {
        left.add(v)
      }
    } else false
  }

  def infixForeach(op: T => Unit): Unit = {
    if (left != null) {
      left.infixForeach(op)
    }
    op(this.value)
    if (right != null) {
      right.infixForeach(op)
    }
  }

  def search(v: T): TreeNode[T] = {
    if (ord.equiv(value, v)) {
      this
    } else if (ord.gt(value, v)) {
      if (left != null) {
        left.search(v)
      } else {
        null
      }
    } else {
      if (right != null) {
        right.search(v)
      } else {
        null
      }
    }
  }

  /**
   * 计算树的高度
   */
  def height: Int = leftHeight.max(rightHeight) + 1

  /**
   * 左树高度
   *
   * @return
   */
  def leftHeight: Int = if (left == null) -1 else left.height

  /**
   * 右树高度
   *
   * @return
   */
  def rightHeight: Int = if (right == null) -1 else right.height

  def delete():Unit = {
    if (this.left == null && this.right == null) {
      //1.this是叶节点
      val father: TreeNode[T] = this.father
      //判断this是左叶子节点还是右
      if (father.left == this) {
        this.father = null
        father.left = null
      } else {
        this.father = null
        father.right = null
      }
    } else if (this.left != null && this.right != null) {
      //2.this不是叶结点,且左右都有
      //找到该节点右最小,或左最大
      if (this.rightHeight >= this.leftHeight) {
        val rightMinNode = this.rightMinNode
        this.value = rightMinNode.value
        rightMinNode.delete()
      } else {
        val leftMaxNode = this.leftMaxNode
        this.value = leftMaxNode.value
        leftMaxNode.delete()
      }
    } else if(this.left == null && this.right != null){
      //左没有右有
      this.right.father = this.father
      if(this.father.left == this){
        this.father.left = this.right
      } else {
        this.father.right = this.right
      }
      this.right = null
      this.father = null
    }else{
      //右没有左有
      this.left.father = this.father
      if(this.father.left == this){
        this.father.left = this.left
      } else {
        this.father.right = this.left
      }
      this.left = null
      this.father = null
    }

  }

}

点个赞再走,球球啦!

原创不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

本博客仅发布于CSDN—一个帅到不能再帅的人 Mr_kidBK。转载请标明出处。
https://blog.csdn.net/Mr_kidBK

点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
————————————————
在这里插入图片描述

发布了9 篇原创文章 · 获赞 36 · 访问量 4265

猜你喜欢

转载自blog.csdn.net/Mr_kidBK/article/details/105501805