数据结构之树复习笔记(数组树、链表树)

数组树逻辑 tree_array.go

package tree

import (
	"fmt"
	"math"
)

// TreeArray struct.
type TreeArray struct {
	tree []int
}

// CreateTree .
func CreateTree(s int, rootValue int) *TreeArray {
	treeList := TreeArray{
		tree: make([]int, s),
	}
	treeList.tree[0] = rootValue
	return &treeList
}

// Search func.
func (this *TreeArray) Search(idx int) *int {
	if idx < 0 {
		return nil
	}
	return &this.tree[idx]
}

// Insert func.
func (this *TreeArray) Insert(idx int, direction int, value int) bool {
	if idx < 0 || idx >= len(this.tree) {
		return false
	}

	if this.tree[idx] == 0 {
		return false
	}

	if direction == 0 { // 左
		if idx*2+1 >= len(this.tree) {
			return false
		}
		if this.tree[idx*2+1] != 0 {
			return false
		}
		this.tree[idx*2+1] = value
	}

	if direction == 1 { // 右
		if idx*2+2 >= len(this.tree) {
			return false
		}
		if this.tree[idx*2+2] != 0 {
			return false
		}
		this.tree[idx*2+2] = value
	}
	return true
}

// Delete func.
func (this *TreeArray) Delete(idx int, value int) (int, bool) {
	if idx < 0 || idx >= len(this.tree) {
		return 0, false
	}
	if this.tree[idx] == 0 {
		return 0, false
	}
	v := this.tree[idx]
	this.tree[idx] = 0
	return v, true
}

// Traverse func.
func (this *TreeArray) Traverse() {
	level := 0
	for index, v := range this.tree {
		if index == 0 {
			fmt.Printf("\t")
			fmt.Printf("%d", v)
			fmt.Printf("\n")
		} else {
			value := int(math.Log2(float64(index)))
			fmt.Printf("\t%d\t", v)
			if value > level {
				level = value
				fmt.Printf("\n")
			}
		}

	}
}

数组树测试 tree_array_test.go

package tree

import (
	"testing"
)

func TestTree(t *testing.T) {

	tree := CreateTree(10, 1)
	tree.Insert(0, 0, 2)
	tree.Insert(0, 1, 3)
	tree.Insert(1, 0, 4)
	tree.Insert(1, 1, 5)
	tree.Insert(2, 0, 6)
	tree.Insert(2, 1, 7)
	tree.Insert(3, 0, 8)
	tree.Insert(3, 1, 9)
	tree.Insert(4, 0, 10)
	tree.Insert(4, 1, 11)

	tree.Traverse()
}

链表树逻辑 tree_list.go

package tree

import "fmt"

// TreeNode struct.
type TreeNode struct {
	index      int
	data       int
	parent     *TreeNode
	leftChild  *TreeNode
	rightChild *TreeNode
}

// Search .
func (node *TreeNode) Search(nodeIndex int) *TreeNode {
	if node.index == nodeIndex {
		return node
	}

	if node.leftChild != nil {
		if node.leftChild.index == nodeIndex {
			return node.leftChild
		}
		tmp := node.leftChild.Search(nodeIndex)
		if tmp != nil {
			return tmp
		}
	}

	if node.rightChild != nil {
		if node.rightChild.index == nodeIndex {
			return node.rightChild
		}
		tmp := node.rightChild.Search(nodeIndex)
		if tmp != nil {
			return tmp
		}
	}

	return nil
}

// DeleteNode .
func (node *TreeNode) DeleteNode() {
	if node.leftChild != nil {
		node.leftChild.DeleteNode()
	}
	if node.rightChild != nil {
		node.rightChild.DeleteNode()
	}
	if node.parent != nil {
		if node.parent.leftChild == node {
			node.parent.leftChild = nil
		}
		if node.parent.rightChild == node {
			node.parent.rightChild = nil
		}
	}

	node = nil
}

// PreOrderTraversal struct.
func (node *TreeNode) PreOrderTraversal() {
	fmt.Println(node.index, "\t", node.data)

	if node.leftChild != nil {
		node.leftChild.PreOrderTraversal()
	}
	if node.rightChild != nil {
		node.rightChild.PreOrderTraversal()
	}
}

// InOrderTraversal impl.
func (node *TreeNode) InOrderTraversal() {

	if node.leftChild != nil {
		node.leftChild.InOrderTraversal()
	}

	fmt.Println(node.index, "\t", node.data)

	if node.rightChild != nil {
		node.rightChild.InOrderTraversal()
	}

}

// PostOrderTraversal impl.
func (node *TreeNode) PostOrderTraversal() {
	if node.leftChild != nil {
		node.leftChild.PostOrderTraversal()
	}
	if node.rightChild != nil {
		node.rightChild.PostOrderTraversal()
	}
	fmt.Println(node.index, "\t", node.data)
}

// Tree struct.
type Tree struct {
	RootNode *TreeNode
}

// SearchNode impl.
func (tree *Tree) SearchNode(nodeIndex int) *TreeNode {
	if tree.RootNode != nil {
		return tree.RootNode.Search(nodeIndex)
	}
	return nil
}

// AddNode impl
func (tree *Tree) AddNode(nodeIndex int, direction int, node *TreeNode) bool {
	temp := tree.SearchNode(nodeIndex)
	if temp == nil {
		return false
	}

	// new node
	newNode := new(TreeNode)
	newNode.index = node.index
	newNode.data = node.data
	newNode.parent = temp
	newNode.leftChild = nil
	newNode.rightChild = nil

	if direction == 0 {
		temp.leftChild = newNode
	} else {
		temp.rightChild = newNode
	}

	return true
}

// DeleteNode impl
func (tree *Tree) DeleteNode(nodeIndex int, node *TreeNode) bool {
	temp := tree.SearchNode(nodeIndex)
	if temp == nil {
		return false
	}

	if node != nil {
		node.data = temp.data
	}

	// delete
	temp.DeleteNode()
	return true
}

// PreOrderTraversal impl
func (tree *Tree) PreOrderTraversal() {
	tree.RootNode.PreOrderTraversal()
}

// InOrderTraversal impl
func (tree *Tree) InOrderTraversal() {
	tree.RootNode.InOrderTraversal()
}

// PostOrderTraversal impl
func (tree *Tree) PostOrderTraversal() {
	tree.RootNode.PostOrderTraversal()
}

链表树测试逻辑 tree_list_test.go


func TestTreeList(t *testing.T) {
	rootNode := &TreeNode{}
	tree := Tree{rootNode}

	node1 := TreeNode{}
	node1.index = 1
	node1.data = 5

	node2 := TreeNode{}
	node2.index = 2
	node2.data = 8

	node3 := TreeNode{}
	node3.index = 3
	node3.data = 2

	node4 := TreeNode{}
	node4.index = 4
	node4.data = 6

	node5 := TreeNode{}
	node5.index = 5
	node5.data = 9

	node6 := TreeNode{}
	node6.index = 6
	node6.data = 7

	tree.AddNode(0, 0, &node1)
	tree.AddNode(0, 1, &node2)

	tree.AddNode(1, 0, &node3)
	tree.AddNode(1, 1, &node4)

	tree.AddNode(2, 0, &node5)
	tree.AddNode(2, 1, &node6)

	//tree.DeleteNode(6, nil)

	tree.PreOrderTraversal()
	//tree.InOrderTraversal()

}

转载于:https://my.oschina.net/997155658/blog/3041714

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值