dsa_rust/hierarchies/
linked_bst.rs1use crate::hierarchies::traits::{BinaryTree, Tree};
11
12type Pos<T> = Box<Node<T>>;
14
15#[derive(Clone, PartialEq)]
17pub struct Node<T: std::cmp::PartialEq> {
18 parent: Option<Pos<T>>,
19 left: Option<Pos<T>>,
20 right: Option<Pos<T>>,
21 data: Option<T>,
22}
23
24pub struct BinTree<T: std::cmp::PartialEq> {
28 root: Box<Node<T>>, size: usize,
30}
31impl<T> Default for BinTree<T>
32where
33 T: std::cmp::PartialEq,
34{
35 fn default() -> Self {
36 Self::new()
37 }
38}
39impl<T> BinTree<T>
40where
41 T: std::cmp::PartialEq,
42{
43 pub fn new() -> BinTree<T> {
45 let node: Node<T> = Node {
46 parent: None,
47 left: None,
48 right: None,
49 data: None,
50 };
51 BinTree {
52 root: Box::from(node),
53 size: 0,
54 }
55 }
56
57 pub fn add_left(&mut self, mut ancestor: Node<T>, node: Pos<T>) {
64 ancestor.left = Some(node);
65 }
66
67 pub fn add_right(&mut self, mut ancestor: Node<T>, node: Pos<T>) {
69 ancestor.right = Some(node);
70 }
71
72 pub fn set(&mut self, _p: Node<T>, _node: T) -> Result<(), String> {
76 Ok(())
77 }
78
79 pub fn attach(&mut self, _left: Node<T>, _right: Node<T>) {}
83
84 pub fn remove(&mut self, p: Node<T>) {
88 let _ = p;
89 }
90
91 pub fn size(&self) -> usize {
93 self.size
94 }
95
96 pub fn is_empty(&self) -> bool {
98 self.size == 0
100 }
101
102 pub fn root(&self) -> Option<&Pos<T>> {
104 Some(&self.root)
105 }
106}
107impl<T> Tree<T> for BinTree<T>
109where
110 T: Clone + std::cmp::PartialEq,
111{
112 type Position = Pos<T>;
113
114 fn get<'a>(&'a self, node: &'a Self::Position) -> Option<&'a T> {
116 if node.data.is_some() {
117 node.data.as_ref()
118 } else {
119 None
120 }
121 }
122
123 fn parent(&self, node: Self::Position) -> Option<Self::Position> {
125 if node.parent.is_some() {
129 node.parent
130 } else {
131 None
132 }
133 }
134
135 fn num_children(&self, node: Self::Position) -> Option<usize> {
137 self.children(node).map(|n| n.len())
138 }
144
145 fn children(&self, node: Self::Position) -> Option<Vec<Self::Position>> {
147 let mut vec = Vec::with_capacity(2);
148 if let Some(l) = node.left {
149 vec.push(l)
150 };
151 if let Some(r) = node.right {
152 vec.push(r)
153 };
154 Some(vec)
155 }
156
157 fn is_leaf(&self, node: Self::Position) -> bool {
159 node.left.is_none() && node.right.is_none()
165 }
166
167 fn is_root(&self, node: Self::Position) -> bool {
169 node == self.root
170 }
171
172 fn depth(&self, node: Self::Position) -> Option<usize> {
174 let mut d = 1;
175 let mut cursor = node.clone();
176 while !self.is_root(cursor.clone()) {
177 cursor = self.parent(cursor.clone()).unwrap();
178 d += 1;
179 }
180 Some(d)
181 }
182
183 fn height(&self, node: Self::Position) -> Option<usize> {
185 let mut h = 0;
186 if let Some(c) = self.children(node) {
187 for p in c {
188 h = std::cmp::max(h, 1 + self.height(p).unwrap())
189 }
190 Some(h)
191 } else {
192 None
193 }
194 }
195}
196impl<T> BinaryTree<T> for BinTree<T>
197where
198 T: Clone + std::cmp::PartialEq,
199{
200 type Position = Pos<T>;
201
202 fn left(&self, node: Self::Position) -> Option<Self::Position> {
203 if node.left.is_some() {
204 node.left
205 } else {
206 None
207 }
208 }
209
210 fn right(&self, node: Self::Position) -> Option<Self::Position> {
211 if node.right.is_some() {
212 node.right
213 } else {
214 None
215 }
216 }
217
218 fn sibling(&self, node: Self::Position) -> Option<Self::Position> {
219 if let Some(ref parent) = node.parent {
228 if parent.left.as_ref() == Some(&node) {
231 parent.right.clone() } else {
233 parent.left.clone() }
235 } else {
236 None }
238 }
239}