From 68557240413fa780ca90de166840d55972c62009 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Tue, 23 Dec 2025 18:15:31 +0300 Subject: [PATCH] Imported neighbor-swap function --- Cargo.toml | 5 +++-- src/context.rs | 11 +++++++++++ src/lib.rs | 2 ++ src/swap/mod.rs | 1 + src/swap/neighbors/context.rs | 25 +++++++++++++++++++++++++ src/swap/neighbors/mod.rs | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/context.rs create mode 100644 src/swap/mod.rs create mode 100644 src/swap/neighbors/context.rs create mode 100644 src/swap/neighbors/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 3d1db59..e38a18e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] -name = "binary-tree-utilities" +name = "binary-tree-utilities-0" version = "0.0.0" edition = "2024" -[dependencies] +[lints] +rust = { nonstandard_style = "allow" } \ No newline at end of file diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..0e9c3c0 --- /dev/null +++ b/src/context.rs @@ -0,0 +1,11 @@ +pub unsafe trait BinTreeContext { + type NodeRef: Sized; + + fn getParent(&self, node: Self::NodeRef) -> Option; + fn getLeftChild(&self, node: Self::NodeRef) -> Option; + fn getRightChild(&self, node: Self::NodeRef) -> Option; + + fn setParent(&mut self, node: Self::NodeRef, newParent: Option); + fn setLeftChild(&mut self, node: Self::NodeRef, newChild: Option); + fn setRightChild(&mut self, node: Self::NodeRef, newChild: Option); +} diff --git a/src/lib.rs b/src/lib.rs index e69de29..7c3d45b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,2 @@ +mod context; +mod swap; \ No newline at end of file diff --git a/src/swap/mod.rs b/src/swap/mod.rs new file mode 100644 index 0000000..22cc63f --- /dev/null +++ b/src/swap/mod.rs @@ -0,0 +1 @@ +mod neighbors; \ No newline at end of file diff --git a/src/swap/neighbors/context.rs b/src/swap/neighbors/context.rs new file mode 100644 index 0000000..84dcefd --- /dev/null +++ b/src/swap/neighbors/context.rs @@ -0,0 +1,25 @@ +pub trait OrientedNeighborSwapContext { + type NodeRef: Sized + Copy; + + fn getParent(&self, node: Self::NodeRef) -> Option; + fn getForwardChild(&self, node: Self::NodeRef) -> Option; + fn getOppositeChild(&self, node: Self::NodeRef) -> Option; + + fn setParent(&mut self, node: Self::NodeRef, newParent: Option); + fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Option); + fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Option); + + 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; +} \ No newline at end of file diff --git a/src/swap/neighbors/mod.rs b/src/swap/neighbors/mod.rs new file mode 100644 index 0000000..d8267bc --- /dev/null +++ b/src/swap/neighbors/mod.rs @@ -0,0 +1,33 @@ +use crate::swap::neighbors::context::OrientedNeighborSwapContext; + +mod context; +mod context_impl; + + + +fn swapNeighbors( + ctx: &mut Context, + grandparent: Option, + 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)); +}