/// <summary>
        /// The method to perform tree reconfiguration
        /// </summary>
        /// <param name="n1">
        /// A <see cref="Node"/>
        /// Reference to the first node
        /// </param>
        /// <param name="n2">
        /// A <see cref="Node"/>
        /// Reference to the second node
        /// </param>
        private void SwapNodes(Node n1,Node n2)
        {
            int tmp = n1.freq;
            n1.freq = n2.freq;
            n2.freq = tmp;

            Node t1 = n1.parent.left;
            Node t2 = n2.parent.left;

            //Determine if n1 was the left or the right child
            if (t1 == n1)
                n1.parent.left = n2;
            else
                n1.parent.right = n2;

            //Same thing for the second node
            if (t2 == n2)
                n2.parent.left = n1;
            else
                n2.parent.right = n1;

            //Swap the nodes
            Node t3 = n1.parent;
            n1.parent = n2.parent;
            n2.parent = t3;
        }
        public int DecodeBinary(int bit)
        {
            try {
            if (ptr == null) ptr = root;
            if (InNyt) {
                _tempcode <<= 1;
                _tempcode |= bit;
                _count++;
                if (_count == 8) {
                    UpdateTree(_tempcode);
                    int sym = _tempcode;
                    _tempcode = _count = 0;
                    InNyt = false;
                    return sym;
                }
                return CharIsEof;
            }

            if (bit == 1) ptr = ptr.right;
            else ptr = ptr.left;

            if (ptr.nt == NodeType.NYT && ptr.sym == 257) {
                ptr = root;
                InNyt = true;
                return CharIsEof;
            }
            if (ptr.nt == NodeType.SYM) {
                int sym = ptr.sym;
                UpdateTree(sym);
                ptr = root;
                return sym;
            }
            return CharIsEof;
            }
            catch (System.NullReferenceException) {
                throw new Exception("Corrupted Huffman sequence supplied for decoding");
            }
        }
        /// <summary>
        /// Returns the reference to the node that has the same frequency
        /// as the argument and highest order
        /// </summary>
        /// <param name="nd">
        /// A <see cref="Node"/>
        /// Reference to the node
        /// </param>
        /// <returns>
        /// A <see cref="Node"/>
        /// </returns>
        private Node FindHighestWithSameFreq(Node nd)
        {
            Node current = nd;
            if (nd.parent != null) {
                Node nd2 = current.parent;
                if ((nd2.left == current) && (nd2.right.freq == current.freq))
                    current = nd2.right;

                if (nd2.parent != null) {
                    Node nd3 = nd2.parent;
                    if ((nd3.left == nd2) && (nd3.right.freq == current.freq))
                        current = nd3.right;
                    else if ((nd3.right == nd2) && (nd3.left.freq == current.freq))
                        current = nd3.left;
                }
            }

            return current;
        }
 /// <summary>
 /// The constructor for the Tree class
 /// </summary>
 public Tree()
 {
     root = new Node (NodeType.INT, 258, 0, 516);
     root.right = new Node (NodeType.NYT, 257, 0, root.order - 1);
     root.left = new Node (NodeType.EOF, 256, 0, root.order - 2);
     root.left.parent = root.right.parent = root;
     nodes = new Node[259];
     nodes[256] = root.left;
     nodes[257] = root.right;
     nodeCount = 258;
 }