二叉树查找(C#)
参考文章:
http://www.cnblogs.com/huangxincheng/archive/2012/07/21/2602375.html
http://www.cnblogs.com/xiashengwang/archive/2013/03/04/2942555.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections; namespace ConsoleApplication2
{
public class Program
{
public static void Main()
{
BinaryTree<int, Student> binaryTree = new BinaryTree<int, Student>();
binaryTree.Add(, new Student() { Age = , Name = "", Sex = false });//这个是用Age作为key
binaryTree.Add(, new Student() { Age = , Name = "", Sex = false });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = false });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = false });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = false });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "", Sex = true });
binaryTree.Add(, new Student() { Age = , Name = "38@", Sex = true });//注意哦 这里故意又出现个38
binaryTree.Add(, new Student() { Age = , Name = "", Sex = false }); Console.WriteLine("层排序遍历:");
LevelOrder(binaryTree.RootNote);
Console.WriteLine(); Console.WriteLine("是否含有指定元素");
Console.WriteLine(binaryTree.Contain());
Console.WriteLine(); Console.WriteLine("最小元素");
Console.WriteLine(binaryTree.FindMin().TreeKey);
Console.WriteLine(); Console.WriteLine("最大元素");
Console.WriteLine(binaryTree.FindMax().TreeKey);
Console.WriteLine(); Console.WriteLine("范围查找");
foreach (var item in binaryTree.SearchRange(, ))
{
Console.WriteLine(item.Age);
}
Console.WriteLine(); Console.WriteLine("删除指定元素");
binaryTree.Remove(, new Student() { Age = , Name = "", Sex = false });
Console.WriteLine(); Console.WriteLine("层排序遍历:");
LevelOrder(binaryTree.RootNote);
Console.WriteLine(); Console.WriteLine("前序遍历:");
PreOrder(binaryTree.RootNote);
Console.WriteLine(); Console.WriteLine("中序遍历:");
InOrder(binaryTree.RootNote);
Console.WriteLine(); Console.WriteLine("后序遍历:");
PostOrder(binaryTree.RootNote);
Console.WriteLine(); Console.Read();
} private static void PreOrder(TreeNote<int, Student> treeNote)
{
foreach (var item in treeNote.TreeValues)
{
Console.Write(item.Name + " ");
} if (treeNote.LeftTreeNote != null)
{
PreOrder(treeNote.LeftTreeNote);
} if (treeNote.RightTreeNote != null)
{
PreOrder(treeNote.RightTreeNote);
}
} private static void InOrder(TreeNote<int, Student> treeNote)
{
if (treeNote.LeftTreeNote != null)
{
InOrder(treeNote.LeftTreeNote);
} foreach (var item in treeNote.TreeValues)
{
Console.Write(item.Name + " ");
} if (treeNote.RightTreeNote != null)
{
InOrder(treeNote.RightTreeNote);
}
} private static void PostOrder(TreeNote<int, Student> treeNote)
{
if (treeNote.LeftTreeNote != null)
{
PostOrder(treeNote.LeftTreeNote);
} if (treeNote.RightTreeNote != null)
{
PostOrder(treeNote.RightTreeNote);
} foreach (var item in treeNote.TreeValues)
{
Console.Write(item.Name + " ");
}
} private static void LevelOrder(TreeNote<int, Student> treeNote)
{
Queue queue = new Queue();
queue.Enqueue(treeNote);
while (queue.Count > )
{
var treeNoteTemp = (TreeNote<int, Student>)queue.Dequeue(); foreach (var item in treeNoteTemp.TreeValues)
{
Console.Write(item.Name + " ");
} if (treeNoteTemp.LeftTreeNote != null)
{
queue.Enqueue(treeNoteTemp.LeftTreeNote);
} if (treeNoteTemp.RightTreeNote != null)
{
queue.Enqueue(treeNoteTemp.RightTreeNote);
}
}
}
} public class Student : IEquatable<Student>//由于后面代码的treeNote.TreeValues.Remove(treeValue); 因为HashCode不同无法删除 这里需要重写GetHashCode()
{
public string Name { get; set; }
public bool Sex { get; set; }
public int Age { get; set; } public bool Equals(Student other)
{
return this.Name == other.Name &&
this.Sex == other.Sex &&
this.Age == other.Age;
}
public override bool Equals(object obj)
{
if (obj == null) return base.Equals(obj); if (obj is Student)
return Equals(obj as Student);
else
throw new InvalidCastException("the 'obj' Argument is not a Student object");
}
public override int GetHashCode()
{
return Name.GetHashCode()^Sex.GetHashCode()^Age.GetHashCode();//return object's hashcode
}
} public class TreeNote<TKey, TValue> where TKey : IComparable
{
public TreeNote(TKey treeKey, TValue treeValue)
{
TreeKey = treeKey;
TreeValues = new HashSet<TValue>();
TreeValues.Add(treeValue);
} public TKey TreeKey { get; set; }
public HashSet<TValue> TreeValues { get; set; }
public TreeNote<TKey, TValue> LeftTreeNote { get; set; }
public TreeNote<TKey, TValue> RightTreeNote { get; set; }
} public class BinaryTree<TKey, TValue> where TKey : IComparable
{
public TreeNote<TKey, TValue> RootNote = null;//根节点 public void Add(TKey treeKey, TValue treeValue)
{
RootNote = Add(treeKey, treeValue, RootNote);
} private TreeNote<TKey, TValue> Add(TKey treeKey, TValue treeValue, TreeNote<TKey, TValue> treeNote)
{
if (treeNote == null)
{
return new TreeNote<TKey, TValue>(treeKey, treeValue);
} if (treeNote.TreeKey.CompareTo(treeKey) > )
{
treeNote.LeftTreeNote = Add(treeKey, treeValue, treeNote.LeftTreeNote);
} if (treeNote.TreeKey.CompareTo(treeKey) < )
{
treeNote.RightTreeNote = Add(treeKey, treeValue, treeNote.RightTreeNote);
} if (treeNote.TreeKey.CompareTo(treeKey) == )
{
treeNote.TreeValues.Add(treeValue);
} return treeNote;
} public bool Contain(TKey treeKey)
{
return Contain(treeKey, RootNote);
} private bool Contain(TKey treeKey, TreeNote<TKey, TValue> treeNote)
{
if (treeNote == null)
{
return false;
} if (treeNote.TreeKey.CompareTo(treeKey) > )
{
return Contain(treeKey, treeNote.LeftTreeNote);
} if (treeNote.TreeKey.CompareTo(treeKey) < )
{
return Contain(treeKey, treeNote.RightTreeNote);
} return treeNote.TreeKey.CompareTo(treeKey) == ;
} public HashSet<TValue> SearchRange(TKey minTreeKey, TKey maxTreeKey)
{
return SearchRange(minTreeKey, maxTreeKey, RootNote, new HashSet<TValue>());
} private HashSet<TValue> SearchRange(TKey minTreeKey, TKey maxTreeKey, TreeNote<TKey, TValue> treeNote, HashSet<TValue> attach)
{
if (treeNote == null)
{
return attach;
} if (treeNote.TreeKey.CompareTo(minTreeKey) > )
{
SearchRange(minTreeKey, maxTreeKey, treeNote.LeftTreeNote, attach);
} if (treeNote.TreeKey.CompareTo(minTreeKey) >= && treeNote.TreeKey.CompareTo(maxTreeKey) <= )
{
foreach (var item in treeNote.TreeValues)
{
attach.Add(item);
}
} if (treeNote.TreeKey.CompareTo(maxTreeKey) < )
{
SearchRange(minTreeKey, maxTreeKey, treeNote.RightTreeNote, attach);
} return attach;
} public TreeNote<TKey, TValue> FindMin()
{
return FindMin(RootNote);
} private TreeNote<TKey, TValue> FindMin(TreeNote<TKey, TValue> treeNote)
{
if (treeNote == null)
{
return null;
} if (treeNote.LeftTreeNote == null)
{
return treeNote;
}
else
{
return FindMin(treeNote.LeftTreeNote);
}
} public TreeNote<TKey, TValue> FindMax()
{
return FindMax(RootNote);
} private TreeNote<TKey, TValue> FindMax(TreeNote<TKey, TValue> treeNote)
{
if (treeNote == null)
{
return null;
} if (treeNote.RightTreeNote == null)
{
return treeNote;
}
else
{
return FindMax(treeNote.RightTreeNote);
}
} public void Remove(TKey treeKey, TValue treeValue)
{
Remove(treeKey, treeValue, RootNote, true);
} private TreeNote<TKey, TValue> Remove(TKey treeKey, TValue treeValue, TreeNote<TKey, TValue> treeNote, bool isAccordingToTreeValues)
{
if (treeNote == null)
{
return null;
} if (treeNote.TreeKey.CompareTo(treeKey) > )
{
treeNote.LeftTreeNote = Remove(treeKey, treeValue, treeNote.LeftTreeNote, isAccordingToTreeValues);
} if (treeNote.TreeKey.CompareTo(treeKey) < )
{
treeNote.RightTreeNote = Remove(treeKey, treeValue, treeNote.RightTreeNote, isAccordingToTreeValues);
} if (treeNote.TreeKey.CompareTo(treeKey) == )
{
if (treeNote.TreeValues.Count > && isAccordingToTreeValues)
{
treeNote.TreeValues.Remove(treeValue);
}
else
{
if (treeNote.LeftTreeNote == null || treeNote.RightTreeNote == null)
{
treeNote = treeNote.LeftTreeNote == null ? treeNote.RightTreeNote : treeNote.LeftTreeNote;
}
else //有一对子元素
{
var treeNoteTemp = FindMin(treeNote.RightTreeNote);//右子节点下的最小子节点替换当前节点
treeNote.TreeKey = treeNoteTemp.TreeKey;
treeNote.TreeValues = treeNoteTemp.TreeValues; treeNote.RightTreeNote = Remove(treeNoteTemp.TreeKey, treeValue, treeNote.RightTreeNote, false);
}
}
}
return treeNote;
}
}
}
二叉树查找(C#)的更多相关文章
- 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)
查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...
- java实现二叉树查找树
二叉树(binary)是一种特殊的树.二叉树的每个节点最多只能有2个子节点: 二叉树 由于二叉树的子节点数目确定,所以可以直接采用上图方式在内存中实现.每个节点有一个左子节点(left childre ...
- 动态查找之二叉树查找 c++实现
算法思想 二叉搜索树(又称二叉查找树或二叉排序树)BST树 二叉查找树 二叉查找树,也称二叉搜索树,或二叉排序树.其定义也比较简单,要么是一颗空树,要么就是具有如下性质的二叉树: (1)若任意节点的左 ...
- 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree
[抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...
- 数据结构与算法(c++)——查找二叉树与中序遍历
查找树ADT--查找二叉树 定义:对于树中的每个节点X,它的左子树中的所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项. 现在给出字段和方法定义(BinarySearchTree.h) # ...
- [javaSE] 数据结构(二叉树-遍历与查找)
前序遍历:中,左,右 中序遍历:左,中,右 后序遍历:左,右,中 二叉树查找 从根节点进行比较,目标比根节点小,指针移动到左边 从根节点进行比较,目标比根节点大,指针移动到右边 /** * 前序遍历 ...
- 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较
二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』 二分法的存储要求:要求顺序存储,以便于根据下标随机访问 二分法的时间效率:O(Log(n)) 二分 ...
- javascript数据结构与算法-- 二叉树
javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...
- [Data Structure & Algorithm] 七大查找算法
查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...
随机推荐
- web.xml文件中的7个错误的安全配置
web.xml文件中的7个错误的安全配置 关于Java的web.xml文件中配置认证和授权有大 量 的 文章.本文不再去重新讲解如何配置角色.保护web资源和设置不同类型的认证,让我们来看看web.x ...
- ng-option小解
ng-option 有数组,对象两种情况,但目前只接触过数组 数组: label for value in array 分为一般数组和对象数组 一般数组: <select ng-model=&q ...
- java跳过构造方法新建对象
Java有四种创建对象的方法: (1) 用new语句创建对象,这是最常见的创建对象的方法. (2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Constr ...
- WPF(x:Null 使用)
<Window x:Class="TestOfNull.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- java synchronized 线程同步机制详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...
- PHP上传图片三个步骤详细分析
学习PHP时,你可能会遇到PHP上传图片问题,这里将介绍PHP上传图片问题的解决方法,在这里拿出来和大家分享一下.今天我们就开始一起学习PHP上传图片.上传图片原理:首先判断文件类型是否为图片格式,若 ...
- 4位开锁<dfs>
题意: 有一个四位密码的锁,每一位是1~9的密码,1跟9相连.并且相邻的连个密码位可以交换.每改变一位耗时1s,给出锁的当前状态和密码,求最少解锁时间. 思路: 用bfs枚举出所有相邻交换的情况,并记 ...
- 转:LoadRunner中参数化技术详解
LoadRunner中参数化技术详解 LoadRunner在录制脚本的时候,只是忠实的记录了所有从客户端发送到服务器的数据,而在进行性能测试的时候,为了更接近真实的模拟现实应用,对于某些信息需要每次提 ...
- LINUX中磁盘挂载与卸除
一.挂载格式与参数说明: 要将文件系统挂载到我们的 Linux 系统上,就要使用 mount 这个命令啦! 不过,这个命令真的是博大精深-粉难啦!我们学简单一点啊- ^_^ [root@www ~]# ...
- Oracle数据库设计小细节
1. 如果使用PowerDesigner此类工具,注意将工具的导出的SQL语句中对于表的双引号去掉. 2. 建表和建字段的时候,不同单词之间使用下划线分隔,比如 REC_ID 3. Oracle中数值 ...