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();
}
}
}
查看全文
相关阅读:
Gin 使用 websocket
7天用Go从零实现Web框架Gee教程
docker-compose 搭建 Redis Sentinel 测试环境
关闭禁用 Redis 危险命令
Redis Cluster 设置密码
使用 twine 上传自己的 python 包到 pypi
Redis 5.0.7 讲解,单机、集群模式搭建
Redis 单机模式,主从模式,哨兵模式(sentinel),集群模式(cluster),第三方模式优缺点分析
django 重写 mysql 连接库实现连接池
编译安装httpd-2.4
原文地址:https://www.cnblogs.com/guoxiaowen/p/1262571.html
最新文章
redis集群搭建
mongodb集群故障转移实践
redis访问安全加固
mongodb高级聚合查询
apigateway-kong(七)配置说明
apigateway-kong(六)认证
centos7部署posgresql和kong总结
mysql常见问题解决
apigateway-kong(五)集群搭建部署
统计队列 重大生产事故 解决历程
热门文章
真假 主从延迟 +队列先后顺序执行问题
多线程+异步 遇到的异常:关键业务未执行前就结束了主线程
数据库设计规范
.netFramework 升级NetCore 问题汇总及解决方案
批量更新表注释 mysql
mysql group by 报错 ,only_full_group_by 三种解决方案
简述关系型数据库与非关系型数据库
IIS基本设置、回收机制、性能、并发、安全性
大话设计模式
go 面向对象编程方法
Copyright © 2011-2022 走看看