using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public class nodes<T>
{
T data;
nodes<T> Lnode, Rnode, Pnode; public T Data //中
{
set { data = value; }
get { return data; }
} public nodes<T> LNode //左
{
get { return Lnode; }
set { Lnode = value; }
}
public nodes<T> RNode //右
{
set { Rnode = value; }
get { return Rnode; }
} public nodes<T> PNode
{
set { Pnode = value; }
get { return Pnode; }
}
public nodes() { } public nodes(T data)
{
this.data = data;
} //先序遍历
public static void PreOrder<T>(nodes<T> rootNode)
{
if (rootNode != null)
{
Console.WriteLine(rootNode.Data);
PreOrder<T>(rootNode.LNode);
PreOrder<T>(rootNode.RNode);
}
}
//中序遍历二叉树
public static void MidOrder<T>(nodes<T> rootNode)
{
if (rootNode != null)
{
MidOrder<T>(rootNode.LNode);
Console.WriteLine(rootNode.Data);
MidOrder<T>(rootNode.RNode);
}
} //后续遍历二叉树
public static void AfterOrder<T>(nodes<T> rootNode)
{
if (rootNode != null)
{
AfterOrder<T>(rootNode.LNode);
AfterOrder<T>(rootNode.RNode);
Console.WriteLine(rootNode.Data);
}
}
//层次遍历
public static void LayerOrder<T>(nodes<T> rootNode)
{
nodes<T>[] Nodes = new nodes<T>[];
int front = -; //前
int rear = -; //后
if (rootNode != null)
{
rear++;
Nodes[rear] = rootNode;
}
while (front != rear)
{
front++;
rootNode = Nodes[front];
Console.WriteLine(rootNode.Data);
if (rootNode.LNode != null)
{
rear++;
Nodes[rear] = rootNode.LNode;
}
if (rootNode.RNode != null)
{
rear++;
Nodes[rear] = rootNode.RNode;
}
}
} //构造一颗已知的二叉树
public static nodes<string> BinTree()
{
nodes<string>[] binTree = new nodes<string>[]; //创建结点
binTree[] = new nodes<string>("A");
binTree[] = new nodes<string>("B");
binTree[] = new nodes<string>("C");
binTree[] = new nodes<string>("D");
binTree[] = new nodes<string>("E");
binTree[] = new nodes<string>("F");
binTree[] = new nodes<string>("G");
binTree[] = new nodes<string>("H"); //使用层次遍历二叉树的思想,构造一个已知的二叉树
binTree[].LNode = binTree[];
binTree[].RNode = binTree[];
binTree[].RNode = binTree[];
binTree[].LNode = binTree[];
binTree[].RNode = binTree[];
binTree[].LNode = binTree[];
binTree[].RNode = binTree[]; //返回二叉树的根结点
return binTree[];
} }
static void Main(string[] args)
{
nodes<string> rootNode = nodes<string>.BinTree(); Console.WriteLine("先序遍历二叉树");
nodes<string>.PreOrder(rootNode);
Console.WriteLine("中序遍历二叉树");
nodes<string>.MidOrder(rootNode);
Console.WriteLine("后序遍历二叉树");
nodes<string>.AfterOrder(rootNode);
Console.WriteLine("层次遍历二叉树");
nodes<string>.LayerOrder(rootNode);
Console.Read();
} }
}

