A binary tree is defined as a tree where each node can have no more than two children.

Building a Binary Search Tree:

首先创建一个节点Class

    public class BtNode
{
public int Data { get; set; } public BtNode Left { get; set; } public BtNode Right { get; set; } public void DisplayNode()
{
Console.Write("Data: {0}, ", this.Data);
}
}

然后创建BST Class 提供Insert 方法:

    public class BinarySearchTree
{
private BtNode root = null; public BtNode Root
{
get
{
return this.root;
}
set
{
this.root = value;
}
} public void Insert(int data)
{
BtNode newNode = new BtNode();
newNode.Data = data;
if (this.root == null)
{
this.root = newNode;
}
else
{
// Add to children nodes.
BtNode current = this.root;
BtNode parent = new BtNode(); while(true)
{
parent = current;
if (data < current.Data)
{
current = current.Left;
if (current == null )
{
parent.Left = newNode;
break;
} }
else
{
current = current.Right;
if (current == null)
{
parent.Right = newNode;
break;
}
}
}
}
}
}

Level traverse the binary tree.

思路就是用 Queue (FIFO),从左到右扫描节点,一个节点出队列,就将其节点的左右孩子入队。

        public static void PrintLevelTracversal(BtNode root)
{
if (root == null)
{
throw new Exception("Binary true is null!");
} Queue<BtNode> queueBtNode = new Queue<BtNode>();
queueBtNode.Enqueue(root);
BtNode currentNode = root; // Dequeue the node from left to right and put its left/right children into queue.
while(currentNode != null)
{
currentNode = queueBtNode.Dequeue();
currentNode.DisplayNode(); if (currentNode.Left != null)
{
queueBtNode.Enqueue(currentNode.Left);
} if (currentNode.Right != null)
{
queueBtNode.Enqueue(currentNode.Right);
}
}
}

Level traverse and print nodes as Zigzag.

思路: 用2个Stuck, 一个保存Current Level, 另一个保存Next Level.

当一层 扫完后,将下一层copy到当前层。

       // Non-Recursive method.  Use two stacks.
public static void PrintZigzagLevelTraversal(BtNode root)
{
if (root == null)
{
throw new Exception("Binary tree is null!");
} Stack<BtNode> currentLevelStack = new Stack<BtNode>();
Stack<BtNode> nextLevelStack = new Stack<BtNode>();
BtNode currentBtNode = new BtNode(); int level = ;
currentLevelStack.Push(root); while(currentLevelStack.Count > )
{
// Display current node data.
currentBtNode = currentLevelStack.Pop();
currentBtNode.DisplayNode(); if (level % == ) // even level
{
if (currentBtNode.Left != null)
{
nextLevelStack.Push(currentBtNode.Left);
}
if (currentBtNode.Right != null)
{
nextLevelStack.Push(currentBtNode.Right);
}
}
else
{
if (currentBtNode.Right != null)
{
nextLevelStack.Push(currentBtNode.Right);
}
if (currentBtNode.Left != null)
{
nextLevelStack.Push(currentBtNode.Left);
}
} // Move data from next level to current level stack.
if (nextLevelStack.Count > && currentLevelStack.Count == )
{
BtNode[] nodes = new BtNode[nextLevelStack.Count];
nextLevelStack.CopyTo(nodes, );
for (int i = nodes.Length - ; i >= ; i--)
{
currentLevelStack.Push(nodes[i]);
}
nextLevelStack.Clear();
level++;
}
}
}
 

(C# Binary Tree) 基本概念和算法的更多相关文章

  1. Binary Tree Preorder Traversal——经典算法的迭代求解(前序,中序,后序都在这里了)

    先序遍历,用递归来做,简单的不能再简单了.代码如下: (以下仅实现了先序遍历,中序遍历类似,后序遍历和这两个思路不一样,具体详见Binary Tree Postorder Traversal) /** ...

  2. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  5. 一道算法题目, 二行代码, Binary Tree

    June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 多问几个为什么, 可不可以简化.想一想, 二行代码, 五分钟就可以搞定; 2015年网上大家热议的 Home ...

  6. LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)

    这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...

  7. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

  8. LeetCode算法题-Construct String from Binary Tree(Java实现)

    这是悦乐书的第273次更新,第288篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第141题(顺位题号是606).构造一个字符串,该字符串由二叉树中的括号和整数组成,并具 ...

  9. Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder Traversal )——数据结构和算法的基本问题

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

随机推荐

  1. python学习------dictionary和set

    一.dictionary 1.字典的形式:a={‘key’:value,‘key1’:value,................} 2.字典的的key不能重复,是一个不可变对象 3.字典的的查找和添 ...

  2. Mac OSX 无法SSH远程的原因

    在mac中开启了远程共享,拿windows上的putty 死活无法ssh上去. 后来下载了个新版的putty, 连上了.

  3. jQuery键盘控制方法,以及键值(keycode)对照表

    键盘控制应用范围非常广泛,比如快捷键控制页面的滚动:在填写表单时候,限制输入内容:或者是屏蔽复制.粘贴.退后等功能.这里说说用jQuery比原生态的JS好用,代码简单清晰,不要问我JS怎么写,因为我不 ...

  4. 在 Ubuntu 14.04 中配置 PXE 服务器

    PXE(预启动执行环境Preboot Execution Environment)服务器允许用户从网络中启动 Linux 发行版并且可以不需要 Linux ISO 镜像就能同时在数百台 PC 中安装. ...

  5. SQL Server 2008中查看锁信息

    ;with tran_locks as(select resource_type,db_name(resource_database_id) as db_name,resource_descripti ...

  6. 用Firefox的debugger来调试JavaScript

    1.自我感觉比firebug更好用 https://developer.mozilla.org/zh-CN/docs/Tools/Debugger

  7. android布局学习之相对布局(RelativeLayout)

    移通152余继彪 RelativeLayout可以设置某一个视图相对于其他视图的位置,这些位置可以包括上下左右等 RelativeLayout    属性  说明 android:layout_bel ...

  8. MS Sql server 2008 学习笔记

    数据库中常用的概念 Sql本身是一个服务器,没有界面,Management Studio  只是一个SQL Server管理工具而已,不是服务器. Sql server 在管理工具下面的服务SQL S ...

  9. 立即调用的函数表达式IIFE

    1.写法 (function () { alert("IIFE");})();//或者(function () { alert("IIFE"); }());

  10. jQuery.ajax() 函数详解

    jQuery.ajax()函数用于通过后台HTTP请求加载远程数据. jQuery.ajax()函数是jQuery封装的AJAX技术实现,通过该函数,我们无需刷新当前页面即可获取远程服务器上的数据. ...