rt。
package com.fetching.algorithm
/** * 二叉搜尋樹,即每乙個節點,它的左孩子都不大於它,右孩子都不小於它
* author:臥龍居
*/class treenode
}object binarysearchtree else
}else}}
root}}
//遞迴先序遍歷二叉搜尋樹
def preorder(root:treenode): unit =
}//中序遍歷二叉搜尋樹,則相當於排序輸出
def inorder(root:treenode): unit =
}//取得以某個節點為根的子樹中的最小節點
def getminnode(root:treenode):treenode = else
}//取得以某個節點為根的子樹的最大節點
def getmaxnode(root:treenode):treenode = else
}//根據data返回樹節點
def search(root:treenode,data:integer):treenode=else if(root == null)elseelse}}
//乙個節點的後繼,即是搜尋樹中大於該節點的最小節點
def successor(root:treenode,curnodeval:treenode):treenode = else
curparent}}
//刪除節點
def deletetreenode(root:treenode,curnodedata:integer):treenode=else
root
}else if(curnode.lchild == null || curnode.rchild == null)else
}elseelse
}root
}elseelse
root
}elseelse
curnode.rchild.parent = successornode
successornode.rchild = curnode.rchild
curnode.lchild.parent = successornode
successornode.lchild = curnode.lchild
if(curnode == curnode.parent.lchild)else
root}}
}else
}def main(args:array[string]):unit =
println("create binarysearch tree complete")
//新增乙個節點
addtreenode(root,4)
println("**************pre order********************")
preorder(root)
println("**************pre order********************")
println("**************in order********************")
inorder(root)
println("**************in order********************")
println("min node:"+getminnode(root).data)
println("max node:"+getmaxnode(root).data)
println("cur node:"+search(root,4).data)
//刪除節點
root = deletetreenode(root,6)
println("**************pre order********************")
preorder(root)
println("**************pre order********************")
}}
二叉搜尋樹 二叉搜尋樹
題目 二叉搜尋樹 time limit 2000 1000 ms j a others memory limit 32768 32768 k j a others total submission s 6945 accepted submission s 3077 problem descripti...
二叉搜尋樹 修剪二叉搜尋樹
第一反應是重構,看來別人的解答發現,其實不用重構那麼複雜。treenode trimbst treenode root,int low,int high if root val high 下一層處理完左子樹的結果賦給root left,處理完右子樹的結果賦給root right。root left ...
二叉搜尋樹的實現
binarysearchtree.h inte ce for the binarysearchtree class.include binarytreenode.h include binarytree1.h if defined afx binarysearchtree h 1cd2ff9d 73...