private bool InsertItem(T item, int level, BinaryTreeNode <T> root) { if (root == null || level < 1) { return(false); } if (level <= 2) { if (root.LeftChild == null) { root.AddLeftChild(item); UpHeapify(root.LeftChild); return(true); } else if (root.RightChild == null) { root.AddRightChild(item); UpHeapify(root.RightChild); return(true); } } bool inserted = false; inserted = InsertItem(item, level - 1, root.LeftChild); if (!inserted) { inserted = InsertItem(item, level - 1, root.RightChild); } return(inserted); }
private void RotateRight(BinaryTreeNode <T> node) { BinaryTreeNode <T> rotatedRoot = new BinaryTreeNode <T>(node.Value); rotatedRoot.AddRightChild(node.RightChild); rotatedRoot.AddLeftChild(node.LeftChild.RightChild); node.Value = node.LeftChild.Value; node.AddLeftChild(node.LeftChild.LeftChild); node.AddRightChild(rotatedRoot); }
public void AddRightChild(BinaryTreeNode <T> node) { if (node == null) { rightChild = null; return; } BinaryTreeNode <T> treeNode = new BinaryTreeNode <T>(node.Value, this); treeNode.AddLeftChild(node.LeftChild); treeNode.AddRightChild(node.RightChild); rightChild = treeNode; }
public void AddLeftChildTo(BinaryTreeNode <T> parent, T leftChildItem) { parent.AddLeftChild(leftChildItem); }