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进行数据分析》读书笔记--第五章 pandas入门
《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算
《利用python进行数据分析》读书笔记--第三章 ipython使用
《算法导论》读书笔记--第三章 函数的增长
《利用python进行数据分析》读书笔记 --第一、二章 准备与例子
《R语言实战》读书笔记--学习张丹日志
《R语言实战》读书笔记 第七章--基本统计分析
解决Mybatis-plus高版本不向后兼容的问题
【Javascript】: for循环中定义的变量在for循环体外也有效
原文地址:https://www.cnblogs.com/guoxiaowen/p/1262571.html
最新文章
csp-s72瓶颈记录
csp-s67总结
csp-s 65 Simple
csp-64问题总结
csp-s模拟62问题总结
坑
青云
csp-s模拟测试60问题总结
nginx如何配置网页错误页面
接口速度慢问题查找(TTFB时间长)
热门文章
Centos7 安装高版本php
centos7安装mariadb后无法启动的问题
mac生成ssh keys
硬链接和软链接的区别
用yum源安装Nginx
centos7如何安装pandoc
如何查看centos系统版本
json_encode和json_decode
《利用python进行数据分析》读书笔记--第七章 数据规整化:清理、转换、合并、重塑(一)
《利用python进行数据分析》读书笔记--第六章 数据加载、存储与文件格式
Copyright © 2011-2022 走看看