二叉树查找(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] 七大查找算法
查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...
随机推荐
- CentOS 7 BIND 主从搭建
主机 10.2.0.15 从机 10.2.0.14 1 主机配置$vim /etc/named.bodani.com.zones zone"bodani.com" IN { typ ...
- 杂音 & pop 音的解决方法
杂音 & pop 音的解决方法 1. 喇叭有严重的"吱吱"破音,绝大多数的原因有可能在于V(out)电压不稳定,所以最好测一下无负载时的输出电压.同时也可以测量 VCC – ...
- Java 反射实例
实体类:Userpackage com.reflect.model; public class User{ private User(int id, String username, String p ...
- ubuntu setup adb tool
sudo add-apt-repository ppa:nilarimogard/webupd8sudo apt-get updatesudo apt-get install android-tool ...
- KVM 基本硬件容量扩容
在工作当中如果虚拟机的容量不够使用 如何添加呢? CPU添加 cpu添加有两种方式: 1 创建虚拟机的时候可以添加 # virt-install --help | grep cpu --vcpus=V ...
- java模式:模板模式的简单理解
1.模板模式就是用虚类作为基类将几个要执行差不多操作中相同的部分提取出来,不同的部分各自实现! 2.下面给出简单栗子: 我要进行的操作是将大象和狐狸放入冰箱,放入大象和狐狸有相同的步骤:开冰箱和关冰箱 ...
- 【iOS 】UIView 中有一个autoresizingMask的属性
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- UITabBarItem's appearance
1.我们知道,用tabBarController创建出来默认的tabBar似这个样子滴... -----------------我是图片分割线----------------------------- ...
- 第三方 XListview 上拉加载、下拉刷新、分页加载和Gson解析
注意:此Demo用的是第三方的Xlistview.jar,需要复制me文件夹到项目中,两个XML布局文件和一张图片 把下面的复制到String中 <string name="xlist ...
- android 代码动态创建视图
LinearLayout 如何动态设置 margin? LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayou ...