⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree2.cs

📁 树的简单使用方法和基本操作,这是一个CS程序.
💻 CS
📖 第 1 页 / 共 5 页
字号:
			}

			public RootObject( IEqualityComparer<T> dataComparer )
			{
				_DataComparer = dataComparer;
			}

			/// <summary>Obtains the <see cref="String"/> representation of this instance.</summary>
			/// <returns>The <see cref="String"/> representation of this instance.</returns>
			/// <remarks>
			/// <p>
			/// This method returns a <see cref="String"/> that represents this instance.
			/// </p>
			/// </remarks>
			public override string ToString() { return "视频文件" ; }

			// Save
			/// <summary>Populates a SerializationInfo with the data needed to serialize the target T.</summary>
			/// <param name="info">The SerializationInfo to populate with data.</param>
			/// <param name="context">The destination for this serialization.</param>
			/// <remarks>
			/// <p>This method is called during serialization.</p>
			/// <p>Do not call this method directly.</p>
			/// </remarks>
			[SecurityPermission( SecurityAction.Demand, SerializationFormatter = true )]
			public override void GetObjectData( SerializationInfo info, StreamingContext context )
			{
				base.GetObjectData( info, context );

				info.AddValue( "RootObjectVersion", 1 );
				//info.AddValue( "DataType", _DataType );
			}

			// Load
			/// <summary>Initializes a new instance of the class during deserialization.</summary>
			/// <param name="info">The SerializationInfo populated with data.</param>
			/// <param name="context">The source for this serialization.</param>
			/// <remarks>
			/// <p>This method is called during deserialization.</p>
			/// <p>Do not call this method directly.</p>
			/// </remarks>
			protected RootObject( SerializationInfo info, StreamingContext context )
				: base( info, context )
			{
				int iVersion = info.GetInt32( "RootObjectVersion" );
				if ( iVersion != 1 ) throw new SerializationException( "Unknown version" );

				//_DataType = info.GetValue( "DataType", typeof( Type ) ) as Type;
			}
		}

		//-----------------------------------------------------------------------------
		// GetRootObject

		protected virtual RootObject GetRootObject
		{
			get
			{
				return ( RootObject ) Root;
			}
		}

		//-----------------------------------------------------------------------------
		// DataComparer

		public virtual IEqualityComparer<T> DataComparer
		{
			get
			{
				if ( !Root.IsTree ) throw new InvalidOperationException( "This is not a Tree" );

				return GetRootObject.DataComparer;
			}

			set
			{
				if ( !Root.IsTree ) throw new InvalidOperationException( "This is not a Tree" );

				GetRootObject.DataComparer = value;
			}
		}

		//-----------------------------------------------------------------------------
		// Version

		protected virtual int Version
		{
			get
			{
				INode<T> root = Root;

				if ( !root.IsTree ) throw new InvalidOperationException( "This is not a Tree" );

				return GetNodeTree( root ).Version;
			}

			set
			{
				INode<T> root = Root;

				if ( !root.IsTree ) throw new InvalidOperationException( "This is not a Tree" );

				GetNodeTree( root ).Version = value;
			}
		}

		protected bool HasChanged( int version ) { return ( Version != version ); }

		protected void IncrementVersion()
		{
			INode<T> root = Root;

			if ( !root.IsTree ) throw new InvalidOperationException( "This is not a Tree" );

			GetNodeTree( root ).Version++;
		}

		//-----------------------------------------------------------------------------
		// ISerializable

		// Save
		/// <summary>Populates a SerializationInfo with the data needed to serialize the target T.</summary>
		/// <param name="info">The SerializationInfo to populate with data.</param>
		/// <param name="context">The destination for this serialization.</param>
		/// <remarks>
		/// <p>This method is called during serialization.</p>
		/// <p>Do not call this method directly.</p>
		/// </remarks>
		[SecurityPermission( SecurityAction.Demand, SerializationFormatter = true )]
		public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			info.AddValue( "NodeTreeVersion", 1 );
			info.AddValue( "Data", _Data );
			info.AddValue( "Parent", _Parent );
			info.AddValue( "Previous", _Previous );
			info.AddValue( "Next", _Next );
			info.AddValue( "Child", _Child );
		}

		// Load
		/// <summary>Initializes a new instance of the class during deserialization.</summary>
		/// <param name="info">The SerializationInfo populated with data.</param>
		/// <param name="context">The source for this serialization.</param>
		/// <remarks>
		/// <p>This method is called during deserialization.</p>
		/// <p>Do not call this method directly.</p>
		/// </remarks>
		protected NodeTree( SerializationInfo info, StreamingContext context )
		{
			int iVersion = info.GetInt32( "NodeTreeVersion" );
			if ( iVersion != 1 ) throw new SerializationException( "Unknown version" );

			_Data = ( T ) info.GetValue( "Data", typeof( T ) );
			_Parent = ( NodeTree<T> ) info.GetValue( "Parent", typeof( NodeTree<T> ) );
			_Previous = ( NodeTree<T> ) info.GetValue( "Previous", typeof( NodeTree<T> ) );
			_Next = ( NodeTree<T> ) info.GetValue( "Next", typeof( NodeTree<T> ) );
			_Child = ( NodeTree<T> ) info.GetValue( "Child", typeof( NodeTree<T> ) );
		}

		//-----------------------------------------------------------------------------
		// XmlSerialization

		public virtual void XmlSerialize( Stream stream )
		{
			XmlSerializer xmlSerializer;

			try
			{
				xmlSerializer = new XmlSerializer( typeof( TreeXmlSerializationAdapter ) );
			}
			catch ( Exception x )
			{
				Debug.Assert( x == null ); // false
				throw;
			}

			try
			{
				xmlSerializer.Serialize( stream, new TreeXmlSerializationAdapter( XmlAdapterTag, this ) );
			}
			catch ( Exception x )
			{
				Debug.Assert( x == null ); // false
				throw;
			}

		}

		public static ITree<T> XmlDeserialize( Stream stream )
		{
			XmlSerializer xmlSerializer;

			try
			{
				xmlSerializer = new XmlSerializer( typeof( TreeXmlSerializationAdapter ) );
			}
			catch ( Exception x )
			{
				Debug.Assert( x == null ); // false
				throw;
			}

			object o;

			try
			{
				o = xmlSerializer.Deserialize( stream );
			}
			catch ( Exception x )
			{
				Debug.Assert( x == null ); // false
				throw;
			}

			TreeXmlSerializationAdapter adapter = ( TreeXmlSerializationAdapter ) o;

			ITree<T> tree = adapter.Tree;

			return tree;
		}

		//-----------------------------------------------------------------------------

		protected static readonly object XmlAdapterTag = new object();

		[XmlType( "Tree" )]
		public class TreeXmlSerializationAdapter
		{
			private int _Version = 0;

			[XmlAttribute]
			public int Version
			{
				get { return 1; }
				set { _Version = value; }
			}

			private ITree<T> _Tree;

			[XmlIgnore]
			public ITree<T> Tree
			{
				get { return _Tree; }
			}

			private TreeXmlSerializationAdapter()
			{
			}

			public TreeXmlSerializationAdapter( object tag, ITree<T> tree )
			{
				if ( !ReferenceEquals( XmlAdapterTag, tag ) ) throw new InvalidOperationException( "Don't use this class" );

				_Tree = tree;
			}

			public NodeXmlSerializationAdapter Root
			{
				get
				{
					return new NodeXmlSerializationAdapter( XmlAdapterTag, _Tree.Root );
				}

				set
				{
					_Tree = NodeTree<T>.NewTree();

					ReformTree( _Tree.Root, value );
				}
			}

			private void ReformTree( INode<T> parent, NodeXmlSerializationAdapter node )
			{
				foreach ( NodeXmlSerializationAdapter child in node.Children )
				{
					INode<T> n = parent.AddChild( child.Data );

					ReformTree( n, child );
				}
			}

		}

		[XmlType( "Node" )]
		public class NodeXmlSerializationAdapter
		{
			private int _Version = 0;

			[XmlAttribute]
			public int Version
			{
				get { return 1; }
				set { _Version = value; }
			}

			private INode<T> _Node;

			private IXmlCollection _Children = new ChildCollection();

			[XmlIgnore]
			public INode<T> Node
			{
				get { return _Node; }
			}

			// Load
			private NodeXmlSerializationAdapter()
			{
				_Node = NodeTree<T>.NewNode();
			}

			// Save
			public NodeXmlSerializationAdapter( object tag, INode<T> node )
			{
				if ( !ReferenceEquals( XmlAdapterTag, tag ) ) throw new InvalidOperationException( "Don't use this class" );

				_Node = node;

				foreach ( INode<T> child in node.DirectChildren.Nodes )
					_Children.Add( new NodeXmlSerializationAdapter( XmlAdapterTag, child ) );
			}

			public T Data
			{
				get { return _Node.Data; }

				set
				{
					GetNodeTree( _Node )._Data = value;
				}
			}

			public IXmlCollection Children
			{
				get { return _Children; }
				set { Debug.Assert( value == null ); }
			}

			public interface IXmlCollection : ICollection
			{
				NodeXmlSerializationAdapter this[ int index ] { get; }

				void Add( NodeXmlSerializationAdapter item );
			}

			private class ChildCollection : List<NodeXmlSerializationAdapter>, IXmlCollection { }
		}


		//-----------------------------------------------------------------------------
		// INode<T>

		public T Data
		{
			get
			{
				return _Data;
			}

			set
			{
				if ( IsRoot ) throw new InvalidOperationException( "This is a Root" );

				OnSetting( this, value );

				_Data = value;

				OnSetDone( this, value );
			}
		}

		public INode<T> Parent { get { return _Parent; } }
		public INode<T> Previous { get { return _Previous; } }
		public INode<T> Next { get { return _Next; } }
		public INode<T> Child { get { return _Child; } }

		//-----------------------------------------------------------------------------

		public ITree<T> Tree
		{
			get
			{
				return ( ITree<T> ) Root;
			}
		}

		public INode<T> Root
		{
			get
			{
				INode<T> node = this;

				while ( node.Parent != null )
					node = node.Parent;

				return node;
			}
		}

		public INode<T> Top
		{
			get
			{
				if ( !Root.IsTree ) throw new InvalidOperationException( "This is not a tree" );
				//if ( this.IsRoot ) throw new InvalidOperationException( "This is a Root" );
				if ( this.IsRoot ) return null;

				INode<T> node = this;

				while ( node.Parent.Parent != null )
					node = node.Parent;

				return node;
			}
		}

		public INode<T> First
		{
			get
			{
				INode<T> node = this;

				while ( node.Previous != null )
					node = node.Previous;

				return node;
			}
		}

		public INode<T> Last
		{
			get
			{
				INode<T> node = this;

				while ( node.Next != null )
					node = node.Next;

				return node;
			}
		}

		public INode<T> LastChild
		{
			get
			{
				if ( Child == null ) return null;
				return Child.Last;
			}
		}

		//-----------------------------------------------------------------------------

		public bool HasPrevious { get { return Previous != null; } }
		public bool HasNext { get { return Next != null; } }
		public bool HasChild { get { return Child != null; } }
		public bool IsFirst { get { return Previous == null; } }
		public bool IsLast { get { return Next == null; } }

		public bool IsTree
		{
			get
			{
				if ( !IsRoot ) return false;
				return this is RootObject;
			}
		}

		public bool IsRoot
		{
			get
			{
				bool b = ( Parent == null );

				if ( b )
				{
					Debug.Assert( Previous == null );
					Debug.Assert( Next == null );
				}

				return b;
			}
		}

		public bool HasParent
		{
			get
			{
				//if ( IsRoot ) throw new InvalidOperationException( "This is a Root" );
				if ( IsRoot ) return false;
				return Parent.Parent != null;
			}
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -