Click here to Skip to main content
15,893,486 members
Articles / Programming Languages / C#

Red-Black Trees in C#

Rate me:
Please Sign up or sign in to vote.
4.82/5 (30 votes)
14 Sep 2004CPOL8 min read 256.1K   5.9K   86  
A C# implementation of a Red-Black Tree.
///<summary>
/// The RedBlackNode class encapsulates a node in the tree
///</summary>

using System;
using System.Text;

namespace RedBlackCS
{
	public class RedBlackNode
	{
        // tree node colors
		public static int	RED		= 0;
		public static int	BLACK	= 1;

		// key provided by the calling class
		private IComparable ordKey;
		// the data or value associated with the key
		private object objData;
		// color - used to balance the tree
		private int intColor;
		// left node 
		private RedBlackNode rbnLeft;
		// right node 
		private RedBlackNode rbnRight;
        // parent node 
        private RedBlackNode rbnParent;
		
		///<summary>
		///Key
		///</summary>
		public IComparable Key
		{
			get
            {
				return ordKey;
			}
			
			set
			{
				ordKey = value;
			}
		}
		///<summary>
		///Data
		///</summary>
		public object Data
		{
			get
            {
				return objData;
			}
			
			set
			{
				objData = value;
			}
		}
		///<summary>
		///Color
		///</summary>
		public int Color
		{
			get
            {
				return intColor;
			}
			
			set
			{
				intColor = value;
			}
		}
		///<summary>
		///Left
		///</summary>
		public RedBlackNode Left
		{
			get
            {
				return rbnLeft;
			}
			
			set
			{
				rbnLeft = value;
			}
		}
		///<summary>
		/// Right
		///</summary>
		public RedBlackNode Right
		{
			get
            {
				return rbnRight;
			}
			
			set
			{
				rbnRight = value;
			}
		}
        public RedBlackNode Parent
        {
            get
            {
                return rbnParent;
            }
			
            set
            {
                rbnParent = value;
            }
        }

		public RedBlackNode()
		{
			Color = RED;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Roy is a software developer who digs all aspects of software development, from design and architecture to implementation.

Comments and Discussions