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. Singular value encountered in calculation for ROI

    在ENVI中对一幅TM影像进行监督分类,在进行compute ROI separability时提示Singular value encountered in calculation for ROI, ...

  2. STL - 容器 - vector简单应用

    VectorTest.cpp #include <vector> #include <iostream> #include <string> #include &l ...

  3. 4667 Building Fence 解题报告

    题意:给n个圆和m个三角形,且保证互不相交,用一个篱笆把他们围起来,求最短的周长是多少. 解法1:在每个圆上均匀的取2000个点,求凸包周长就可以水过. 解法2:求出所有圆之间的外公切线的切点,以及过 ...

  4. Struts2(二)action的三种方式

    一.普通java类 package com.pb.web.action; /* * 创建普通的java类 */ public class HelloAction1 { public String ex ...

  5. PHP 表单和用户输入

    PHP 的 $_GET 和 $_POST 用于检索表单中的值,比如用户输入. index.php页面 <?php  /*时间:2014-09-14  *作者:葛崇  *功能:表单传值小实例  * ...

  6. spring启动方式

    spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.看一下ContextLoaderListe ...

  7. 保护HTTP的安全

    #如果没有严格的限制访问的权限,公司放在服务器上的重要文档就存在隐患,web需要有一些安全的http形式: #安全方法: #基本认证.摘要认证.报文完整性检查都是一些轻量级的方法,但还不够强大,下面介 ...

  8. 工具-VIM配置

    设置缩进的空格数 shiftwidth=4 设置制表符宽度 tabstop=4 高亮显示当前行 cursorline 高亮显示当前列 cursorcolumn

  9. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  10. Tomcat JNDI + spring配置

    http://hi.baidu.com/lzpsky/item/f9a727ba823257eb4ec7fd27 一.简介 JNDI : Java Naming and Directory Inter ...