public void Remove(TVal val) { using (this.SmartRWLocker.Lock(AccessMode.Write)) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> root = this.Get(val); if (root != null) { if (this.allNode.Count == 1) { this.root = null; } else { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node2 = this.allNode[this.allNode.Count - 1]; if (root.TheValue.CompareTo(node2.TheValue) != 0) { root.TheValue = node2.TheValue; } if ((this.allNode.Count % 2) == 0) { this.allNode[(this.allNode.Count - 2) / 2].LeftChild = null; } else { this.allNode[(this.allNode.Count - 3) / 2].RightChild = null; } this.allNode.RemoveAt(this.allNode.Count - 1); this.count--; this.SwapFromRootToLeaf(root); } } } }
public virtual void Insert(TVal val) { using (this.SmartRWLocker.Lock(AccessMode.Write)) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> item = new global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal>(val, null); if (this.root == null) { this.root = item; } else { int fatherIndx = this.GetFatherIndx(this.allNode.Count); if ((this.allNode.Count % 2) == 1) { this.allNode[fatherIndx].LeftChild = item; item.Parent = this.allNode[fatherIndx]; } else { this.allNode[fatherIndx].RightChild = item; item.Parent = this.allNode[fatherIndx]; } } this.count++; this.allNode.Add(item); } }
public void Insert(TVal val) { using (this.SmartRWLocker.Lock(AccessMode.Write)) { if (!this.Contains(val)) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node = new global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal>(val, null); if (this.root == null) { this.root = node; } else { int num = 0; if ((this.allNode.Count % 2) == 1) { num = (this.allNode.Count - 1) / 2; this.allNode[num].LeftChild = node; node.Parent = this.allNode[num]; } else { num = (this.allNode.Count - 2) / 2; this.allNode[num].RightChild = node; node.Parent = this.allNode[num]; } this.SwapFromLeafToRoot(node); } this.count++; this.allNode.Add(node); } } }
private void RemoveLastNode() { using (this.SmartRWLocker.Lock(AccessMode.Write)) { if (this.count != 0) { if (this.count == 1) { this.root = null; } else { int fatherIndx = this.GetFatherIndx(this.allNode.Count - 1); if ((this.allNode.Count % 2) == 1) { this.allNode[fatherIndx].RightChild = null; } else { this.allNode[fatherIndx].LeftChild = null; } } this.count--; this.allNode.RemoveAt(this.allNode.Count - 1); } } }
public global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> Get(TVal theValue) { if (this.root != null) { if (theValue.CompareTo(this.root.TheValue) == 0) { return(this.root); } global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> root = this.root; while (root != null) { int num = theValue.CompareTo(root.TheValue); if (num == 0) { return(root); } if (num > 0) { root = root.RightChild; } else { root = root.LeftChild; } } } return(null); }
private void SwapValueOfTwoNode(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node1, global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node2) { TVal theValue = node1.TheValue; node1.TheValue = node2.TheValue; node2.TheValue = theValue; }
private void DoGetAllNodes(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> childTreeRoot, bool ascend, ref IList <global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> > list, TraverseMode mode) { if (childTreeRoot != null) { if (mode == TraverseMode.MidOrder) { if (ascend) { this.DoGetAllNodes(childTreeRoot.LeftChild, ascend, ref list, mode); list.Add(childTreeRoot); this.DoGetAllNodes(childTreeRoot.RightChild, ascend, ref list, mode); } else { this.DoGetAllNodes(childTreeRoot.RightChild, ascend, ref list, mode); list.Add(childTreeRoot); this.DoGetAllNodes(childTreeRoot.LeftChild, ascend, ref list, mode); } } else if (mode == TraverseMode.PreOrder) { list.Add(childTreeRoot); this.DoGetAllNodes(childTreeRoot.LeftChild, ascend, ref list, mode); this.DoGetAllNodes(childTreeRoot.RightChild, ascend, ref list, mode); } else { this.DoGetAllNodes(childTreeRoot.LeftChild, ascend, ref list, mode); this.DoGetAllNodes(childTreeRoot.RightChild, ascend, ref list, mode); list.Add(childTreeRoot); } } }
private void Remove(TVal theValue, ref global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node) { if (node != null) { if (node.TheValue.CompareTo(theValue) == 0) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> parent; if ((node.LeftChild != null) && (node.RightChild != null)) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> minNode = this.GetMinNode(node.RightChild); if (minNode.Parent.TheValue.CompareTo(theValue) == 0) { parent = node.Parent; minNode.LeftChild = node.LeftChild; node = minNode; node.Parent = parent; } else { minNode.Parent.LeftChild = minNode.RightChild; if (minNode.RightChild != null) { minNode.RightChild.Parent = minNode.Parent; } node.TheValue = minNode.TheValue; } } else if (node.LeftChild != null) { parent = node.Parent; node = node.LeftChild; node.Parent = parent; } else if (node.RightChild != null) { parent = node.Parent; node = node.RightChild; node.Parent = parent; } else { node = null; } } else if (theValue.CompareTo(node.TheValue) < 0) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> leftChild = node.LeftChild; this.Remove(theValue, ref leftChild); node.LeftChild = leftChild; } else { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> rightChild = node.RightChild; this.Remove(theValue, ref rightChild); node.RightChild = rightChild; } } }
private void CountAllNodes(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> childTreeRoot, ref int count) { if (childTreeRoot != null) { count++; this.CountAllNodes(childTreeRoot.LeftChild, ref count); this.CountAllNodes(childTreeRoot.RightChild, ref count); } }
public global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> GetMinNode(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> childTree) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> leftChild = childTree; if (childTree.LeftChild != null) { leftChild = childTree.LeftChild; } return(leftChild); }
public global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> GetMaxNode(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> childTree) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> rightChild = childTree; if (childTree.RightChild != null) { rightChild = childTree.RightChild; } return(rightChild); }
public void SwapValueOfTwoNode(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node1, global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node2) { using (this.SmartRWLocker.Lock(AccessMode.Write)) { TVal theValue = node1.TheValue; node1.TheValue = node2.TheValue; node2.TheValue = theValue; } }
private int ComputeDepth(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> leaf) { int num = 1; for (global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node = leaf; node.Parent != null; node = node.Parent) { num++; } return(num); }
private bool Contain(TVal val, global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node) { if (node == null) { return(false); } if (node.TheValue.CompareTo(val) == 0) { return(true); } if (((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Max) && (node.TheValue.CompareTo(val) < 0)) || ((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Min) && (node.TheValue.CompareTo(val) > 0))) { return(false); } return(this.Contain(val, node.LeftChild) || this.Contain(val, node.RightChild)); }
private void SwapFromLeafToRoot(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> parent = node; if (this.heapType != global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Max) { while ((parent.Parent != null) && (parent.Parent.TheValue.CompareTo(parent.TheValue) > 0)) { this.SwapValueOfTwoNode(parent, parent.Parent); parent = parent.Parent; } } else { while ((parent.Parent != null) && (parent.Parent.TheValue.CompareTo(parent.TheValue) < 0)) { this.SwapValueOfTwoNode(parent, parent.Parent); parent = parent.Parent; } } }
public virtual void Insert(TVal theValue) { if (this.root == null) { this.root = new global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal>(theValue, null); } else { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> root = this.root; while (true) { int num = theValue.CompareTo(root.TheValue); if (num == 0) { return; } if (num > 0) { if (root.RightChild == null) { root.RightChild = new global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal>(theValue, root); return; } root = root.RightChild; } else { if (root.LeftChild == null) { root.LeftChild = new global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal>(theValue, root); return; } root = root.LeftChild; } } } }
private global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> GetTheLastNode(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> rootNode) { if (rootNode == null) { return(null); } global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> node = null; Queue <global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> > queue = new Queue <global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> >(); queue.Enqueue(rootNode); while (queue.Count > 0) { node = queue.Dequeue(); if (node.LeftChild != null) { queue.Enqueue(node.LeftChild); } if (node.RightChild != null) { queue.Enqueue(node.RightChild); } } return(node); }
private void SwapFromRootToLeaf(global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> root) { global::CJBasic.ObjectManagement.Trees.Binary.Node <TVal> leftChild = root; while ((leftChild.LeftChild != null) || (leftChild.RightChild != null)) { if (leftChild.RightChild == null) { if (((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Max) && (leftChild.TheValue.CompareTo(leftChild.LeftChild.TheValue) < 0)) || ((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Min) && (leftChild.TheValue.CompareTo(leftChild.LeftChild.TheValue) > 0))) { this.SwapValueOfTwoNode(leftChild, leftChild.LeftChild); leftChild = leftChild.LeftChild; continue; } break; } if (((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Max) && (leftChild.LeftChild.TheValue.CompareTo(leftChild.RightChild.TheValue) < 0)) || ((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Min) && (leftChild.LeftChild.TheValue.CompareTo(leftChild.RightChild.TheValue) > 0))) { if (((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Max) && (leftChild.TheValue.CompareTo(leftChild.RightChild.TheValue) < 0)) || ((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Min) && (leftChild.TheValue.CompareTo(leftChild.RightChild.TheValue) > 0))) { this.SwapValueOfTwoNode(leftChild, leftChild.RightChild); leftChild = leftChild.RightChild; continue; } break; } if (((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Max) && (leftChild.TheValue.CompareTo(leftChild.LeftChild.TheValue) < 0)) || ((this.heapType == global::CJBasic.ObjectManagement.Trees.Binary.HeapType.Min) && (leftChild.TheValue.CompareTo(leftChild.LeftChild.TheValue) > 0))) { this.SwapValueOfTwoNode(leftChild, leftChild.LeftChild); leftChild = leftChild.LeftChild; } else { break; } } }
public SorttedBinaryTree() { this.root = null; }