public static int Traverse(string Name, List <PatriciaTreeNode> Nodes, out PatriciaTreeNode Root, uint Bit = 0) { Root = Nodes[0]; int Output = Root.LeftNodeIndex; PatriciaTreeNode Left = Nodes[Output]; while (Root.ReferenceBit > Left.ReferenceBit && Left.ReferenceBit > Bit) { if (GetBit(Name, Left.ReferenceBit)) { Output = Left.RightNodeIndex; } else { Output = Left.LeftNodeIndex; } Root = Left; Left = Nodes[Output]; } return(Output); }
public STRGPatriciaTree(EndianBinaryReaderEx er) { RootNodeIndex = er.ReadUInt32(); NrNodes = er.ReadUInt32(); Nodes = new PatriciaTreeNode[NrNodes]; for (int i = 0; i < NrNodes; i++) { Nodes[i] = new PatriciaTreeNode(er); } }
public static void Insert(List <PatriciaTreeNode> Nodes, PatriciaTreeNode Value, int MaxLength) { uint Bit = (uint)((MaxLength << 3) - 1); PatriciaTreeNode Root = Nodes[0]; int Index = Traverse(Value.Name, Nodes, out Root); while (GetBit(Nodes[Index].Name, Bit) == GetBit(Value.Name, Bit)) { if (--Bit == uint.MaxValue) { throw new InvalidOperationException(DuplicateKeysEx); } } Value.ReferenceBit = Bit; if (GetBit(Value.Name, Bit)) { Value.LeftNodeIndex = (ushort)Traverse(Value.Name, Nodes, out Root, Bit); Value.RightNodeIndex = (ushort)Nodes.Count; } else { Value.LeftNodeIndex = (ushort)Nodes.Count; Value.RightNodeIndex = (ushort)Traverse(Value.Name, Nodes, out Root, Bit); } int RootIndex = Nodes.IndexOf(Root); if (GetBit(Value.Name, Root.ReferenceBit)) { Root.RightNodeIndex = (ushort)Nodes.Count; } else { Root.LeftNodeIndex = (ushort)Nodes.Count; } Nodes.Add(Value); Nodes[RootIndex] = Root; }