// This will Search for an entry in the list // returns true if found, // false if not found public BalancedNode Search(IComparable v) { BalancedNode result = null; // Here I am at a node if (v.CompareTo(Value) < 0) { // I am less than this nodes Value if (Left != null) { result = Left.Search(v); } } else if (v.CompareTo(Value) > 0) { // I am higher than this nodes Value if (Right != null) { result = Right.Search(v); } } else { // I Already exist in the tree result = this; } return(result); }
static int HigherOf(BalancedNode l, BalancedNode r) { int dl = (l != null) ? l.depth : -1; int dr = (r != null) ? r.depth : -1; return((dl > dr) ? dl : dr); }
void TraverseTree(BalancedNode c) { if (c.Left != null) { TraverseTree(c.Left); } elements[current++] = c; if (c.Right != null) { TraverseTree(c.Right); } }
static int Main(string[] args) { int exitcode = 0; String typelib = null; String outfile = null; String verifyfile = null; String dump = null; bool silent = false; bool verifyiid = false; bool inbuild = false; try { Setting[] switches = GetSwitches(args); foreach (Setting sw in switches) { if (sw.option == null) { if (typelib != null) { // We already have a typelib PrintLogo(); WriteErrorMsg("Only one typelibrary can be operated on at a time."); exitcode = 1; goto done; } else { // We are cool with this typelib = sw.value; } } else { if (CompareString(sw.option.value, "/out") == 0) { outfile = sw.value; } else if (CompareString(sw.option.value, "/verify") == 0) { verifyfile = sw.value; } else if (CompareString(sw.option.value, "/iid") == 0) { verifyiid = true; } else if (CompareString(sw.option.value, "/dump") == 0) { dump = sw.value; } else if (CompareString(sw.option.value, "/silent") == 0) { silent = true; } else if (CompareString(sw.option.value, "/build") == 0) { inbuild = true; } else if (CompareString(sw.option.value, "/?") == 0) { PrintLogo(); PrintUsage(); exitcode = 0; goto done; } } } if (!silent) { PrintLogo(); } if (typelib == null) { WriteErrorMsg("No Type library file specified"); exitcode = 1; goto done; } ExtractNames(typelib); if (dump != null) { symbols.DumpAsTreeToFile(dump); if (!silent) { Console.WriteLine("Successfully generated " + dump + " from " + typelib); } } // Is an output file desired? if (outfile != null) { symbols.DumpToFile(outfile); if (!silent) { Console.WriteLine("Successfully generated " + outfile + " from " + typelib); } } // Is verification desired? if (verifyfile != null) { if (!silent) { if (inbuild) { Console.Write("Build_Status "); } Console.WriteLine("Verify the names from " + typelib); } int errors = 0; verify = Names.LoadFromFile(verifyfile); foreach (BalancedNode s in symbols) { if (s.Value != null) { Name n = (Name)s.Value; BalancedNode o = verify.Search(n); if (o == null) { Console.WriteLine(typelib + " : error N2000 : Symbol named '" + n.Value + "' added to typelib."); ++errors; } else { Name f = (Name)o.Value; f.Found = true; if (n.UUID != f.UUID) { f.NEWUUID = n.UUID; } } } } foreach (BalancedNode s in verify) { if (s.Value != null) { Name n = (Name)s.Value; if (n.Found != true) { Console.WriteLine(typelib + " : error N2001 : Symbol named '" + n.Value + "' removed from typelib."); ++errors; } } } if (verifyiid) { foreach (BalancedNode s in verify) { if (s.Value != null) { Name n = (Name)s.Value; if (n.NEWUUID != Guid.Empty) { Console.WriteLine(typelib + " : error N2002 : Guid changed for symbol named '" + n.Value + "'; was " + n.UUID + " now is " + n.NEWUUID); ++errors; } } } } if (!silent) { if (errors != 0) { Console.WriteLine(typelib + " : error N1000 : errors comparing typelib to names file " + verifyfile); } else if (!inbuild) { Console.WriteLine(typelib + " : no errors comparing typelib to names file " + verifyfile); } } exitcode = errors; } } catch (Exception e) { Console.WriteLine(typelib + " : error N1001 : Exception " + e.Message); return(1); } done: return(exitcode); }
public BalancedTree(IComparable v) { root = new BalancedNode(v); count = 1; }
// Used to keep the tree balanced, deepened indicates when The depth of the subtree has changed private bool InternalInsert(IComparable v, ref bool deepened) { bool result = false; // Here I am at a node if (v.CompareTo(Value) < 0) { // I am less than this nodes Value if (Left == null) { result = true; Left = new BalancedNode(v); if (Right == null) { ++depth; deepened = true; } } else { result = Left.InternalInsert(v, ref deepened); // Maintain an accurate depth count if (deepened) { ++depth; } } } else if (v.CompareTo(Value) > 0) { // I am above than this nodes Value if (Right == null) { result = true; Right = new BalancedNode(v); if (Left == null) { ++depth; deepened = true; } } else { result = Right.InternalInsert(v, ref deepened); // Maintain an accurate depth count if (deepened) { ++depth; } } } else { // I Already exist in the tree result = false; } // I have added my Node and updated my Depths // Now I need to ensure that my left and right subtrees differ by no more than 1 if (deepened) { // Node with empty left and right is at depth -1 // int left = (Left != null) ? Left.depth : -1; int right = (Right != null) ? Right.depth : -1; if (right - left > 1) { // Right subtree is deeper than left subtree IComparable t = Value; BalancedNode l = Left; BalancedNode r = Right.Left; Value = Right.Value; Right = Right.Right; Left = new BalancedNode(t); Left.Left = l; Left.Right = r; Left.depth = HigherOf(Left.Left, Left.Right) + 1; } else if (left - right > 1) { // Left subtree is deeper than the right subtree IComparable t = Value; BalancedNode r = Right; BalancedNode l = Left.Right; Value = Left.Value; Left = Left.Left; Right = new BalancedNode(t); Right.Right = r; Right.Left = l; Right.depth = HigherOf(Right.Left, Right.Right) + 1; } } depth = HigherOf(Left, Right) + 1; return(result); }
public TreeEnumerator(BalancedNode root, int nitems) { elements = new object[nitems]; current = 0; TraverseTree(root); }