algorithms_edu/algo/graph/
tree.rs1pub mod center;
4pub mod height;
5pub mod isomorphism;
6pub mod lca;
7pub mod rooting;
8pub mod sum;
9
10#[derive(Debug, Eq, PartialEq)]
13pub struct Node {
14 pub id: usize,
15 pub children: Vec<Node>,
16}
17
18impl Node {
19 pub fn new(id: usize) -> Self {
21 Self {
22 id,
23 children: vec![],
24 }
25 }
26}
27
28pub struct BinaryTreeNode {
29 pub id: usize,
30 pub left: Option<Box<BinaryTreeNode>>,
31 pub right: Option<Box<BinaryTreeNode>>,
32}
33
34impl BinaryTreeNode {
35 pub fn new(id: usize) -> Self {
36 Self {
37 id,
38 left: None,
39 right: None,
40 }
41 }
42}
43
44pub mod with_parent {
45 pub use std::cell::RefCell;
46 pub use std::rc::{Rc, Weak};
47 #[derive(Debug)]
50 pub struct Node {
51 pub id: usize,
52 pub parent: Option<Weak<RefCell<Node>>>,
55 pub children: Vec<Rc<RefCell<Node>>>,
56 }
57
58 impl std::cmp::PartialEq for Node {
61 fn eq(&self, other: &Node) -> bool {
62 self.id == other.id
63 && self.children == other.children
64 && match (self.parent.as_ref(), other.parent.as_ref()) {
65 (None, None) => true,
66 (Some(x), Some(y)) => {
67 x.upgrade().unwrap().borrow().id == y.upgrade().unwrap().borrow().id
68 }
69 _ => false,
70 }
71 }
72 }
73 impl std::cmp::Eq for Node {}
74
75 impl Node {
76 pub fn new(id: usize, parent: Option<&Rc<RefCell<Node>>>) -> Rc<RefCell<Node>> {
78 Rc::new(RefCell::new(Self {
79 id,
80 parent: parent.map(|parent| Rc::downgrade(&parent)),
81 children: vec![],
82 }))
83 }
84 pub fn add_child(parent: &Rc<RefCell<Node>>, child: &Rc<RefCell<Node>>) {
88 child.borrow_mut().parent = Some(Rc::downgrade(parent));
89 parent.borrow_mut().children.push(child.clone());
90 }
91 }
92
93 #[derive(Debug, Eq, PartialEq)]
94 #[allow(clippy::vec_box)]
95 pub struct UnsafeTreeNode {
96 pub id: usize,
97 pub parent: *const UnsafeTreeNode,
98 pub children: Vec<Box<UnsafeTreeNode>>,
100 }
101
102 impl UnsafeTreeNode {
103 pub fn new(id: usize, parent: *const UnsafeTreeNode) -> Self {
104 Self {
105 id,
106 parent,
107 children: vec![],
108 }
109 }
110 }
111}