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();
}
}
}
查看全文
相关阅读:
MyBatis配置项--配置环境(environments)--数据源(dataSource)
MyBatis配置项--配置环境(environments)--数据源(dataSource)
开发AI+诊疗生发系统,「先锋汇美」借力人工智能辅助诊疗实现头皮医学检测...
Node.js简述
Node.js简述
解决跨域问题代码示例
解决跨域问题代码示例
javaScript常用到的方法
javaScript常用到的方法
接口自动化测试平台-接入持续集成jenkins
原文地址:https://www.cnblogs.com/guoxiaowen/p/1262571.html
最新文章
在class里面写入图片。+button
ATL中宏定义offsetofclass的分析
codeforces 460C
HDU 2222 AC自己主动机模版题
零基础学python-2.4 操作符
适配器模式
Android---59---Toast的使用
linux下安装配置JDK,具体步骤图文
手机管理应用研究【5】——应用杂篇
HTML5 新的API 窗体可视区 scrollIntoView dataset calssList
热门文章
BZOJ1296: [SCOI2009]粉刷匠
BZOJ2734: [HNOI2012]集合选数
BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节
BZOJ1642: [Usaco2007 Nov]Milking Time 挤奶时间
BZOJ1629: [Usaco2007 Demo]Cow Acrobats
BZOJ1627: [Usaco2007 Dec]穿越泥地
BZOJ1677: [Usaco2005 Jan]Sumsets 求和
BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
BZOJ1231: [Usaco2008 Nov]mixup2 混乱的奶牛
BZOJ1782: [Usaco2010 Feb]slowdown 慢慢游
Copyright © 2011-2022 走看看