示例#1
0
        /// <summary>
        /// Determines whether the specified <see cref="System.IBinaryTree{T}"/> is identical (same structure same data, but not necessarily same type) to the current <see cref="System.BinaryTree{T}"/>.
        /// </summary>
        /// <param name="tree">The <see cref="System.IBinaryTree{T}"/> to compare.</param>
        /// <returns>true if the two trees are identical, otherwise false.</returns>
        public bool Equals(IBinaryTree <T> tree)
        {
            if (this == tree)
            {
                return(true);
            }
            else
            {
                var ie1 = this.GetBreadthFirstEnumerator();
                var ie2 = tree.GetBreadthFirstEnumerator();
                while (ie1.MoveNext())
                {
                    if (ie2.MoveNext())
                    {
                        var curr1   = ie1.Current;
                        var curr2   = ie2.Current;
                        var isNull1 = (curr1 == null);
                        var isNull2 = (curr2 == null);

                        if (isNull1 != isNull2)
                        {
                            return(false);
                        }
                        else if (!isNull1)
                        {
                            var v1 = curr1.Value;
                            var v2 = curr2.Value;
                            isNull1 = (v1 == null);
                            isNull2 = (v2 == null);
                            if (isNull1 != isNull2 || (!isNull1 && !v1.Equals(v2)))
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                if (ie2.MoveNext())
                {
                    return(false);
                }

                return(true);
            }
        }
示例#2
0
        public static void WriteBinaryTree <T>(this BitStream stream, IBinaryTree <T> tree, Action <BitStream, T> writeValue)
        {
            var ie = tree.GetBreadthFirstEnumerator();

            while (ie.MoveNext())
            {
                var node = (IBinaryTree <T>)ie.Current;
                if (node == null)
                {
                    continue;
                }

                var isNull = !typeof(T).IsValueType && node.Value == null;
                stream.WriteBit(isNull);
                stream.WriteBit(node.LeftChild == null);
                stream.WriteBit(node.RightChild == null);
                if (!isNull)
                {
                    writeValue(stream, node.Value);
                }
            }
        }