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. junit高级篇(参数化、打包测试)-实例代码

    工程目录: 参数化测试,SquareTest.java: import static org.junit.Assert.*; import java.util.Arrays; import java. ...

  2. PDF 补丁丁 0.4.2.1023 测试版使用手册发布

    由于 PDF 补丁丁新版本增加的功能颇多,界面也重新设计过,因此,使用手册需要作比较大幅度的改动才能与软件相匹配. 目前,使用手册已经撰写了一部分,但新增功能的介绍和新界面的截图还没完成,有2/3的内 ...

  3. windows下安装mysql解压版

    1.解压压缩版的MySQL 其中: bin目录        -    主要存放MySQL的各种可执行程序 data目录      -   存放数据库的数据文件和索引文件等 MySQL-test -  ...

  4. css2----清除浮动

    为什么要清除浮动? 非IE下,当容器的高度为auto,容器有浮动元素,此时容器的高度不能自己伸长适应内容的高度,造成内容溢出乃至影响布局,即所谓的“浮动溢出”,为防此象,需要清除浮动. 如何清除浮动? ...

  5. pads 扇出

    1 选择BGA器件 2.扇出设置 3 安全间距问题 4 可以区域扇出也可以多点扇出

  6. TopCoder----卖柠檬

    1. 题目描述 Problem Statement   You are playing a game called Slime Tycoon.You will be selling Slimonade ...

  7. My first Scratch small game

    My first Scratch small game:https://scratch.mit.edu/projects/62700370/ PC or Mac only. Browser & ...

  8. Interproscan, xml文件转化为tsv

    将interproscan的结果转化格式 很奇怪 tsv格式里没有go, kegg, inter-domain信息,但是xml文件里面却有,tsv文件比较好处理,所以先将xml文件转化为tsv.用软件 ...

  9. 9.9,新iPhone要来了,是欢呼,还是墙角画圈,一会儿见分晓

  10. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...