#![no_std]
extern crate alloc;
use alloc::vec::Vec;
pub mod bfs_order;
pub mod dfs_order;
#[inline]
pub fn compute_num_nodes(height: usize) -> usize {
(1 << height) - 1
}
#[derive(Copy, Clone, Debug)]
pub struct NotCompleteTreeSizeErr{
pub length:usize
}
#[must_use]
fn valid_node_num(num: usize) -> Result<(),NotCompleteTreeSizeErr> {
if (num + 1).is_power_of_two() && num != 0{
Ok(())
}else{
Err(NotCompleteTreeSizeErr{length:num})
}
}
#[inline]
pub fn compute_height(num_nodes: usize) -> usize {
(num_nodes + 1).trailing_zeros() as usize
}
#[derive(Clone)]
pub struct DfsInOrderIter<C: Visitor> {
a: Vec<(C::Item, Option<C>)>,
length: Option<usize>,
min_length: usize,
num: usize,
}
impl<C: Visitor> DfsInOrderIter<C> {
fn add_all_lefts(stack: &mut Vec<(C::Item, Option<C>)>, node: C) {
let mut target = Some(node);
loop {
let (i, next) = target.take().unwrap().next();
match next {
Some([left, right]) => {
let bleep = (i, Some(right));
stack.push(bleep);
target = Some(left);
}
None => {
let bleep = (i, None);
stack.push(bleep);
break;
}
}
}
}
}
impl<C: Visitor> Iterator for DfsInOrderIter<C> {
type Item = C::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.a.pop() {
Some((i, nl)) => match nl {
Some(nl) => {
let res = i;
DfsInOrderIter::add_all_lefts(&mut self.a, nl);
self.num += 1;
Some(res)
}
None => Some(i),
},
None => None,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(
self.min_length - self.num,
self.length.map(|a| a - self.num),
)
}
}
impl<C: Visitor> core::iter::FusedIterator for DfsInOrderIter<C> {}
impl<C: FixedDepthVisitor> core::iter::ExactSizeIterator for DfsInOrderIter<C> {}
#[derive(Clone)]
pub struct DfsPreOrderIter<C: Visitor> {
a: Vec<C>,
length: Option<usize>,
min_length: usize,
num: usize,
}
impl<C: Visitor> core::iter::FusedIterator for DfsPreOrderIter<C> {}
impl<C: FixedDepthVisitor> core::iter::ExactSizeIterator for DfsPreOrderIter<C> {}
impl<C: Visitor> Iterator for DfsPreOrderIter<C> {
type Item = C::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.a.pop() {
Some(x) => {
let (i, next) = x.next();
if let Some([left, right]) = next {
self.a.push(right);
self.a.push(left);
}
self.num += 1;
Some(i)
}
None => None,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(
self.min_length - self.num,
self.length.map(|a| a - self.num),
)
}
}
#[derive(Clone)]
pub struct Map<C, F> {
func: F,
inner: C,
}
impl<B, C: Visitor, F: Fn(C::Item) -> B + Clone> Visitor for Map<C, F> {
type Item = B;
#[inline]
fn next(self) -> (Self::Item, Option<[Self; 2]>) {
let (a, rest) = self.inner.next();
let k = (self.func)(a);
match rest {
Some([left, right]) => {
let ll = Map {
func: self.func.clone(),
inner: left,
};
let rr = Map {
func: self.func,
inner: right,
};
(k, Some([ll, rr]))
}
None => (k, None),
}
}
}
unsafe impl<B, C: FixedDepthVisitor, F: Fn(C::Item) -> B + Clone> FixedDepthVisitor for Map<C, F> {}
pub unsafe trait FixedDepthVisitor: Visitor {
fn get_height(&self)->usize{
self.level_remaining_hint().0
}
}
use core::iter::FusedIterator;
pub fn dfs_preorder_iter2<C:Visitor>(a:C)->impl Iterator<Item=C::Item>+FusedIterator{
let mut stack=Vec::new();
stack.push(a);
core::iter::from_fn(move ||{
if let Some(x)=stack.pop(){
let (i, next) = x.next();
if let Some([left, right]) = next {
stack.push(right);
stack.push(left);
}
Some(i)
}else{
None
}
}).fuse()
}
pub trait Visitor: Sized {
type Item;
fn next(self) -> (Self::Item, Option<[Self; 2]>);
#[inline(always)]
fn level_remaining_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
#[inline(always)]
fn with_depth(self, start_depth: Depth) -> LevelIter<Self> {
LevelIter {
inner: self,
depth: start_depth,
}
}
#[inline(always)]
fn zip<F: Visitor>(self, f: F) -> Zip<Self, F> {
Zip { a: self, b: f }
}
#[inline(always)]
fn map<B, F: Fn(Self::Item) -> B>(self, func: F) -> Map<Self, F> {
Map { func, inner: self }
}
#[inline(always)]
fn take(self, num: usize) -> Take<Self> {
Take { a: self, num }
}
#[inline(always)]
fn flip(self) -> Flip<Self> {
Flip(self)
}
#[inline]
fn dfs_preorder_iter(self) -> DfsPreOrderIter<Self> {
let (levels, max_levels) = self.level_remaining_hint();
let mut a = Vec::with_capacity(levels);
a.push(self);
let min_length = 2usize.pow(levels as u32) - 1;
let length = max_levels.map(|levels_max| 2usize.pow(levels_max as u32) - 1);
DfsPreOrderIter {
a,
length,
min_length,
num: 0,
}
}
#[inline]
fn dfs_inorder_iter(self) -> DfsInOrderIter<Self> {
let (levels, max_levels) = self.level_remaining_hint();
let mut a = Vec::with_capacity(levels);
let length = max_levels.map(|levels_max| 2usize.pow(levels_max as u32) - 1);
let min_length = 2usize.pow(levels as u32) - 1;
DfsInOrderIter::add_all_lefts(&mut a, self);
DfsInOrderIter {
a,
min_length,
length,
num: 0,
}
}
#[inline]
fn dfs_preorder(self, mut func: impl FnMut(Self::Item)) {
rec_pre(self, &mut func);
}
#[inline]
fn dfs_inorder(self, mut func: impl FnMut(Self::Item)) {
rec_inorder(self, &mut func);
}
#[inline]
fn dfs_postorder(self, mut func: impl FnMut(Self::Item)) {
rec_post(self, &mut func);
}
}
fn rec_pre<C: Visitor>(a: C, func: &mut impl FnMut(C::Item)) {
let (nn, rest) = a.next();
match rest {
Some([left, right]) => {
func(nn);
rec_pre(left, func);
rec_pre(right, func);
}
None => func(nn),
}
}
fn rec_inorder<C: Visitor>(a: C, func: &mut impl FnMut(C::Item)) {
let (nn, rest) = a.next();
match rest {
Some([left, right]) => {
rec_inorder(left, func);
func(nn);
rec_inorder(right, func);
}
None => {
func(nn);
}
}
}
fn rec_post<C: Visitor>(a: C, func: &mut impl FnMut(C::Item)) {
let (nn, rest) = a.next();
match rest {
Some([left, right]) => {
rec_post(left, func);
rec_post(right, func);
func(nn);
}
None => {
func(nn);
}
}
}
#[derive(Clone)]
pub struct Flip<T: Visitor>(T);
impl<T: Visitor> Visitor for Flip<T> {
type Item = T::Item;
fn next(self) -> (Self::Item, Option<[Self; 2]>) {
let (a, rest) = self.0.next();
(a, rest.map(|[l, r]| [Flip(r), Flip(l)]))
}
}
unsafe impl<T: FixedDepthVisitor> FixedDepthVisitor for Flip<T> {}
#[derive(Clone)]
pub struct Take<T: Visitor> {
a: T,
num: usize,
}
impl<T: Visitor> Visitor for Take<T> {
type Item = T::Item;
fn next(self) -> (Self::Item, Option<[Self; 2]>) {
let (a, rest) = self.a.next();
let rest = match rest {
Some([left, right]) => {
if self.num == 0 {
None
} else {
Some([
Take {
a: left,
num: self.num - 1,
},
Take {
a: right,
num: self.num - 1,
},
])
}
}
None => None,
};
(a, rest)
}
}
#[derive(Clone)]
pub struct Zip<T1: Visitor, T2: Visitor> {
a: T1,
b: T2,
}
impl<T1: Visitor, T2: Visitor> Zip<T1, T2> {
#[inline]
pub fn into_inner(self) -> (T1, T2) {
let Zip { a, b } = self;
(a, b)
}
#[inline]
pub fn as_inner(&self) -> (&T1, &T2) {
(&self.a, &self.b)
}
#[inline]
pub fn as_inner_mut(&mut self) -> (&mut T1, &mut T2) {
(&mut self.a, &mut self.b)
}
}
impl<T1: Visitor, T2: Visitor> Visitor for Zip<T1, T2> {
type Item = (T1::Item, T2::Item);
#[inline]
fn next(self) -> (Self::Item, Option<[Self; 2]>) {
let (a_item, a_rest) = self.a.next();
let (b_item, b_rest) = self.b.next();
let item = (a_item, b_item);
match (a_rest, b_rest) {
(Some(a_rest), Some(b_rest)) => {
let [aleft, aright] = a_rest;
let [bleft, bright] = b_rest;
let f1 = Zip { a: aleft, b: bleft };
let f2 = Zip {
a: aright,
b: bright,
};
(item, Some([f1, f2]))
}
_ => (item, None),
}
}
#[inline]
fn level_remaining_hint(&self) -> (usize, Option<usize>) {
let a = self.a.level_remaining_hint();
let b = self.b.level_remaining_hint();
let min = a.0.min(b.0);
let min2 = match (a.1, b.1) {
(Some(a), Some(b)) => Some(a.min(b)),
_ => None,
};
(min, min2)
}
}
unsafe impl<T1: FixedDepthVisitor, T2: FixedDepthVisitor> FixedDepthVisitor for Zip<T1, T2> {}
#[derive(Copy, Clone)]
pub struct Depth(pub usize);
#[derive(Clone)]
pub struct LevelIter<T> {
inner: T,
depth: Depth,
}
impl<T> LevelIter<T> {
#[inline]
pub fn depth(&self) -> usize {
self.depth.0
}
#[inline]
pub fn into_inner(self) -> T {
self.inner
}
#[inline]
pub fn as_inner(&self) -> &T {
&self.inner
}
#[inline]
pub fn as_inner_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T: Visitor> Visitor for LevelIter<T> {
type Item = (Depth, T::Item);
#[inline(always)]
fn next(self) -> (Self::Item, Option<[Self; 2]>) {
let LevelIter { inner, depth } = self;
let (nn, rest) = inner.next();
let r = (depth, nn);
match rest {
Some([left, right]) => {
let ln = Depth(depth.0 + 1);
let ll = LevelIter {
inner: left,
depth: ln,
};
let rr = LevelIter {
inner: right,
depth: ln,
};
(r, Some([ll, rr]))
}
None => (r, None),
}
}
#[inline]
fn level_remaining_hint(&self) -> (usize, Option<usize>) {
self.inner.level_remaining_hint()
}
}
unsafe impl<T: FixedDepthVisitor> FixedDepthVisitor for LevelIter<T> {}