Skip to content

Commit 465dfbd

Browse files
committed
adding clean inserting
1 parent 910bc69 commit 465dfbd

File tree

4 files changed

+130
-105
lines changed

4 files changed

+130
-105
lines changed

CompleteTree.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
*
3+
* @Name: Complete Tree Implementation in Go By Linked List
4+
* @Author: Max Base
5+
* @Repository: https://github.com/BaseMax/CompleteTreeLinkedListGo
6+
* @License: GPL-3.0
7+
* @Date: 2022/12/24
8+
*
9+
**/
10+
11+
package main
12+
13+
type Node struct {
14+
value int
15+
left *Node
16+
right *Node
17+
}
18+
19+
type CompleteTree struct {
20+
root *Node
21+
}
22+
23+
/**
24+
* build a complete tree
25+
*/
26+
func NewCompleteTree() *CompleteTree {
27+
return &CompleteTree{}
28+
}
29+
30+
/**
31+
* insert a new node to complete tree
32+
*/
33+
func (h *CompleteTree) Insert(value int) {
34+
if h.root == nil {
35+
h.root = &Node{value: value}
36+
return
37+
}
38+
39+
h.insert(h.root, value)
40+
}
41+
42+
func (h *CompleteTree) insert(n *Node, value int) {
43+
if n.left == nil {
44+
n.left = &Node{value: value}
45+
return
46+
}
47+
48+
if n.right == nil {
49+
n.right = &Node{value: value}
50+
return
51+
}
52+
53+
h.insert(n.left, value)
54+
}
55+
56+
/**
57+
* insert a new node to complete tree by filling from left to right (per row)
58+
*/
59+
func (h *CompleteTree) InsertClean(value int) {
60+
if h.root == nil {
61+
h.root = &Node{value: value}
62+
return
63+
}
64+
65+
h.insertClean(h.root, value)
66+
}
67+
func (h *CompleteTree) insertClean(n *Node, value int) {
68+
if n.left == nil {
69+
n.left = &Node{value: value}
70+
return
71+
}
72+
73+
if n.right == nil {
74+
n.right = &Node{value: value}
75+
return
76+
}
77+
78+
h.insertClean(n.left, value)
79+
if n.left != nil {
80+
h.insertClean(n.right, value)
81+
}
82+
}
83+
84+
/**
85+
* print complete tree
86+
*/
87+
func (h *CompleteTree) Print() {
88+
// print the complete tree by using ASCII art
89+
h.print(h.root, 0)
90+
}
91+
func (h *CompleteTree) print(n *Node, level int) {
92+
if n == nil {
93+
return
94+
}
95+
96+
h.print(n.right, level+1)
97+
98+
for i := 0; i < level; i++ {
99+
print(" ")
100+
}
101+
102+
print(n.value)
103+
print(" ")
104+
print(" ")
105+
print("\n")
106+
107+
h.print(n.left, level+1)
108+
}
109+
110+
func main() {
111+
// Create a new complete tree
112+
CompleteTree := NewCompleteTree()
113+
114+
// Insert some values
115+
CompleteTree.Insert(1)
116+
CompleteTree.Insert(2)
117+
CompleteTree.Insert(3)
118+
CompleteTree.Insert(4)
119+
CompleteTree.Insert(5)
120+
CompleteTree.Insert(6)
121+
CompleteTree.Insert(7)
122+
CompleteTree.Insert(8)
123+
CompleteTree.Insert(9)
124+
CompleteTree.Insert(10)
125+
}

HeapTree.go

Lines changed: 0 additions & 100 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# Heap Tree Linked List (Go)
1+
# Complete Tree Linked List (Go)
22

3-
This is a simple Go program based on Linked List and by using it you can easily insert your nodes to the heap tree without worring about it to not be a heap tree. This program will automatically fix the heap tree for you and you are sure that your heap tree is always a heap tree.
43

5-
Heap Tree Go is a Go implementation of a heap tree. It is a binary tree where the parent node is always greater than or equal to its children. It is a complete binary tree, meaning that all levels of the tree are fully filled except for possibly the last level. The last level is filled from left to right.
64

75
## Using
86

9-
In the following we will create an empty Heap tree and add some values to it.
7+
In the following we will create an empty **Complete Tree** and add some values to it.
108

119
Finally we will print the heap tree.
1210

@@ -45,4 +43,6 @@ The output of above code will be:
4543
10
4644
```
4745

46+
47+
4848
Copyright (c) 2022, Max Base

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/BaseMax/HeapTreeGo
1+
module github.com/BaseMax/CompleteTree
22

33
go 1.19

0 commit comments

Comments
 (0)