Binary Search Tree Learning Summary
BST Definition
BST is short for Binary Search Tree, by definition, the value of right node is always greater or equal to the root node, the value of left node is always less than the root node.
BST data structure and insert data
public class TreeNode<T>
{
public T data
{
get;
set;
} public TreeNode<T> leftChild
{
get;
set;
} public TreeNode<T> rightChild
{
get;
set;
}
}
public class BinarySearchTree<T> where T: IComparable
{
public TreeNode<T> root
{
get;
set;
} public BinarySearchTree()
{
root = null;
} public void Insert(T data)
{
TreeNode<T> temp = new TreeNode<T>();
temp.data = data;
TreeNode<T> current = root;
TreeNode<T> parent = null; if (root == null)
{
root = temp;
return;
} while (current != null)
{
parent = current;
if (current.data.CompareTo(data) > )
{
current = current.leftChild;
}
else
{
current = current.rightChild;
}
} // end of while if (data.CompareTo(parent.data) >= )
{
parent.rightChild = temp;
}
else
{
parent.leftChild = temp;
}
}
insert element to BST
1) if nothing is there, current element will be root node.
2) compare the value in BST, find the position of to be inserted, also track the parent reference. if found the position, compare the target value with parent node value, if less, insert to left, otherwise, insert to the right child.
Find Min from BST
Brutal force is to traverse the tree and also record the min value. However for BST, need to leverage its feature. becuase its value is kind of sorted.
so find min, continue find left until leaf node, which is the smallest value.
same pattern as above, if asked to find max, then starting from root node, find the right leaf node, then that one is the biggest value.
This is good optimization as you save some steps to traverse the whole tree.
Delete node with target value, which has two children
1. Find the target node firstly
2. if Found, check if in-order successor node is right child of to be deleted node, if is, replace the target node.
3. If the in-order successor node is not right child, replace the left node with root node.
4. Please make sure, current's parent left and child node reference needs be pointed to in-order successor node
5. Whenever you change the node, let it pointing to different node, please check the one pointing to it is updated firstly. we need ensure the sequence, otherwise, there might be loop reference.
public bool FindAndDeleteNodeIfItHasTwoChild(T key)
{
if (this.root == null)
{
return false;
} TreeNode<T> current = this.root;
TreeNode<T> parent = null;
TreeNode<T> nextSuccessorNode = null;
TreeNode<T> nextSuccessorParentNode = null; // locate the node
while (current != null)
{
if (key.CompareTo(current.data) == )
{
if (current.leftChild != null && current.rightChild != null)
{
FindInOrderNextSuccessor(current.rightChild, ref nextSuccessorNode, ref nextSuccessorParentNode); if (current.rightChild == nextSuccessorNode)
{
if (parent.leftChild == current)
{
parent.leftChild = nextSuccessorNode;
}
else if (parent.rightChild == current)
{
parent.rightChild = nextSuccessorNode;
} nextSuccessorNode.leftChild = current.leftChild; if (current == root)
{
this.root = nextSuccessorNode;
}
Console.WriteLine("NEXT SUCCESSOR node is right child of target node, replace");
}
else
{
// move up
nextSuccessorParentNode.leftChild = nextSuccessorNode.rightChild; nextSuccessorNode.leftChild = current.leftChild;
nextSuccessorNode.rightChild = current.rightChild;
if (parent != null)
{
if (parent.leftChild == current)
{
parent.leftChild = nextSuccessorNode;
}
else if (parent.rightChild == current)
{
parent.rightChild = nextSuccessorNode;
}
} if (current == root)
{
this.root = nextSuccessorNode;
}
} current.leftChild = null;
current.rightChild = null; return true;
}
else
{
Console.WriteLine("found the value, but it does not have two childs, return.");
return false;
}
} parent = current;
if (key.CompareTo(current.data) >= )
{
current = current.rightChild;
}
else
{
current = current.leftChild;
}
} return false;
}
Binary Search Tree Learning Summary的更多相关文章
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- Html 标签种类
Html 标签种类 自闭合标签与闭合标签 自闭合标签 <标签 /> 闭合标签 <标签></标签> 块级标签与行内标签 浏览器审查元素使用: (1) f12 查看每个 ...
- Linux 查看网卡流量、网络端口
查看网络流量 # 查看网卡流量 命令:sar -n DEV 1 10 注:每1秒 显示 1次 显示 10次 平均时间: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcm ...
- jsp传Array数组到后台
jsp页面传递对象数组到后台的需求 JSP: //保存 $("#submitBtn").click(function(){ var flag = true; var eachfla ...
- jmeter基本使用
下载安装,推荐官网http://jmeter.apache.org/download_jmeter.cgi 安装步骤不做赘述,可以看这篇博文https://blog.csdn.net/u0103401 ...
- MIME类型解析
MIME(Multipurpose Internet Mail Extensions)多用途网络邮件扩展类型,可被称为Media type或Content type, 它设定某种类型的文件当被浏览器打 ...
- JS设计模式(13)状态模式
什么是状态模式? 定义:将事物内部的每个状态分别封装成类,内部状态改变会产生不同行为. 主要解决:对象的行为依赖于它的状态(属性),并且可以根据它的状态改变而改变它的相关行为. 何时使用:代码中包含大 ...
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
- event.target.dataset
dataset并不是典型意义上的JavaScript对象,而是个DOMStringMap对象,DOMStringMap是HTML5一种新的含有多个名-值对的交互变量. 1.event.target.d ...
- sqlserver 判断字符串是否是数字
https://www.cnblogs.com/zjfblog/p/5625202.html sql2005有个函数ISNUMERIC(expression)函数:当expression为数字时,返回 ...
- C++字节对齐汇总
一.什么是字节对齐 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问,这就需要各种类型数 ...