C#二叉树简易实例的更多相关文章

  1. 利用python检测色情图片简易实例

    import sys import os import _io from collections import namedtuple from PIL import Image class Nude( ...

  2. JAVA实现二叉树(简易版--实现了二叉树的各种遍历)

    1,个人感觉二叉树的实现主要还是如何构造一颗二叉树.构造二叉树函数的设计方法多种多样,本例采用 addNode 方法实现.以下程序通过定义内部类来表示二叉树的结点,然后再实现了二叉树这种数据结构的一些 ...

  3. python测试rabbitmq简易实例

    生产者 import pika #coding=utf8 credentials = pika.PlainCredentials('guest', '密码') connection = pika.Bl ...

  4. Vue组件实例间的直接访问

    前面的话 有时候需要父组件访问子组件,子组件访问父组件,或者是子组件访问根组件. 在组件实例中,Vue提供了相应的属性,包括$parent.$children.$refs和$root,这些属性都挂载在 ...

  5. 数据结构(DataStructure)与算法(Algorithm)、STL应用

    catalogue . 引论 . 数据结构的概念 . 逻辑结构实例 2.1 堆栈 2.2 队列 2.3 树形结构 二叉树 . 物理结构实例 3.1 链表 单向线性链表 单向循环链表 双向线性链表 双向 ...

  6. C语言范例学习03-下

    树与图 3.5 二叉树及其应用 PS:二叉树是最经典的树形结构,适合计算机处理,具有存储方便和操作灵活等特点,而且任何树都可以转换成二叉树. 实例101 二叉树的递归创建 实例102 二叉树的遍历 问 ...

  7. php class类的用法详细总结

    以下是对php中class类的用法进行了详细的总结介绍,需要的朋友可以过来参考下 一:结构和调用(实例化): class className{} ,调用:$obj = new className(); ...

  8. 使用React Native来撰写跨平台的App

    React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...

  9. canvas粒子系统的构建

    前面的话 本文将从最基本的imageData对象的理论知识说开去,详细介绍canvas粒子系统的构建 效果演示 下面是实例效果演示,博文结尾有全部源码 imageData 关于图像数据imageDat ...

随机推荐

  1. cocos lua 加密与解密 混淆 (版本号cocos3.4)

    cocos luacompile cocos luacompile Overview Usage Available Arguments Samples Overview Compile the .l ...

  2. Ipad也怕冷?!

    今天,说一Ipad充不了电,我想才没买好久,这么快电池就坏了呀.难道买到歪货了? 它的表现是充电线一接上去,电池指示有反应,也有"闪电"标志,就是充不进去电.本来想打客服的,还是先 ...

  3. Xcode插件,模板安装

    一:是使用工具安装 A Package Manager for Xcode -Xcode模板管理工具Alcatraz使用 二:手动安装 1:打开资源库: 打开Finder---按住alt----前往- ...

  4. Windows下安装配置SBT

    1:安装包下载界面 http://www.scala-sbt.org/download.html 下载后进行安装. 安装路径:D:\Java\sbt\conf 2:进行配置 (1)sbtconfig. ...

  5. Spring <context:annotation-config/> 说明

    在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册AutowiredAn ...

  6. plsql 常用快捷键(自动替换)

      plsql 常用快捷键 CreateTime--2018年4月23日17:33:05 Author:Marydon 说明:这里的快捷键,不同于以往的快捷键,输入指定字符,按快捷键,可以自动替换成你 ...

  7. Google Hack的一些整理

    这里是一些关于Google Hack方面的整理 黑客专用信息和资料搜索地址为: http://www.google.com/custom?hl=xx-hacker 这里是google关键字的用法,要设 ...

  8. Linux安装Nginx1.7.4、php5.5.15和配置

    Nginx是一个轻量级的高性能Webserver.反向代理server.邮件(IMAP/POP3/SMTP)server,是Igor Sysoev为俄罗斯訪问量第二的Rambler.ru网站开发,第一 ...

  9. coding云(git)远程创建版本库和上传文件

    1.创建项目不讲,注意勾选 README选项 2.本地需要首先安装 windows 的git库,https://gitforwindows.org/ 3.进入www目录下,直接将coding云上的项目 ...

  10. 转载【微信小程序】:微信小程序滚动Tab选项卡:左右可滑动切换(仿某宝)

    1.本文转载至:http://blog.csdn.net/sophie_u/article/details/71745125 2.效果: 3.最终效果如上.问题: 1).tab标题总共8个,所以一屏无 ...