mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-12 16:52:29 +08:00
feat: add DeletetNode
This commit is contained in:
@@ -21,7 +21,7 @@ func NewBSTree[T any](rootData T) *BSTree[T] {
|
|||||||
return &BSTree[T]{root}
|
return &BSTree[T]{root}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert data into BSTree
|
// InsertNode insert data into BSTree
|
||||||
func (t *BSTree[T]) InsertNode(data T, comparator lancetconstraints.Comparator) {
|
func (t *BSTree[T]) InsertNode(data T, comparator lancetconstraints.Comparator) {
|
||||||
root := t.root
|
root := t.root
|
||||||
newNode := datastructure.NewTreeNode(data)
|
newNode := datastructure.NewTreeNode(data)
|
||||||
@@ -32,6 +32,11 @@ func (t *BSTree[T]) InsertNode(data T, comparator lancetconstraints.Comparator)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeletetNode delete data into BSTree
|
||||||
|
func (t *BSTree[T]) DeletetNode(data T, comparator lancetconstraints.Comparator) {
|
||||||
|
deleteTreeNode(t.root, data, comparator)
|
||||||
|
}
|
||||||
|
|
||||||
// NodeLevel get node level in BSTree
|
// NodeLevel get node level in BSTree
|
||||||
func (t *BSTree[T]) NodeLevel(node *datastructure.TreeNode[T]) int {
|
func (t *BSTree[T]) NodeLevel(node *datastructure.TreeNode[T]) int {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
|
|||||||
@@ -79,3 +79,29 @@ func TestBSTree_InOrderTraverse(t *testing.T) {
|
|||||||
t.Log(acturl)
|
t.Log(acturl)
|
||||||
assert.Equal([]int{2, 4, 5, 6, 7}, acturl)
|
assert.Equal([]int{2, 4, 5, 6, 7}, acturl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBSTree_DeletetNode(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
|
||||||
|
|
||||||
|
bstree := NewBSTree(6)
|
||||||
|
|
||||||
|
comparator := &intComparator{}
|
||||||
|
bstree.InsertNode(7, comparator)
|
||||||
|
bstree.InsertNode(5, comparator)
|
||||||
|
bstree.InsertNode(2, comparator)
|
||||||
|
bstree.InsertNode(4, comparator)
|
||||||
|
|
||||||
|
bstree.Print()
|
||||||
|
|
||||||
|
// bstree.DeletetNode(4, comparator)
|
||||||
|
// bstree.Print()
|
||||||
|
// acturl1 := bstree.InOrderTraverse()
|
||||||
|
// t.Log(acturl1)
|
||||||
|
// assert.Equal([]int{2, 5, 6, 7}, acturl1)
|
||||||
|
|
||||||
|
bstree.DeletetNode(6, comparator)
|
||||||
|
bstree.Print()
|
||||||
|
acturl2 := bstree.InOrderTraverse()
|
||||||
|
t.Log(acturl2)
|
||||||
|
assert.Equal([]int{2, 5, 7}, acturl2)
|
||||||
|
}
|
||||||
|
|||||||
@@ -84,6 +84,38 @@ func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], compara
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteTreeNode[T any](node *datastructure.TreeNode[T], data T, comparator lancetconstraints.Comparator) *datastructure.TreeNode[T] {
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if comparator.Compare(data, node.Data) == -1 {
|
||||||
|
node.Left = deleteTreeNode(node.Left, data, comparator)
|
||||||
|
} else if comparator.Compare(data, node.Data) == 1 {
|
||||||
|
node.Right = deleteTreeNode(node.Right, data, comparator)
|
||||||
|
} else {
|
||||||
|
if node.Left == nil {
|
||||||
|
node = node.Right
|
||||||
|
} else if node.Right == nil {
|
||||||
|
node = node.Left
|
||||||
|
} else {
|
||||||
|
l := node.Right
|
||||||
|
d := inOrderSuccessor(l)
|
||||||
|
d.Left = node.Left
|
||||||
|
return node.Right
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
func inOrderSuccessor[T any](root *datastructure.TreeNode[T]) *datastructure.TreeNode[T] {
|
||||||
|
cur := root
|
||||||
|
for cur.Left != nil {
|
||||||
|
cur = cur.Left
|
||||||
|
}
|
||||||
|
return cur
|
||||||
|
}
|
||||||
|
|
||||||
func printTreeNodes[T any](nodes []*datastructure.TreeNode[T], level, maxLevel int) {
|
func printTreeNodes[T any](nodes []*datastructure.TreeNode[T], level, maxLevel int) {
|
||||||
if len(nodes) == 0 || isAllNil(nodes) {
|
if len(nodes) == 0 || isAllNil(nodes) {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user