C# 二叉查找树实现
BuildTree 代码1次CODE完,没有BUG.
在画图地方debug了很多次.第一次画这种图.
一开始用treeview显示,但发现不是很好看出树结构,于是自己动手画了出来.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 二叉查找树
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); //BuildTreeView(topNode , treeView1.Nodes);
treeView1.Visible = false;
}
BNode<int> topNode;
private void Form1_Click(object sender, EventArgs e)
{
var lastNode = RandomGenBSPTree();
topNode = lastNode.GetTopNode();
//r1 += 0.1f;
DrawTree(topNode, ); maxLevel = dicLevel.Count;
Bitmap b = new Bitmap((marginX + blockWidth) * maxLevel, (marginY + blockHeight) * maxLevel);
var g = Graphics.FromImage(b);
g.Clear(Color.Black);
DrawTree2(topNode, g, b.Width / - blockWidth / , ,false,true);
DrawTree3(topNode, g);
int minI=,maxI = ;
for (int i = ; i < b.Width; i++)
{
for (int y = ; y < b.Height; y++)
{
if (b.GetPixel(i, y).R != || b.GetPixel(i, y).G != || b.GetPixel(i, y).B != )
goto QUITMAXI;
}
minI = i;
}
QUITMAXI:
for (int i = ; i < b.Width-minI; i++)
{
int id=b.Width - - i;
int ct = ;
for (int y = ; y < b.Height; y++)
{
if (b.GetPixel(id, y).R != || b.GetPixel(id, y).G != || b.GetPixel(id, y).B != )
ct++;
if(ct>)
goto QUITMIN;
}
maxI = id;
}
QUITMIN:
Bitmap bmpOut = new Bitmap(maxI-minI, b.Height);
Graphics g2 = Graphics.FromImage(bmpOut);
g2.DrawImage(b, new Rectangle(, , bmpOut.Width, bmpOut.Height), new Rectangle(minI-, , maxI, b.Height), GraphicsUnit.Pixel); this.BackgroundImage = bmpOut;
} void BuildTreeView(BNode<int> node,TreeNodeCollection tn)
{
if (node == null) return;
var newNode= tn.Add(node.value.ToString());
BuildTreeView(node.left, newNode.Nodes);
BuildTreeView(node.right, newNode.Nodes);
}
int blockWidth = ;
int minBlockWidth = ;
int blockHeight = ;
int marginX = ;
int marginY = ;
int maxLevel = ;
Font font = new Font("宋体",,FontStyle.Bold);
Brush brush = new SolidBrush(Color.White);
Dictionary<int, List<BNode<int>>> dicLevel = new Dictionary<int, List<BNode<int>>>();
void DrawTree(BNode<int> node,int level)
{
if (node == null) return;
if (dicLevel.ContainsKey(level) == false)
dicLevel.Add(level,new List<BNode<int>>());
dicLevel[level].Add(node);
node.level = level;
DrawTree(node.left,level+);
DrawTree(node.right, level + );
} void DrawTree2(BNode<int> node , Graphics g,int x,int y , bool isLeft=false,bool isRoot=false)
{
if (node == null) return; if (isRoot){
g.DrawString(node.value.ToString(), font, brush, x, y);
node.pos = new Point(x, y);
}
else
{ Text = r1.ToString();
var rate = (int)(blockWidth - r1*Math.Pow(node.level,) * marginX);
rate = rate < minBlockWidth ? minBlockWidth : rate;
node.pos = new Point(x + (isLeft ? - : ) * (rate), y + blockHeight + marginY);
g.DrawString(node.value.ToString(), font, brush, node.pos.X, node.pos.Y);
} DrawTree2(node.left, g, node.pos.X, node.pos.Y, true);
DrawTree2(node.right, g, node.pos.X, node.pos.Y, false);
}
float r1=1f;
void DrawTree3(BNode<int> node, Graphics g)
{
if (node == null) return;
if (node.left != null)
g.DrawLine( new Pen(Color.Green,2.1f),node.pos,node.left.pos);
if (node.right != null)
g.DrawLine(new Pen(Color.Green, 2.1f), node.pos, node.right.pos);
DrawTree3(node.left , g);
DrawTree3(node.right, g);
} BNode<int> RandomGenBSPTree(int Count)
{
var r = new Random();
List<int> pool = new List<int>(); BNode<int> curNode = new BNode<int>();
curNode.value = ;
pool.Add(curNode.value);
for (int i = ; i < Count; i++)
{
do
{
var newValue = r.Next(, );
if (pool.Contains(newValue) == false)
{
pool.Add(newValue);
break;
} }while(true); curNode.Insert(pool[pool.Count-]);
}
return curNode;
} class BNode<T>where T:IComparable
{
public BNode<T> left;
public BNode<T> right;
public BNode<T> parent;
public T value;
public int level;
public string text;
public Point pos;
public void Insert(T v)
{
var firstCompare=v.CompareTo( value );
BNode<T> nextCompare=firstCompare<?left:right; if (nextCompare != null)
{
nextCompare.Insert(v);
}
else
{
if (firstCompare < )
left = new BNode<T> { parent=this, value=v };
else
right = new BNode<T> { parent = this, value = v };
}
} public BNode<T> GetTopNode()
{
if (parent != null)
return parent.GetTopNode();
else
return this;
}
} }
}
C# 二叉查找树实现的更多相关文章
- 数据结构:二叉查找树(C语言实现)
数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...
- 数据结构笔记--二叉查找树概述以及java代码实现
一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- 平衡二叉查找树(AVL)的理解与实现
AVL树的介绍 平衡二叉树,又称AVL(Adelson-Velskii和Landis)树,是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它必须保证树的深度是 O(log N).一棵AVL ...
- 二叉查找树 C++实现(含完整代码)
一般二叉树的查找是通过遍历整棵二叉树实现,效率较低.二叉查找树是一种特殊的二叉树,可以提高查找的效率.二叉查找树又称为二叉排序树或二叉搜索树. 二叉查找树的定义 二叉排序树(Binary Search ...
- 数据结构——二叉查找树、AVL树
二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...
- Java for LintCode 验证二叉查找树
给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. ...
- 数据结构和算法 – 9.二叉树和二叉查找树
9.1.树的定义 9.2.二叉树 人们把每个节点最多拥有不超过两个子节点的树定义为二叉树.由于限制子节点的数量为 2,人们可以为插入数据.删除数据.以及在二叉树中查找数据编写有效的程序了. 在 ...
- 二叉树-二叉查找树-AVL树-遍历
一.二叉树 定义:每个节点都不能有多于两个的儿子的树. 二叉树节点声明: struct treeNode { elementType element; treeNode * left; treeNod ...
- 二叉查找树的Java实现
为了克服对树结构编程的恐惧感,决心自己实现一遍二叉查找树,以便掌握关于树结构编程的一些技巧和方法.以下是基本思路: [1] 关于容器与封装.封装,是一种非常重要的系统设计思想:无论是面向过程的函数,还 ...
随机推荐
- js 作用域,变量提升
先看下面一段代码: 代码执行的结果是: 1st alert : a = 0 2nd alert : a = undefined 5th alert : a = 0 3rd alert : a = 3 ...
- 使用Python实现Hadoop MapReduce程序
转自:使用Python实现Hadoop MapReduce程序 英文原文:Writing an Hadoop MapReduce Program in Python 根据上面两篇文章,下面是我在自己的 ...
- Alice's Chance
poj1698:http://poj.org/problem?id=1698 题意:爱丽丝要拍电影,有n部电影,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影 ...
- 【UVA11478】Halum (最短路解差分约束)
题目: Sample Input2 11 2 102 11 2 -103 31 2 42 3 23 1 54 52 3 44 2 53 4 23 1 01 2 -1Sample OutputInfin ...
- SysErrorMessage 函数和系统错误信息表
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示.但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢?FormatMessa ...
- 搜索提示時jquery的focusout和click事件沖突問題完美解决
在主流的搜索引擎上搜索時,輸入內容,往往會彈出智能提示.輸入框为input,智能提示區域为suggest.接下來一般有兩種操作: 1.選擇某一提示,則把內容复制到input中 ...
- 【转】linux(Ubuntu)配置svn仓库,搭建svn服务器
原文网址:http://blog.1v2d.com/322.html 在家里搞了好久,终于搞出来,并且在线上已经成功搭建成功,在这感谢一个博主的文章,本篇文章也主要是转载他的内容,写的非常好,而且非常 ...
- A Dicey Problem 骰子难题(Uva 810)
题目描述:https://uva.onlinejudge.org/external/8/810.pdf 把一个骰子放在一个M x N的地图上,让他按照规定滚动,求滚回原点的最短路径. 思路: 记忆化 ...
- Mysql性能优化那些事
对于全栈而言,数据库技能不可或缺,关系型数据库或者nosql,内存型数据库或者偏磁盘存储的数据库,对象存储的数据库或者图数据库--林林总总,但是第一必备技能还应该是MySQL.从LAMP的 ...
- python 开发简单的聊天工具
python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...