二叉树类(C#语言)

二叉树是一种非常有用的数据结构,可用它实现大量操作,其中包括以极快的速度来排序和搜索数据。以下是用C#语言写的一个二叉树类:
using System;
using System.Collections.Generic;
using System.Text;

namespace BinaryTree
{
    public class Tree<T> where T:IComparable<T>
    {
        public Tree(T nodeValue)
        {
            this.data=nodeValue;
            this.left=null;
            this.right=null;
        }
        public T NodeData
        {
            get { return this.data; }
            set { this.data = value; }
        }
        public Tree<T> LeftTree
        {
            get { return this.left; }
            set { this.left = value; }
        }
        public Tree<T> RightTree
        {
            get { return this.right; }
            set { this.right = value; }
        }
        public void Insert(T newItem)
        {
            T currentNodeValue = this.NodeData;
            if (currentNodeValue.CompareTo(newItem) > 0)
            {
                if (this.LeftTree == null)
                {
                    this.LeftTree = new Tree<T>(newItem);
                }
                else
                {
                    this.LeftTree.Insert(newItem);
                }
            }
            else
            {
                if (this.RightTree == null)
                {
                    this.RightTree = new Tree<T>(newItem);
                }
                else
                {
                    this.RightTree.Insert(newItem);
                }
            }
        }
        public void WalkTree()
        {
            if (this.LeftTree != null)
            {
                this.LeftTree.WalkTree();
            }
            Console.WriteLine(this.NodeData.ToString());
            if (this.RightTree != null)
            {
                this.RightTree.WalkTree();
            }
        }
        private T data;
        private Tree<T> left;
        private Tree<T> right;
    }
}




文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: C# 数据结构
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.