算法:非平衡二叉搜索树(UnBalanced Binary Search Tree)
背景
很多场景下都需要将元素存储到已排序的集合中。用数组来存储,搜索效率非常高: O(log n),但是插入效率比较低:O(n)。用链表来存储,插入效率和搜索效率都比较低:O(n)。如何能提供插入和搜索效率呢?这就是二叉搜索树的由来,本文先介绍非平衡二叉搜索树。
非平衡二叉搜索树
规则
所有节点的左节点小于节点,所有节点的右节点大于等于自身,即:node.value > node.left.value && node.value <= node.right.value。
示例
根据上面的规则,我们也很容易找到最大值和最小值,后面也会用到这种算法。最大值可以通过递归方法 node.right 得到,最小值可以递归 node.left 得到。
为什么叫非平衡?
说明:下图变成链表了,这会导致效率非常低,后面找机会再介绍平衡算法。
实现
搜索、遍历(前序、中序和后序)、添加算法都比较简单,可以直接看后面的代码,这里重点介绍一下删除算法。
如何删除元素?
第一步:要找到删除的元素(current)。
第二步:判断 current 满足如下哪种场景:
- current.Right == null
示例
代码if (parent == null)
{
this.Root = current.Left;
}
else if (isLeft)
{
parent.Left = current.Left;
}
else
{
parent.Right = current.Left;
}结果
- current.Right != null && current.Right.Left == null
示例
代码current.Right.Left = current.Left; if (parent == null)
{
this.Root = current.Right;
}
else if (isLeft)
{
parent.Left = current.Right;
}
else
{
parent.Right = current.Right;
}结果
- current.Right != null && current.Right.Left != null
示例
代码Node<T> currentRightSmallestParent = current.Right;
var currentRightSmallest = current.Right.Left; this.FindSmallest(ref currentRightSmallestParent, ref currentRightSmallest); currentRightSmallestParent.Left = currentRightSmallest.Right;
currentRightSmallest.Left = current.Left;
currentRightSmallest.Right = current.Right;
if (parent == null)
{
this.Root = currentRightSmallest;
}
else if (isLeft)
{
parent.Left = currentRightSmallest;
}
else
{
parent.Right = currentRightSmallest;
}结果
说明
这里的重点是 FindSmallest,找出 current.Right.Left 子树中最小的元素,然后用它替换 current。
完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DataStuctureStudy.Trees
{
class UnBalancedBinarySearchTree
{
class Node<T>
where T : IComparable<T>
{
public T Value { get; set; } public Node<T> Left { get; set; } public Node<T> Right { get; set; } public void InOrderTraverse(Action<T> action)
{
if (this.Left != null)
{
this.Left.InOrderTraverse(action);
} action(this.Value); if (this.Right != null)
{
this.Right.InOrderTraverse(action);
}
} public int Depth()
{
var leftDepth = ;
var rightDepth = ; if (this.Left != null)
{
leftDepth = this.Left.Depth();
}
if (this.Right != null)
{
rightDepth = this.Right.Depth();
} return
leftDepth > rightDepth
? leftDepth +
: rightDepth + ;
}
} public class Tree<T>
where T : IComparable<T>
{
private Node<T> Root { get; set; } public void Display()
{
Console.WriteLine(); if (this.Root == null)
{
return;
} var depth = this.Root.Depth();
var buffers = new string[depth][];
for (int i = ; i < buffers.Length; i++)
{
buffers[i] = new string[(int)(Math.Pow(, depth) - )];
} this.BuildArray(this.Root, depth, buffers, , ); for (int i = ; i < buffers.Length; i++)
{
for (int j = ; j < buffers[i].Length; j++)
{
if (buffers[i][j] == null)
{
Console.Write(new string(' ', ));
}
else
{
var leftPad = ( - buffers[i][j].Length) / ;
Console.Write(buffers[i][j]
.PadLeft(leftPad + buffers[i][j].Length)
.PadRight());
}
}
Console.WriteLine();
Console.WriteLine();
}
} private void BuildArray(Node<T> node, int nodeDepth, string[][] buffers, int row, int startColumn)
{
if (node == null)
{
return;
} var nodeWidth = Math.Pow(, nodeDepth) - ;
var column = (int)(startColumn + nodeWidth / ); buffers[row][column] = node.Value.ToString(); this.BuildArray(node.Left, nodeDepth - , buffers, row + , startColumn);
this.BuildArray(node.Right, nodeDepth - , buffers, row + , column + );
} public bool Contains(T item)
{
var current = this.Root; while (current != null)
{
if (item.CompareTo(current.Value) == )
{
return true;
}
else if (item.CompareTo(current.Value) < )
{
current = current.Left;
}
else
{
current = current.Right;
}
} return false;
} public void InOrderTraverse(Action<T> action)
{
if (this.Root != null)
{
this.Root.InOrderTraverse(action);
}
} public void Insert(T item)
{
var node = new Node<T> { Value = item }; Node<T> parent = null;
var current = this.Root;
var isLeft = false; while (current != null)
{
parent = current; if (item.CompareTo(current.Value) < )
{
current = current.Left;
isLeft = true;
}
else
{
current = current.Right;
isLeft = false;
}
} if (parent == null)
{
this.Root = node;
}
else if (isLeft)
{
parent.Left = node;
}
else
{
parent.Right = node;
}
} public bool Delete(T item)
{
Node<T> parent = null;
var current = this.Root;
var isLeft = false; this.Find(item, ref parent, ref current, ref isLeft); if (current == null)
{
return false;
} if (current.Right == null)
{
if (parent == null)
{
this.Root = current.Left;
}
else if (isLeft)
{
parent.Left = current.Left;
}
else
{
parent.Right = current.Left;
}
}
else if (current.Right != null && current.Right.Left == null)
{
current.Right.Left = current.Left; if (parent == null)
{
this.Root = current.Right;
}
else if (isLeft)
{
parent.Left = current.Right;
}
else
{
parent.Right = current.Right;
}
}
else
{
Node<T> currentRightSmallestParent = current.Right;
var currentRightSmallest = current.Right.Left; this.FindSmallest(ref currentRightSmallestParent, ref currentRightSmallest); currentRightSmallestParent.Left = currentRightSmallest.Right;
currentRightSmallest.Left = current.Left;
currentRightSmallest.Right = current.Right;
if (parent == null)
{
this.Root = currentRightSmallest;
}
else if (isLeft)
{
parent.Left = currentRightSmallest;
}
else
{
parent.Right = currentRightSmallest;
}
} return true;
} private void Find(T item, ref Node<T> parent, ref Node<T> current, ref bool isLeft)
{
while (current != null)
{
if (item.CompareTo(current.Value) == )
{
break;
} parent = current; if (item.CompareTo(current.Value) < )
{
current = current.Left;
isLeft = true;
}
else
{
current = current.Right;
isLeft = false;
}
}
} private void FindSmallest(ref Node<T> parent, ref Node<T> current)
{
while (current != null)
{
if (current.Left == null)
{
break;
} parent = current;
current = current.Left;
}
}
}
}
}
备注
学完这个树的最大收获就是,找到了一种输出树形结构相对高效的方法,比我之前用的高效,这种算法可以用在组织结构图的生成中。
算法:非平衡二叉搜索树(UnBalanced Binary Search Tree)的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 二叉搜索树BST(Binary Search Tree)
二叉搜索树(Binary Search Tree)也叫二叉排序树或二叉查找树.它满足以下性质: 1.非空左子树的所有键值小于其根结点的键值: 2.非空右子树的所有键值大于其根结点的键值: 3.左右子树 ...
- 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)
目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...
- 二叉搜索树(Binary Search Tree)
二叉搜索树(BST,Binary Search Tree),也称二叉排序树或二叉查找树. 二叉搜索树:一棵二叉树,可以为空:如果不为空,满足以下性质: 非空左子树的所有键值小于其根结点的键值: 非空右 ...
- 原生JS实现二叉搜索树(Binary Search Tree)
1.简述 二叉搜索树树(Binary Search Tree),它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子 ...
- 二叉搜索树(Binary Search Tree)--C语言描述(转)
图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...
- [Swift]LeetCode98. 验证二叉搜索树 | Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 数据结构-二叉搜索树(BST binary search tree)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...
- [Swift]LeetCode99. 恢复二叉搜索树 | Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- who am i ?
Id:Ox9A82 Email:hucvbty@gmail.com 微博:http://weibo.com/1828621423 知乎:Ox9A82 常乐村男子职业技术学院 Syclover拖后腿成员 ...
- 毕设demo写好
2015年1月20日 14:41:47 阶段性暂停!! 把运行结果截图给了老师,老师说先整理下文档,然后下学期来了再部署到服务器上. 那么,下学期来了,估计也要把Epm和CR1000什么的搞好了. 先 ...
- Azkaban(三)Azkaban的使用
界面介绍 首页有四个菜单 projects:最重要的部分,创建一个工程,所有flows将在工程中运行. scheduling:显示定时任务 executing:显示当前运行的任务 history:显示 ...
- 【POJ】1026.Cipher
题解 置换群的快速幂,然而我姿势水平不高,样例过不去,然后才明白这个置换的意思是这个位置上的数代表要把原位置的某个数换过来 需要新开一个数组存结果 代码 #include <iostream&g ...
- MySQL 存储过程传参数实现where id in(1,2,3,...)示例
一个MySQL 存储过程传参数的问题想实现例如筛选条件为:where id in(1,2,3,...),下面有个不错的示例,感兴趣的朋友可以参考下 正常写法: ,,,,...); 当在写存储过程 ...
- LoadRunner中的IP欺骗的设置以及误区
LoadRunner中的IP欺骗的设置以及误区 最近在忙着部署web性能测试的环境后,对IP欺骗进行设置,特地做个笔记,给自己的学习历程留下点足迹. 一. 什么是IP欺骗? 做什么事首先要问个为什么, ...
- jenkins远程连接linux配置测试
由于配置原因造成一下错误错误: jenkins.plugins.publish_over.BapPublisherException: Failed to connect session for co ...
- 使用setsid替代nohup
使用setsid替代nohup 使用 nohup 执行sudo -e时会有问题的.
- xshell连接linux,切换焦点,自动执行ctrl+c
这几天发现 xshell 连接 linux 的时候,无缘无故的执行了 ctrl+c,导致 执行界面 终端,比方说 ,hbase shell 执行窗口命令 ,每次切换 窗口焦点之后,就终止了.百度后 发 ...
- 学习 HMM
简介 HMM 中的变量可以分为两组. 第一组是状态变量 \(\{y_i,y_2,\cdots, y_n\}\), 其中 \(y_i \in \mathcal{Y}\) 表示第 \(i\) 时刻的系统状 ...