zoukankan
html css js c++ java
C#实现二叉树外带中序遍历(转载)
using
System;
namespace
BinaryTree
{
// Binary Tree的结点类
class
Node
{
public
int
Data {
get
;
set
; }
public
Node LeftSubNode {
get
;
set
; }
public
Node RightSubNode {
get
;
set
; }
// 结点为自己追加子结点(与向左/向右追加结点,形成递归)
public
void
Append(Node subNode)
{
if
(subNode.Data <=
this
.Data)
{
this
.AppendLeft(subNode);
}
else
{
this
.AppendRight(subNode);
}
}
// 向左追加
public
void
AppendLeft(Node subNode)
{
if
(
this
.LeftSubNode ==
null
)
{
this
.LeftSubNode = subNode;
}
else
{
this
.LeftSubNode.Append(subNode);
}
}
// 向右追加
public
void
AppendRight(Node subNode)
{
if
(
this
.RightSubNode ==
null
)
{
this
.RightSubNode = subNode;
}
else
{
this
.RightSubNode.Append(subNode);
}
}
// 结点显示自己的数据
public
void
ShowData()
{
Console.WriteLine(
"Data={0}"
,
this
.Data);
}
}
// BinaryTree类
class
Tree
{
// 根结点
public
Node Root {
get
;
set
; }
// 以根结点为起点,插入结点
public
void
Insert(Node newNode)
{
if
(
this
.Root ==
null
)
{
this
.Root = newNode;
}
else
{
this
.Root.Append(newNode);
}
}
// 重载,默认以根结点为起点遍历
public
void
MidTravel()
{
this
.MidTravel(
this
.Root);
}
// 中序遍历(递归)
public
void
MidTravel(Node node)
{
if
(node.LeftSubNode !=
null
)
{
this
.MidTravel(node.LeftSubNode);
}
node.ShowData();
if
(node.RightSubNode !=
null
)
{
this
.MidTravel(node.RightSubNode);
}
}
}
class
Program
{
static
void
Main(
string
[] args)
{
Tree tree =
new
Tree();
tree.Insert(
new
Node { Data = 3 });
tree.Insert(
new
Node { Data = 6 });
tree.Insert(
new
Node { Data = 2 });
tree.Insert(
new
Node { Data = 7 });
tree.Insert(
new
Node { Data = 18 });
tree.MidTravel();
}
}
}
查看全文
相关阅读:
关于集合中的实现细节
数组与内存控制笔记
python进阶------进程线程(五)
python进阶------进程线程(四)
python进阶------进程线程(三)
python进阶-------进程线程(二)
python进阶------进程线程(一)
python进阶---Python中的socket编程
Python基础---python中的异常处理
Python进阶---面向对象第三弹(进阶篇)
原文地址:https://www.cnblogs.com/guoxiaowen/p/1262571.html
最新文章
Duilib中各个类的简单介绍
VC 和 GDI+ 实现仿ibook 翻页效果
图片放大方法
点击放大图片或者文字
reflection倒影
jquery弹出插件
关于未知大小的文字和图片垂直居中的学习
单选和多选框与文字对齐方法总结
十二月读书计划
margin学习探索
热门文章
base64的图片处理技术
时间戳转换成精细化的时分秒格式
tomcat内存溢出解决办法
两个double相加出现精度问题的解决方法
对oracle的基本了解
关于oracle中的约束
jvm中的堆栈与数据结构中的堆栈
字符串在内存中的情况
关于java中的内存的理解总结
快速创建oracle用户的一系列命令步骤
Copyright © 2011-2022 走看看