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
140
141
142
143
144
145
146
147
use super::node::Node;
use super::node::*;
pub trait BaseTree<T> {
type MNode: Node<T>;
fn get(&self, val: usize) -> &Self::MNode;
fn get_mut(&self, val: usize) -> &mut Self::MNode;
fn delete_node(&mut self, index: usize);
fn create_node(&mut self, val: T) -> usize;
fn rebalance_ins(&mut self, n: usize);
fn rebalance_del(&mut self, n: usize, child: usize);
fn delete_replace(&mut self, n: usize) -> usize;
fn replace_node(&mut self, to_delete: usize, to_attach: Option<usize>);
fn attach_child(&self, p: usize, c: usize, side: Side);
fn get_root(&self) -> Option<usize>;
fn set_root(&mut self, new_root: Option<usize>);
fn get_size(&self) -> usize;
fn crement_size(&mut self, val: isize);
}
pub trait Tree<T: std::fmt::Debug>: BaseTree<T> {
fn new() -> Self;
fn is_empty(&self) -> bool {
return self.get_root().is_none()
}
fn contains(&self, val: &T) -> bool {
let n = self.find(val);
self.get(n).is(val)
}
fn insert(&mut self, val: T) {
if let Some(_root) = self.get_root() {
let n = self.find(&val);
let node = self.get(n);
if node.is(&val) {
self.crement_size(-1);
} else {
let side = if node.lesser(&val) {
Side::Right
} else {
Side::Left
};
let node = self.create_node(val);
self.attach_child(n, node, side);
self.rebalance_ins(node);
}
} else {
let r = Some(self.create_node(val));
self.set_root(r);
}
self.crement_size(1);
}
fn delete(&mut self, val: T) -> bool {
if !self.contains(&val) {
false
} else {
let n = self.find(&val);
let del = self.delete_replace(n);
self.rebalance_del(del, n);
self.delete_node(del);
self.crement_size(-1);
true
}
}
fn rotate(&mut self, side: Side, n: usize) {
let p = self.get(n).get_parent().expect("P rotate");
if let Some(c) = self.get(n).get_child(side) {
self.attach_child(p, c, !side);
} else {
match !side {
Side::Left => self.get_mut(p).set_child_opt(None, Side::Left),
Side::Right => self.get_mut(p).set_child_opt(None, Side::Right),
}
}
if let Some(g) = self.get(p).get_parent() {
self.get_mut(n).set_parent(Some(g));
let pside = if self.get(p).is_child(Side::Left) {
Side::Left
} else {
Side::Right
};
self.attach_child(g, n, pside);
} else {
self.set_root(Some(n));
self.get_mut(n).set_parent(None);
}
self.attach_child(n, p, side);
}
fn find(&self, val: &T) -> usize {
let mut n = self.get_root().expect("n find");
loop {
let node = self.get(n);
if node.lesser(val) && node.get_child(Side::Right).is_some() {
n = node.get_child(Side::Right).expect("find n right child");
} else if node.greater(val) && node.get_child(Side::Left).is_some() {
n = node.get_child(Side::Left).expect("find n left child");
} else {
return n;
}
}
}
fn get_height(&self) -> usize {
if let Some(root) = self.get_root() {
self.get(root).get_height()
} else {
0
}
}
fn get_leaf_count(&self) -> usize {
if let Some(root) = self.get_root() {
self.get(root).get_leaf_count()
} else {
0
}
}
fn to_string(&self) -> String {
if let Some(root) = self.get_root() {
self.get(root).to_string()
} else {
String::from("(Empty tree)")
}
}
fn to_pretty_string(&self) -> String {
if let Some(root) = self.get_root() {
self.get(root).to_pretty_string(1)
} else {
String::from("(Empty tree)")
}
}
}