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();
}
}
}
查看全文
相关阅读:
后期生成事件命令copy /y
SevenZipShaper压缩类
vs2017
WCF路由服务
微服务--
各种流程图的绘画网路工具 processon
ROC 准确率,召回率 F-measure理解(转载)
Unix OpenCV安装
转载:tar 解压缩命令~
cppreference经验总结
原文地址:https://www.cnblogs.com/guoxiaowen/p/1262571.html
最新文章
JS脚本可视化调试支持——基于Google v8引擎的脚本调试
CLion 2016.1.1 下载 附注册激活码 破解版方法
Java Hashtable
C++变参数模板和...操作符
C++ Cross Platform Memory Leak Detector
C++ Traits和模板偏特化
C++内存动态分配
C++平台相关宏
C++ GC
C++反射
热门文章
C#直接赋值和反射赋值(无GC)的性能比较
C#成员函数直接调用和反射+委托的性能比较
验证控件 .net
.NET ViewState对于画面的速度影响
异常来自 HRESULT:0x80070057 (E_INVALIDARG)
service fabric重装电脑后集群失败
RunAsPolicy Exit Code 1替代
.core 学习文档
NServiceBus SAGA 消息状态驱动
NServiceBus消息重播
Copyright © 2011-2022 走看看