public void Add(ComponentName name, IHandler handler) { if (root == null) { root = new TreeNode(name, handler); count++; return; } TreeNode current = root; while (true) { int cmp = String.Compare(current.CompName.Service, name.Service); if (cmp < 0) { if (current.Left != null) { current = current.Left; } else { current.Left = new TreeNode(name, handler); count++; break; } } else if (cmp > 0) { if (current.Right != null) { current = current.Right; } else { current.Right = new TreeNode(name, handler); count++; break; } } else { current.AddSibling(new TreeNode(name, handler)); count++; break; } } }
private void RemoveBinaryTreeNode(TreeNode node) { if (node.Left != null && node.Right != null) { TreeNode inorderSuccessor = FindSuccessor(node); RemoveBinaryTreeNode(inorderSuccessor); //it has maximum one node to reorder PromoteNode(node, inorderSuccessor); } else if (node.Left == null && node.Right == null) { ReplaceNode(node, null); } else if (node.Left != null) { ReplaceNode(node, node.Left); node.Left = null; } else if (node.Right != null) { ReplaceNode(node, node.Right); node.Right = null; } }
public void AddSibling(TreeNode node) { if (NextSibling == null) { NextSibling = node; return; } TreeNode current = NextSibling; while (current.NextSibling != null) { current = current.NextSibling; } current.NextSibling = node; }
private void ReplaceNode(TreeNode oldNode, TreeNode newNode) { TreeNode parent = oldNode.Parent; if (parent == null) { root = newNode; } else if (parent.Right == oldNode) { parent.Right = newNode; } else if (parent.Left == oldNode) { parent.Left = newNode; } //throw if wanted }
private void PromoteNode(TreeNode oldNode, TreeNode promoted) { if (promoted.Right != null || promoted.Left != null) throw new InvalidOperationException("promoted node can not have child nodes, remove node from tree before promoting"); promoted.Right = oldNode.Right; promoted.Left = oldNode.Left; if (root == oldNode) root = promoted; else ReplaceNode(oldNode, promoted); }
/// <summary> /// Method finds the next biggest node /// It assumes Add puts lesser nodes on the right /// </summary> private TreeNode FindSuccessor(TreeNode node) { TreeNode current = node.Left; if (current != null) { while (current.Right != null) { current = current.Right; } } return current; }
private void Remove(TreeNode node) { if (node == null) return; //A real tree node (this should be better indicated, there is really two types of nodes here) if (node.PreviousSibling == null) { if (node.NextSibling != null) { TreeNode replacement = node.NextSibling; replacement.PreviousSibling = null; PromoteNode(node, replacement); } else { RemoveBinaryTreeNode(node); } } else node.PreviousSibling.NextSibling = node.NextSibling; count--; }
internal void Visit(TreeNode node, ArrayList list) { if (node == null) return; list.Add(node.Handler); if (node.Left != null) { Visit(node.Left, list); } if (node.Right != null) { Visit(node.Right, list); } while (node.NextSibling != null) { list.Add(node.NextSibling.Handler); node = node.NextSibling; } }