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 ...
随机推荐
- java excel大数据量导入导出与优化
package com.hundsun.ta.utils; import java.io.File; import java.io.FileOutputStream; import java.io.I ...
- java的一些基本概念——java11、jdk、jre、jvm等
Java字节码 先介绍下c语言的编译过程,写好代码用编译器(比如gcc)编译过后是机器能够直接执行的二进制机器码. java也类似这种情况,但是java代码文件编译过后不是任何机器都能识别的机器码,而 ...
- 8th,常用模块、正则表达式
re模块 什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.内嵌在Python中,通过re模块实现.正则表达式模 ...
- MVC校验方式【六】
一.以往的校验都是自己写正则表达式校验,本篇讲的是通过MVC自带的jquery文件进行校验 实现步骤:1. 在webform页面中引入两个jquery文件,10,11行的代码. <%@ Page ...
- java笔记 -- java运算
运算符: 算术运算符: 加减乘除求余 + , - , * , / , % 当参与/运算的两个操作数都是整数时, 表示整数除法, 否则表示浮点除法. 例: 15 / 2 = 7; 15 % 2 = 1; ...
- 用GraphX分析伴生网络(二)
8. 过滤噪声边 在当前的伴生关系中,边的权重是基于一对概念同时出现在一篇论文中的频率来计算的.这种简单的权重机制的问题在于:它并没有对一对概念同时出现的原因加以区分,有时一对概念同时出现是由于它们具 ...
- js之获取元素最终css属性
很多时候 我们是不用jquery库的,虽然他很好,获取元素最终的css属性我们可以用:getComputedStyle window.getComputedStyle(element, null).g ...
- java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化
需求:远程服务接口模板化配置提供接入服务 模板接口分为三个模块:功能路由.参数校验.模板入库 路由:这里的实现方式很简单,就是根据业务标识找到对应的处理方法 参数校验: 参数校验这步涉及模板和校验类两 ...
- android:Internet(volley)
public void getStringFromCloud(View view) { /*创建请求队列*/ RequestQueue queue = Volley.newRequestQueue(t ...
- .NET学习日记【1】
不得不说,之前一年学习的内容基本上在第一章中都有所涉及,而且还讲了很多不知道知识.看完第一张对多态和继承都多了一些体会.在1.4前面的都有很认证的看过,也在vs上面验证了一下.然后也明白了.NET到底 ...