Imported neighbor-swap function

This commit is contained in:
Andrew Golovashevich 2025-12-23 18:15:31 +03:00
parent db38b824d8
commit 6855724041
6 changed files with 75 additions and 2 deletions

View File

@ -1,6 +1,7 @@
[package] [package]
name = "binary-tree-utilities" name = "binary-tree-utilities-0"
version = "0.0.0" version = "0.0.0"
edition = "2024" edition = "2024"
[dependencies] [lints]
rust = { nonstandard_style = "allow" }

11
src/context.rs Normal file
View File

@ -0,0 +1,11 @@
pub unsafe trait BinTreeContext {
type NodeRef: Sized;
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getLeftChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getRightChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn setParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>);
fn setLeftChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
fn setRightChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
}

View File

@ -0,0 +1,2 @@
mod context;
mod swap;

1
src/swap/mod.rs Normal file
View File

@ -0,0 +1 @@
mod neighbors;

View File

@ -0,0 +1,25 @@
pub trait OrientedNeighborSwapContext {
type NodeRef: Sized + Copy;
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn getOppositeChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef>;
fn setParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>);
fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>);
fn setGrandparentToParent(&mut self, newParent: Self::NodeRef);
}
pub trait NeighbourSwapSupport {
fn leftChild_unknownParent(&self) -> impl OrientedNeighborSwapContext;
fn leftChild_root(&self) -> impl OrientedNeighborSwapContext;
fn leftChild_grandparentToLeft(&self) -> impl OrientedNeighborSwapContext;
fn leftChild_grandparentToRight(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_unknownParent(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_root(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_grandparentToLeft(&self) -> impl OrientedNeighborSwapContext;
fn rightChild_grandparentToRight(&self) -> impl OrientedNeighborSwapContext;
}

33
src/swap/neighbors/mod.rs Normal file
View File

@ -0,0 +1,33 @@
use crate::swap::neighbors::context::OrientedNeighborSwapContext;
mod context;
mod context_impl;
fn swapNeighbors<Context: OrientedNeighborSwapContext>(
ctx: &mut Context,
grandparent: Option<Context::NodeRef>,
parent: Context::NodeRef,
child: Context::NodeRef,
) {
ctx.setParent(child, grandparent);
ctx.setGrandparentToParent(child);
ctx.setForwardChild(parent, ctx.getForwardChild(child));
let oppositeChild = ctx.getOppositeChild(parent);
ctx.setOppositeChild(parent, ctx.getOppositeChild(child));
ctx.setOppositeChild(child, oppositeChild);
if let Some(n) = ctx.getOppositeChild(child) {
ctx.setParent(n, Some(child))
}
if let Some(n) = ctx.getOppositeChild(parent) {
ctx.setParent(n, Some(parent))
}
if let Some(n) = ctx.getForwardChild(child) {
ctx.setParent(n, Some(parent))
}
ctx.setForwardChild(child, Some(parent));
ctx.setParent(parent, Some(child));
}