MCPcopy Create free account
hub / github.com/arnauddri/algorithms / Insert

Method Insert

data-structures/binary-tree/bst.go:38–68  ·  view source on GitHub ↗
(i int)

Source from the content-addressed store, hash-verified

36}
37
38func (t *Tree) Insert(i int) {
39 n := &Node{Value: i}
40 if t.Head == nil {
41 t.Head = n
42 t.Size++
43 return
44 }
45
46 h := t.Head
47
48 for {
49 if n.Compare(h) == -1 {
50 if h.Left == nil {
51 h.Left = n
52 n.Parent = h
53 break
54 } else {
55 h = h.Left
56 }
57 } else {
58 if h.Right == nil {
59 h.Right = n
60 n.Parent = h
61 break
62 } else {
63 h = h.Right
64 }
65 }
66 }
67 t.Size++
68}
69
70func (t *Tree) Search(i int) *Node {
71 h := t.Head

Callers 2

TestTreeFunction · 0.95
DeleteMethod · 0.95

Calls 1

CompareMethod · 0.95

Tested by 1

TestTreeFunction · 0.76