private T MinValue(LazinatorBinaryTreeNode <T> node) { T minv = node.Data; while (node.LeftNode != null) { minv = node.LeftNode.Data; node = node.LeftNode; } return(minv); }
private IEnumerable <T> TraversePostOrder(LazinatorBinaryTreeNode <T> parent) { if (parent != null) { foreach (var item in TraversePostOrder(parent.LeftNode)) { yield return(item); } foreach (var item in TraversePostOrder(parent.RightNode)) { yield return(item); } yield return(parent.Data); } }
private async Task LazinateRootAsync() { if (LazinatorMemoryStorage.Length == 0) { _Root = null; } else { LazinatorMemory childData = await GetChildSliceAsync(LazinatorMemoryStorage, _Root_ByteIndex, _Root_ByteLength, null); _Root = DeserializationFactory.Instance.CreateBaseOrDerivedType(257, (c, p) => new LazinatorBinaryTreeNode <T>(c, p), childData, this); await childData.ConsiderUnloadReadOnlyMemoryAsync(); } _Root_Accessed = true; }
private void LazinateRoot() { if (LazinatorMemoryStorage.Length == 0) { _Root = null; } else { LazinatorMemory childData = GetChildSlice(LazinatorMemoryStorage, _Root_ByteIndex, _Root_ByteLength, null); childData.LoadInitialReadOnlyMemory(); _Root = DeserializationFactory.Instance.CreateBaseOrDerivedType(257, (c, p) => new LazinatorBinaryTreeNode <T>(c, p), childData, this); childData.ConsiderUnloadInitialReadOnlyMemory(); } _Root_Accessed = true; }
// code adapted from http://csharpexamples.com/c-binary-search-tree-implementation/ public bool Add(T value) { LazinatorBinaryTreeNode <T> before = null, after = this.Root; while (after != null) { before = after; int comparison = value.CompareTo(after.Data); if (comparison < 0) //Is new node in left tree? { after = after.LeftNode; } else if (comparison > 0) //Is new node in right tree? { after = after.RightNode; } else { //Exist same value return(false); } } LazinatorBinaryTreeNode <T> newNode = new LazinatorBinaryTreeNode <T>(); newNode.Data = value; if (this.Root == null)//Tree ise empty { this.Root = newNode; } else { int comparison = value.CompareTo(before.Data); if (comparison < 0) { before.LeftNode = newNode; } else { before.RightNode = newNode; } } return(true); }
private LazinatorBinaryTreeNode <T> Find(T value, LazinatorBinaryTreeNode <T> parent) { if (parent != null) { int comparison = value.CompareTo(parent.Data); if (comparison == 0) { return(parent); } if (comparison < 0) { return(Find(value, parent.LeftNode)); } else { return(Find(value, parent.RightNode)); } } return(null); }
private LazinatorBinaryTreeNode <T> Remove(LazinatorBinaryTreeNode <T> parent, T key) { if (parent == null) { return(parent); } int comparison = key.CompareTo(parent.Data); if (comparison < 0) { parent.LeftNode = Remove(parent.LeftNode, key); } else if (comparison > 0) { parent.RightNode = Remove(parent.RightNode, key); } // if value is same as parent's value, then this is the node to be deleted else { // node with only one child or no child if (parent.LeftNode == null) { return(parent.RightNode); } else if (parent.RightNode == null) { return(parent.LeftNode); } // node with two children: Get the inorder successor (smallest in the right subtree) parent.Data = MinValue(parent.RightNode); // Delete the inorder successor parent.RightNode = Remove(parent.RightNode, parent.Data); } return(parent); }
public void SplittableEntitiesWork_SingleSplit() { var node = new LazinatorBinaryTreeNode <WByte>() { Data = 1 }; // node.LeftNode = new LazinatorBinaryTreeNode<WByte>() { Data = 0 }; node.RightNode = new LazinatorBinaryTreeNode <WByte>() { Data = 2 }; LazinatorMemory singleBufferResult = node.SerializeLazinator(new LazinatorSerializationOptions(IncludeChildrenMode.IncludeAllChildren, false, false, false)); LazinatorMemory multipleBufferResult = node.SerializeLazinator(new LazinatorSerializationOptions(IncludeChildrenMode.IncludeAllChildren, false, false, false, 1)); multipleBufferResult.MultipleMemoryChunks.Count().Should().BeGreaterThan(0); LazinatorMemory multipleConsolidated = new LazinatorMemory(multipleBufferResult.GetConsolidatedMemory()); string mString = multipleConsolidated.ToString(); string sString = singleBufferResult.ToString(); mString.Should().Be(sString); multipleConsolidated.Matches(singleBufferResult.InitialReadOnlyMemory.Span).Should().BeTrue(); }
private int GetTreeDepth(LazinatorBinaryTreeNode <T> parent) { return(parent == null ? 0 : Math.Max(GetTreeDepth(parent.LeftNode), GetTreeDepth(parent.RightNode)) + 1); }