c#二叉树
想来自己一个非计算机专业的学生,学习编程以来受到了众多人的质疑。也不奇怪,毕竟什么都是自学的,而自学在很多情况下都难成体系。有次gf向2位做石油软件的仁兄请教:我作为一地质专业出身,凭个人对编程的爱好从事石油软件行业如何?2仁兄很诚恳的分析:“从石油方面来讲:你学地质的跟石油有差距;从计算机方面来讲,你根本不是学计算机的,不懂算法,所以不适合干这行。”
分析的严谨啊,分析的透彻啊!2点疑问:a.你学计算机的就可以进石油部门了啊?b.不学计算机的就不懂算法了啊?
算了不发这些牢骚了,自己在算法上确实不如人家科班出身,不过我还真要决心研究一下算法,他们计算机专业在学了课本《数据结构》后才会了“树”的一些算法,今天我就要自己研究它。事先说明,我从来没看过任何数据结构的书,也没用过“树”的相关程序, 要凭自己的理解编出一个“树”的程序,不为别的,就为实现:计算机科班的人是通过严尉敏的《数据结构》学会了“树”,而我能从我的自我理解实现“树”然后和它们的做对比。通过冥思苦想还终于实现了一个简单的,以后会继续深入的研究:
分析的严谨啊,分析的透彻啊!2点疑问:a.你学计算机的就可以进石油部门了啊?b.不学计算机的就不懂算法了啊?
算了不发这些牢骚了,自己在算法上确实不如人家科班出身,不过我还真要决心研究一下算法,他们计算机专业在学了课本《数据结构》后才会了“树”的一些算法,今天我就要自己研究它。事先说明,我从来没看过任何数据结构的书,也没用过“树”的相关程序, 要凭自己的理解编出一个“树”的程序,不为别的,就为实现:计算机科班的人是通过严尉敏的《数据结构》学会了“树”,而我能从我的自我理解实现“树”然后和它们的做对比。通过冥思苦想还终于实现了一个简单的,以后会继续深入的研究:
using System;
using System.Collections.Generic;
using System.Text;
namespace BinaryTreeTest
{
class Node //定义树的节点
{
int _content;
Node _leftLink;
Node _rightLink;
public Node(int content)
{
_content = content;
_leftLink = null;
_rightLink = null;
}
public int Content
{
set { _content = value; }
get { return _content; }
}
public Node LeftLink
{
get { return _leftLink; }
set { _leftLink = value; }
}
public Node RightLink
{
get { return _rightLink; }
set { _rightLink = value; }
}
}
class BinTree
{
readonly Node root;
public BinTree(int content)
{
root = new Node(content);
}
public void AddNode(int content)
{
Node tNode = root;
Node sNode = null;
while (tNode != null) //while循环用来定位要插入节点位置的父节点
{
sNode = tNode; //sNode 用来记录要插入节点的父节点
if (content < tNode.Content)
{
tNode = tNode.LeftLink;
}
else
{
tNode = tNode.RightLink;
}
}
if (content < sNode.Content) //添加节点
sNode.LeftLink = new Node(content);
else
sNode.RightLink = new Node(content);
}
private void Print(Node aNode) //中序遍历
{
if (aNode.LeftLink != null)
{
Print(aNode.LeftLink);
}
Console.Write("{0}\t", aNode.Content);
if (aNode.RightLink != null)
{
Print(aNode.RightLink);
}
}
public void PrintAll()
{
Print(root);
}
}
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
BinTree t = new BinTree(rnd.Next(100));
for (int i = 0; i < 9; i++)
t.AddNode(rnd.Next(100));
Console.WriteLine();
t.PrintAll();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace BinaryTreeTest
{
class Node //定义树的节点
{
int _content;
Node _leftLink;
Node _rightLink;
public Node(int content)
{
_content = content;
_leftLink = null;
_rightLink = null;
}
public int Content
{
set { _content = value; }
get { return _content; }
}
public Node LeftLink
{
get { return _leftLink; }
set { _leftLink = value; }
}
public Node RightLink
{
get { return _rightLink; }
set { _rightLink = value; }
}
}
class BinTree
{
readonly Node root;
public BinTree(int content)
{
root = new Node(content);
}
public void AddNode(int content)
{
Node tNode = root;
Node sNode = null;
while (tNode != null) //while循环用来定位要插入节点位置的父节点
{
sNode = tNode; //sNode 用来记录要插入节点的父节点
if (content < tNode.Content)
{
tNode = tNode.LeftLink;
}
else
{
tNode = tNode.RightLink;
}
}
if (content < sNode.Content) //添加节点
sNode.LeftLink = new Node(content);
else
sNode.RightLink = new Node(content);
}
private void Print(Node aNode) //中序遍历
{
if (aNode.LeftLink != null)
{
Print(aNode.LeftLink);
}
Console.Write("{0}\t", aNode.Content);
if (aNode.RightLink != null)
{
Print(aNode.RightLink);
}
}
public void PrintAll()
{
Print(root);
}
}
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
BinTree t = new BinTree(rnd.Next(100));
for (int i = 0; i < 9; i++)
t.AddNode(rnd.Next(100));
Console.WriteLine();
t.PrintAll();
Console.ReadLine();
}
}
}
浙公网安备 33010602011771号