private void DeleteInArray(AssociativeTreeNode <T> treeNode) { int indexForRemove = FindIndexOfNode(treeNode); if (indexForRemove != -1) { Count--; RemoveInArray(indexForRemove); DecrementIndexes(indexForRemove); } }
public void AddNode(AssociativeTreeNode <T> nodeForAdd) { if (Root == null) { Root = nodeForAdd; AssociativeTreeNodes[Count++] = Root; } else { AddNode(Root, nodeForAdd); } }
private void RemoveOperationThirdCase(AssociativeTreeNode <T> nodeForRemove) { var nodeForSwap = AssociativeTreeNodes[nodeForRemove.Right]; while (nodeForSwap.Left != -1) { nodeForSwap = AssociativeTreeNodes[nodeForSwap.Left]; } DeleteNode(nodeForSwap); nodeForRemove.Value = nodeForSwap.Value; }
private void RemoveOperationFirstCase(AssociativeTreeNode <T> nodeForRemove, int valueForReplace = -1) { var prevNode = FindPrev(nodeForRemove); if (prevNode.Left != -1) { prevNode.Left = valueForReplace; } else if (prevNode.Right != -1) { prevNode.Right = valueForReplace; } }
public void DeleteNode(AssociativeTreeNode <T> treeNode) { if (treeNode.Equals(Root)) { DeleteRoot(); } else { DeleteNode(Root, treeNode); } DeleteInArray(treeNode); }
private int FindIndexOfNode(AssociativeTreeNode <T> nodeForSearch) { int index = -1; for (int i = 0; i < Count && (index == -1); i++) { if (nodeForSearch.Equals(AssociativeTreeNodes[i])) { index = i; } } return(index); }
private void AssociativeTreeOutput(AssociativeTreeNode <T> treeNode, AssociativeTreeNode <T>[] array, int level) { if (treeNode != null) { var rightLeaf = (treeNode.Right != -1) ? array[treeNode.Right] : null; var leftLeaf = (treeNode.Left != -1) ? array[treeNode.Left] : null; AssociativeTreeOutput(rightLeaf, array, (level + 1)); Console.WriteLine($"{new string('\t',level)}{treeNode.Value}"); AssociativeTreeOutput(leftLeaf, array, (level + 1)); } }
private void StepToAllNodes(AssociativeTreeNode <T>[] array, AssociativeTreeNode <T> treeNode) { if (treeNode != null) { var leftNode = (treeNode.Left != -1) ? array[treeNode.Left] : null; StepToAllNodes(array, leftNode); if (treeNode.Left == -1 && treeNode.Right == -1) { linkedList.Add(treeNode.Value); } var rightNode = (treeNode.Right != -1) ? array[treeNode.Right] : null; StepToAllNodes(array, rightNode); } }
private void RemoveFromNodeOperation(AssociativeTreeNode <T> nodeForRemove) { var node = FindNode(nodeForRemove); if (node.Left == -1 && node.Right == -1) { RemoveOperationFirstCase(node); } else if ((node.Left == -1 && node.Right != -1) || (node.Left != -1 && node.Right == -1)) { RemoveOperationSecondCase(node); } else if (node.Left != -1 && node.Right != -1) { RemoveOperationThirdCase(node); } }
private bool Contains(AssociativeTreeNode <T> treeNode, AssociativeTreeNode <T> nodeForSearch) { if (treeNode != null) { var rightNode = (treeNode.Right != -1) ? AssociativeTreeNodes[treeNode.Right] : null; var leftNode = (treeNode.Left != -1) ? AssociativeTreeNodes[treeNode.Left] : null; if (nodeForSearch > treeNode) { return(Contains(rightNode, nodeForSearch)); } else if (nodeForSearch < treeNode) { return(Contains(leftNode, nodeForSearch)); } } return(treeNode != null && treeNode.Equals(nodeForSearch)); }
private AssociativeTreeNode <T> FindPrev(AssociativeTreeNode <T> treeNode, AssociativeTreeNode <T> nodeForSearch) { if (treeNode != null) { var leftNode = (treeNode.Left != -1) ? AssociativeTreeNodes[treeNode.Left] : null; var rightNode = (treeNode.Right != -1) ? AssociativeTreeNodes[treeNode.Right] : null; if (nodeForSearch > treeNode) { return(nodeForSearch.Equals(rightNode) ? treeNode : FindPrev(rightNode, nodeForSearch)); } else if (nodeForSearch < treeNode) { return(nodeForSearch.Equals(leftNode) ? treeNode : FindPrev(leftNode, nodeForSearch)); } } throw new TreeException("Can't Find the node!"); }
private void AddNode(AssociativeTreeNode <T> treeNode, AssociativeTreeNode <T> nodeForAdd) { if (nodeForAdd > treeNode) { if (treeNode.Right == -1) { if (Count == AssociativeTreeNodes.Length) { Array.Resize(ref AssociativeTreeNodes, Count * 2); } treeNode.Right = Count; AssociativeTreeNodes[Count++] = nodeForAdd; } else { AddNode(AssociativeTreeNodes[treeNode.Right], nodeForAdd); } } else if (nodeForAdd < treeNode) { if (treeNode.Left == -1) { if (Count == AssociativeTreeNodes.Length) { Array.Resize(ref AssociativeTreeNodes, Count * 2); } treeNode.Left = Count; AssociativeTreeNodes[Count++] = nodeForAdd; } else { AddNode(AssociativeTreeNodes[treeNode.Left], nodeForAdd); } } else { throw new TreeException("The Tree contains the element"); } }
private void DeleteNode(AssociativeTreeNode <T> treeNode, AssociativeTreeNode <T> nodeForRemove) { if (treeNode != null) { var rightNode = (treeNode.Right != -1) ? AssociativeTreeNodes[treeNode.Right] : null; var leftNode = (treeNode.Left != -1) ? AssociativeTreeNodes[treeNode.Left] : null; if (nodeForRemove > treeNode) { DeleteNode(rightNode, nodeForRemove); } else if (nodeForRemove < treeNode) { DeleteNode(leftNode, nodeForRemove); } else if (nodeForRemove.Equals(treeNode)) { RemoveFromNodeOperation(treeNode); } } }
private void StepToAllNodes(AssociativeTreeNode <T>[] array, AssociativeTreeNode <T> associativeTreeNode) { if (associativeTreeNode != null) { var leftNode = (associativeTreeNode.Left != -1) ? array[associativeTreeNode.Left] : null; StepToAllNodes(array, leftNode); var linkedList = GetRightBranch(array, associativeTreeNode); if (maxRightBranch == null) { maxRightBranch = linkedList; } else if (maxRightBranch.Count < linkedList.Count) { maxRightBranch = linkedList; } var rightNode = (associativeTreeNode.Right != -1) ? array[associativeTreeNode.Right] : null; StepToAllNodes(array, rightNode); } }
private AssociativeLinkedList <T> GetRightBranch(AssociativeTreeNode <T>[] array, AssociativeTreeNode <T> associativeTreeNode) { AssociativeLinkedList <T> associativeLinkedList = new AssociativeLinkedList <T>(); while (associativeTreeNode != null) { associativeLinkedList.Add(associativeTreeNode.Value); associativeTreeNode = (associativeTreeNode.Right != -1) ? array[associativeTreeNode.Right] : null; } return(associativeLinkedList); }
private void RemoveOperationSecondCase(AssociativeTreeNode <T> nodeForRemove) => RemoveOperationFirstCase(nodeForRemove, Math.Max(nodeForRemove.Left, nodeForRemove.Left));
private AssociativeTreeNode <T> GetMaxNode(AssociativeTreeNode <T> treeNode) => ((treeNode != null && treeNode.Right != -1) ? GetMaxNode(AssociativeTreeNodes[treeNode.Right]) : treeNode);
private AssociativeTreeNode <T> GetMinNode(AssociativeTreeNode <T> treeNode) => ((treeNode != null && treeNode.Left != -1) ? GetMinNode(AssociativeTreeNodes[treeNode.Left]) : treeNode);
private AssociativeTreeNode <T> FindPrev(AssociativeTreeNode <T> treeNode) => FindPrev(Root, treeNode);
private AssociativeTreeNode <T> FindNode(AssociativeTreeNode <T> treeNode) => FindNode(Root, treeNode);
public AssociativeTree(AssociativeTreeNode <T> treeNode) { Root = treeNode ?? throw new ArgumentNullException("Root can't be null"); AssociativeTreeNodes[Count++] = Root; }
public bool Contains(AssociativeTreeNode <T> treeNode) => Contains(Root, treeNode);