From b65db3b4f2bf6201c8eec03e5551f881b1c2818b Mon Sep 17 00:00:00 2001 From: Alec Goncharow Date: Fri, 10 Jul 2026 12:32:02 -0400 Subject: words --- radial/radial.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 radial/radial.go (limited to 'radial') diff --git a/radial/radial.go b/radial/radial.go new file mode 100644 index 0000000..e318252 --- /dev/null +++ b/radial/radial.go @@ -0,0 +1,107 @@ +package main + +import "fmt" + +type Node struct { + parent *Node + left *Node + right *Node + + val int + isLeft bool +} + +func (n *Node) pushLeft(val int) { + n.left = &Node{ + parent: n, + val: val, + isLeft: true, + } +} + +func (n *Node) pushRight(val int) { + n.right = &Node{ + parent: n, + val: val, + isLeft: false, + } +} + +func (n *Node) maxDepth(depth int) int { + if n == nil { + return depth + } + + return max(n.left.maxDepth(depth+1), n.right.maxDepth(depth+1)) +} + +func (n *Node) find(target int) *Node { + if n == nil { + return nil + } + if n.val == target { + return n + } + + l := n.left.find(target) + if l != nil { + return l + } + return n.right.find(target) +} + +func (n *Node) radial(nodes *[]*Node, distance int, fromTarget bool, fromChild bool, fromLeft bool) { + if n == nil { + return + } + + if distance == 0 { + *nodes = append(*nodes, n) + return + } + + if fromChild { + if fromLeft { + n.right.radial(nodes, distance-1, false, false, false) + } else { + n.left.radial(nodes, distance-1, false, false, false) + } + n.parent.radial(nodes, distance-1, false, true, n.isLeft) + } else { + n.left.radial(nodes, distance-1, false, false, false) + n.right.radial(nodes, distance-1, false, false, false) + } + + if fromTarget { + n.parent.radial(nodes, distance-1, false, true, n.isLeft) + } +} + +func main() { + root := Node{ + val: 1, + } + root.pushRight(2) + root.right.pushLeft(3) + root.right.pushRight(4) + + root.pushLeft(5) + root.left.pushLeft(6) + root.left.pushRight(7) + + root.left.left.pushLeft(8) + root.left.left.pushRight(9) + + root.left.right.pushLeft(10) + root.left.right.pushRight(11) + + find := root.find(5) + fmt.Println(find) + nodes := []*Node{} + find.radial(&nodes, 2, true, false, false) + fmt.Println(nodes) + for _, node := range nodes { + fmt.Println(node.val) + } + +} -- cgit v1.2.3-70-g09d2