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] 关于容器与封装.封装,是一种非常重要的系统设计思想:无论是面向过程的函数,还 ...
随机推荐
- hOAuth2.0认证和授权原理
原文地址: http://www.6zou.net/tech/what_is_oauth.html http://www.phpddt.com/%E4%BA%8C%E6%AC%A1%E5%BC%80% ...
- group by 替代distinct
SQL> select distinct employee_id,first_name from test1; 107 rows selected. Execution Plan ------- ...
- (转载)mysql group by 用法解析(详细)
(转载)http://blog.tianya.cn/blogger/post_read.asp?BlogID=4221189&PostID=47881614 mysql distinct 去重 ...
- (转载)mysql_query( )返回值
(转载)http://hi.baidu.com/tfbzccqceabfhyd/item/bd01db9f8995204af04215e4 调用mysql_query( ),当查询操作是update. ...
- Unity3d 游戏中的实时降噪-对Square Enix文档的研究与实现
看到SE的技术文档关于降噪的决定研究一下,本次试验场景: 文章中提到了3中主要滤波方法,最后一种方法又有三种方式分别为Conventional geometry-aware filtering,Dist ...
- FBReader移植日记 第二天
昨天我们的移植工作进行了一大半,还留下两个重要的部分没有完成:1.没有移植的 ZLTextView,2.FormatPlugin相关的类. 第一个问题我们放在后面解决,下面先解决格式插件的问题. 我们 ...
- typedef用法小结
typedef用法小结- - 注意:本文转自网络,版权归原作者所有. typedef typedef用法小结- - 这两天在看程序的时候,发现很多地方都用到typedef,在结构体定义,还有一些数组等 ...
- 【原】Centos6.5下cdh4.6 hive安装部署
1.前提条件: 只需要选择一台服务器即可,这里选择安装在namenode上:安装用户为cloud-user 2.安装包: sudo yum install -y hive hive ...
- 【转】SVN linux命令及 windows相关操作(三)
TortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理. TortoiseS ...
- android ROM备份和还原,再也不用当心刷到垃圾ROM,而还原不了原有系统
安卓刷机后如何还原以前ROM 和系统备份,本教程为大家介绍刷机后如何还原到以前的ROM 和系统备份. 很多人,看到了好多新的rom,包括测试版的新rom,心里痒痒的.想刷一刷.尝尝鲜,结果刷完,感觉新 ...