ZBLOG

go语言,一个数据库中的数据结构是这样的:type NginxResourceGroup struct { ID int Pid int Name string Remarks string State int },将其组织为树形结构,其根节点的id固定为1,预先设定好, 并不需要实际建立,剩余节点都在此基础上插入,每次插入要做校验,父节点id是否存在或者为1(父节点就是根节点),插入后是否会破坏树形结构,删除时要删除本身节点及其子节点,如果删除的节点...

可以使用以下示例代码将数据库中的数据组织为树形结构,并进行插入、删除和校验操作:

package main

import (
	"fmt"
)

type NginxResourceGroup struct {
	ID      int
	Pid     int
	Name    string
	Remarks string
	State   int
}

func main() {
	upGroups := []NginxResourceGroup{
		{ID: 1, Pid: 0, Name: "Root Node", Remarks: "", State: 1},
		{ID: 2, Pid: 1, Name: "Node A", Remarks: "", State: 1},
		{ID: 3, Pid: 2, Name: "Node B", Remarks: "", State: 1},
	}

	nodes := make(map[int]*NginxResourceGroup)

	for _, node := range upGroups {
		nodes[node.ID] = &node

		if !isValidTree(nodes) {
			fmt.Printf("Invalid tree structure after inserting node %d\n", node.ID)
			delete(nodes, node.ID)
		}
	}

	rootNode := nodes[1]

	if rootNode == nil {
        fmt.Println("No root node found.")
        return
    }

	fmt.Println("Initial tree structure:")
	printTree(rootNode, 0, nodes)

	insertNode(4, "New Node", rootNode.ID, nodes)
	insertNode(5, "New Child Node", 4, nodes) // Invalid insertion

	fmt.Println("\nAfter inserting new nodes:")
	printTree(rootNode, 0, nodes)

	deleteNode(4, rootNode.ID, nodes)

	fmt.Println("\nAfter deleting a node:")
	printTree(rootNode, 0, nodes)
}

func isValidTree(nodes map[int]*NginxResourceGroup) bool {
	for _, node := range nodes {
		parent := nodes[node.Pid]
		if parent == nil && node.Pid != 0 {
			return false
		}
	}

	return true
}

func insertNode(id int, name string, parentId int, nodes map[int]*NginxResourceGroup) {
	parent := nodes[parentId]

	if parent == nil {
		fmt.Printf("Parent node with ID %d does not exist.\n", parentId)
		return
	}

	newNode := &NginxResourceGroup{
		ID:      id,
		Pid:     parentId,
		Name:    name,
		Remarks: "",
		State:   1,
	}

	nodes[id] = newNode

	if !isValidTree(nodes) {
        fmt.Printf("Invalid tree structure after inserting node %d\n", id)
        delete(nodes, id)
    }
}

func deleteNode(id int, rootNodeId int, nodes map[int]*NginxResourceGroup) {
	nodeToDelete := nodes[id]

	if nodeToDelete == nil {
        fmt.Printf("Node with ID %d does not exist.\n", id)
        return
    }

	delete(nodes, id)

	for _, node := range nodes {
	    if node.Pid == id { // Delete child nodes recursively
	        deleteNode(node.ID, rootNodeId, nodes)
	    }
	    
	    if node.ID == rootNodeId { // If root is deleted, delete all other inserted nodes as well
	        delete(nodes, node.ID)
	    }
    }
}

func printTree(node *NginxResourceGroup, level int, nodes map[int]*NginxResourceGroup) {
	fmt.Printf("%s%s (ID: %d)\n", getIndent(level), node.Name, node.ID)

	for _, child := range nodes {
	    if child.Pid == node.ID { 
	        printTree(child, level+1, nodes)
	    }
	}
}

func getIndent(level int) string {
	indent := ""
	for i := 0; i < level; i++ {
		indent += "   "
	}
	return indent
}

请注意,示例代码中使用了一个nodes映射来存储所有节点,并根据节点的ID进行索引。插入节点时,首先检查父节点是否存在,并且保持树形结构的完整性。删除节点时,将递归地删除其子节点,并在需要时删除根节点以清空整棵树。

此示例代码仅用于演示目的,请根据您的实际需求和数据库操作进行相应修改。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=14040

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?