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();
}
}
}
查看全文
相关阅读:
ASP.NET Core API ---状态码
ASP.NET Core ---日志
UnitOfWork知多少 【转】
ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor 【转】
ASP.NET Core 2.1 源码学习之 Options[2]:IOptions 【转】
ASP.NET Core 2.1 源码学习之 Options[1]:Configure 【转】
深入理解net core中的依赖注入、Singleton、Scoped、Transient(四)【转】
sonarqube插件开发(三) 调试插件
sonarqube插件开发(二) 开发插件
sonarqube插件开发(一) 环境搭建
原文地址:https://www.cnblogs.com/guoxiaowen/p/1262571.html
最新文章
二叉树常见面试题(进阶)
二叉树及二叉树的基本操作(基础篇)
栈和队列的常见题型
C++单例模式
C/C++中static的用法全局变量与局部变量
TCP常见的定时器及三次握手与四次挥手
C/C++中const关键字的用法及其与宏常量的比较
gdb常用命令及gdb调试多进程/线程程序&coredump
Jquery-ui后台管理(2)
Jquery-UI后台管理(1)
热门文章
窗口
ASP.NET Web API中的依赖注入
解决Entity Framework查询匿名对象后的跨域访问的一种方式
WebSocket在ASP.NET MVC4中的简单实现
在ASP.NET MVC中使用Unity进行依赖注入的三种方式
Unity Container中的几种注册方式与示例
使用Unity创建依赖注入
ASP NET Core ---FluentValidation
ASP NET Core ---Automapper
ASP.NET Core ---异常处理
Copyright © 2011-2022 走看看