BinaryTreesHeight::height_r

This commit is contained in:
AF 2023-06-16 13:23:01 +00:00
parent 53cd98aa86
commit d9c21f639d
6 changed files with 33 additions and 15 deletions

View File

@ -133,7 +133,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
pub trait BinaryTreesHeight<'a>: BinaryTrees<'a> {
fn height(&self, tree: &Self::Tree) -> u64;
fn height_r(&self, reference: &Self::Reference) -> u64;
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T>;
}

View File

@ -155,6 +155,10 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight<
self.0.height(&tree.bound)
}
fn height_r(&self, reference: &Self::Reference) -> u64 {
self.0.height_r(&reference.bound)
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
self.0.leaf_height_error(height)
}

View File

@ -29,12 +29,14 @@ impl<A> AvlN<A> {
struct AvlR<A> {
node: Rc<AvlN<A>>,
height: u64,
}
impl<A> Clone for AvlR<A> {
fn clone(&self) -> Self {
Self {
node: self.node.clone(),
height: self.height,
}
}
}
@ -45,15 +47,8 @@ impl<A: Display> Display for AvlR<A> {
}
}
#[cfg(test)]
impl<A> AvlR<A> {
fn balanced(&self) -> bool {
self.node.balanced()
}
}
struct AvlT<A> {
reference: Option<AvlR<A>>,
reference: Option<Rc<AvlN<A>>>,
height: u64,
}
@ -135,7 +130,10 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTrees<'a> for AvlTs<A> {
}
fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
tree.reference.clone()
Some(AvlR {
node: tree.reference.clone()?,
height: tree.height,
})
}
}
@ -145,7 +143,7 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesTreeOf<'a> for AvlTs<A> {
height: std::cmp::max(node.l.height, node.r.height)
.checked_add(1)
.unwrap(),
reference: Some(AvlR { node: node.into() }),
reference: Some(node.into()),
}
}
}
@ -183,6 +181,10 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesHeight<'a> for AvlTs<A> {
tree.height
}
fn height_r(&self, reference: &Self::Reference) -> u64 {
reference.height
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
panic!("leaf height error: {height}.")
}

View File

@ -4,12 +4,12 @@ use super::*;
pub struct AvlReference<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> {
node: Point<'a, Ctx, AvlNode<'a, Ctx, A>>,
parent_height: u64,
height: u64,
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> PartialEq for AvlReference<'a, Ctx, A> {
fn eq(&self, other: &Self) -> bool {
self.node == other.node && self.parent_height == other.parent_height
self.node == other.node && self.height == other.height
}
}
@ -67,7 +67,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlTree<'a, Ctx, A> {
Nullable::Null(_) => None,
Nullable::NotNull(ref point) => Some(AvlReference {
node: point.clone(),
parent_height: self.height,
height: self.height,
}),
}
}
@ -75,7 +75,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlTree<'a, Ctx, A> {
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlReference<'a, Ctx, A> {
pub fn resolve(&self) -> Resolution<'a, Ctx, AvlNode<'a, Ctx, A>> {
let parent_height = self.parent_height;
let parent_height = self.height;
Ctx::fmap(self.node.resolve(), move |resolved| {
let node = resolved?;
node.matches_height(parent_height)
@ -83,6 +83,10 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlReference<'a, Ctx, A> {
Ok(node)
})
}
pub fn height(&self) -> u64 {
self.height
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Clone for AvlTree<'a, Ctx, A> {

View File

@ -183,4 +183,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> BoundReference<'a, C
pub fn equal(rl: &Self, rr: &Self, comparator: &impl Comparator<A>) -> bool {
rl.reference == rr.reference && Bounds::equal(&rl.bounds, &rr.bounds, comparator)
}
pub fn height(&self) -> u64 {
self.reference.height()
}
}

View File

@ -108,6 +108,10 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator<A
tree.height()
}
fn height_r(&self, reference: &Self::Reference) -> u64 {
reference.height()
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
<FallibleMonad<'a, Ctx, _> as Fail<_>>::fail(ResolutionError::Parse(BoundError::Avl(
AvlError::LeafHeight(height),