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

點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
————————————————
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章