-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_trees.go
More file actions
139 lines (114 loc) · 2.99 KB
/
Copy pathbinary_search_trees.go
File metadata and controls
139 lines (114 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import "fmt"
type BinarySearchTree struct {
Parent *BinarySearchTree
Left *BinarySearchTree
Right *BinarySearchTree
Content int // juste pour que ce soit facile à comparer...
}
func (t *BinarySearchTree) Search(x int) *BinarySearchTree {
if t == nil {
return nil
}
if t.Content == x {
return t
}
if x < t.Content {
return t.Left.Search(x) // même si t.Left est nul ça va appeler la fonction avec nil!
}
return t.Right.Search(x)
}
func (t *BinarySearchTree) Minimum() *BinarySearchTree {
for t.Left != nil {
t = t.Left
}
return t
}
func (t *BinarySearchTree) Maximum() *BinarySearchTree {
for t.Right != nil {
t = t.Right
}
return t
}
func (t *BinarySearchTree) Successor() *BinarySearchTree {
if t.Right != nil {
return t.Right.Minimum()
}
// on remonte jusqu'au root
// OU on remonte jusqu'au noeud telle que notre x était un enfant à gauche (donc plus petit) du parent
currentTree := t
currentParent := t.Parent
for currentParent != nil && currentTree == currentParent.Right {
currentTree = currentParent
currentParent = currentParent.Parent
}
return currentParent
}
func (t *BinarySearchTree) PrintInorder() {
if t.Left != nil {
t.Left.PrintInorder()
}
fmt.Println(t.Content)
if t.Right != nil {
t.Right.PrintInorder()
}
}
func (t *BinarySearchTree) Insert(content int) {
// STEP1 : on cherche l'endroit d'insertion
var insertionPosition *BinarySearchTree
currentTree := t
for currentTree != nil {
insertionPosition = currentTree
if content < currentTree.Content {
currentTree = currentTree.Left
} else {
currentTree = currentTree.Right
}
}
// STEP2 : insertion
newTree := &BinarySearchTree{
Parent: insertionPosition, // on ajoute le nouveau noeud à notre arbre
Content: content,
}
if newTree.Content < newTree.Parent.Content {
newTree.Parent.Left = newTree
} else {
newTree.Parent.Right = newTree
}
}
// Transplant remplace l'arbre U par l'arbre V
func (t *BinarySearchTree) Transplant(u, v *BinarySearchTree) {
if u.Parent == nil { // si u n'a pas de parent, c'était le root
*t = *v
} else if u == u.Parent.Left { // si u était l'enfant à gauche
u.Parent.Left = v // on met v à la place dans le parent
} else {
u.Parent.Right = v
}
if v != nil {
v.Parent = u.Parent // enfin, on set le parent de v à u
}
}
func (t *BinarySearchTree) Delete(z *BinarySearchTree) {
// Cas 1 : pas d'enfant à gauche
if z.Left == nil {
t.Transplant(z, z.Right)
return
}
// Cas 2 : pas d'enfant à droite
if z.Right == nil {
t.Transplant(z, z.Left)
return
}
// Case 3 : deux enfants, on cherche le successor
successor := z.Successor()
if successor.Parent != z {
t.Transplant(successor, successor.Right) // on fait monter l'enfant droit du successor
// par définition, le successor n'a pas d'enfant à gauche!! donc on ne s'en préoccupe pas
successor.Right = z.Right
successor.Right.Parent = successor
}
t.Transplant(z, successor)
successor.Left = z.Left
successor.Left.Parent = successor
}