//二叉排序树
//由数组转为二叉排序树
//三种遍历方式
//添加节点
//删除节点
//查找节点
//查找父节点
class TreeNode(Value:Int){
var value = Value
var left:TreeNode = null
var right:TreeNode = null
def addItem(node:TreeNode):Unit={
//添加元素
//重复节点挂右侧
if (node == null) return
if (node.value < value){
if (this.left == null) this.left = node
else{
this.left.addItem(node)
}
} else{
if (this.right == null) this.right = node
else this.right.addItem(node)
}
}
def preOrder():Unit = {
//前序遍历
if (this == null) {
return
}
println(this.value)
if (this.left != null)
this.left.preOrder()
if (this.right != null)
this.right.preOrder()
}
def inOrder():Unit = {
//中序遍历
if (this == null){
return
}
if (this.left != null)
this.left.inOrder()
println(this.value)
if (this.right != null)
this.right.inOrder()
}
def postOrder():Unit = {
//后序遍历
if (this == null){
return
}
if (this.left != null)
this.left.postOrder()
if (this.right != null)
this.right.postOrder()
println(this.value)
}
def searchPoint(Value:Int):TreeNode={
//查找节点
//从根节点开始找 this即为根节点
//类似二分查找
val value = Value
if (this == null) return null
if (this.value == value) return this
if (this.value > value){
if (this.left != null)
return this.left.searchPoint(value)
else return null
} else {
if (this.right != null)
return this.right.searchPoint(value)
else return null
}
}
def searchParent(Value:Int):TreeNode={
//查找父节点
val value = Value
if (this == null) return null
if (this.value == value) return null
if (this.value > value){
if (this.left != null){
if (this.left.value == value) return this
else this.left.searchParent(value)
} else return null
} else{
if (this.right != null){
if (this.right.value == value) return this
else this.right.searchParent(value)
} else return null
}
}
}
class BinarySortTree(){
var root :TreeNode = null
def addItem(node:TreeNode):Unit = {
if (root == null){
root = node
} else{
root.addItem(node)
}
}
def preOrder(){
//前序遍历
root.preOrder()
}
def inOrder(){
//中序遍历
root.inOrder()
}
def postOrder(){
//后序遍历
root.postOrder()
}
def searchPoint(Value:Int):TreeNode={
//寻找某个点
val value = Value
if (root != null){
return root.searchPoint(value)
} else return null
}
def searchParent(Value:Int):TreeNode = {
//寻找某个点的父节点
val value = Value
if (root != null){
return root.searchParent(value)
} else return null
}
def delRightTreeMin(node1:TreeNode,node2:TreeNode):Unit = {
//node1 要删除的节点 node2:要删除的节点的右子树
//删除右子树最小值,并将该值赋值给右子树的父节点
var target = node2
var flag = target
//如果右子树没有左子树,则说明该右子树节点为最小值,用该值来修改要删除的节点,即完成了删除,
//将该右子树的右节点挂在要删除的节点的右节点,即完成了整个删除步骤
if(target.left == null){
if (target.right != null){
node1.value = target.value
node1.right = target.right
return
}
}
//如果右子树右左节点,则循环查找其左节点,最后一个左节点即为最小值,仍如上述完成修改并将最小节点删除
while(target.left != null){
flag = target
target = target.left
}
var minValue = target.value
node1.value = minValue
flag.left = target.right
}
def delNode(Value:Int):Unit={
//
//删除节点
//节点可能有三种情况
//0.考虑是否是根节点、是否存在该节点
//1.节点为叶子节点:找到并删除即可
//2.节点含有一个子节点:找到该节点的父节点,并把该节点的子节点挂到该节点的父节点
//3.节点含有两个子节点:找到该节点的父节点,并在该节点的右子树中找到最小值,将该节点的值替换为最小值,并删除右子树的最小值
val value = Value
if (root == null) return
var theNode = searchPoint(value)
if (theNode == null){
println("未找到该节点")
return
}
if (theNode != null){
var theParent = searchParent(value)
if (theNode.left == null && theNode.right == null){
//要删节点为叶子节点
if (theParent == null){
//此时树只有一个根
root = null
return
}
if (theParent.left != null && theParent.left.value == value) theParent.left = null
else if (theParent.right != null && theParent.right.value == value) theParent.right = null
}else if(theNode.left != null && theNode.right != null){
//要删节点有两个左右子节点
delRightTreeMin(theNode,theNode.right)
} else{
//要删节点只有一个子节点
if (theParent == null){
//要删除的为根节点,并且根节点只有一侧有数据
if(theNode.left == null) {
root = theNode.right
return
} else {
root = theNode.left
return
}
}
//删除的节点是左节点
if (theParent.left != null && theParent.left.value == value){
if(theNode.left != null)
theParent.left = theNode.left
else theParent.left = theNode.right
//删除的节点是右节点
} else if (theNode.left != null)
//删除的节点左节点不为空,则将该节点的左子树挂在父节点的右子树
theParent.right = theNode.left
//删除的节点右节点不为空,则将该节点的右子树挂在父节点的右子树
else theParent.right = theNode.right
}
}else {}
}
}
var bst = new BinarySortTree()
val arr = Array(10,8,4,9,15,12,10,20,,17,20)
for (item <- arr){
bst.addItem(new TreeNode(item))
}