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的更多相关文章

  1. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  4. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  5. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. python 排序算法

    冒泡排序: 一. 冒泡排序的定义 冒泡排序(英语:Bubble Sort)是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.遍历数列的工作是重复地进 ...

  2. SSM-网站后台管理系统制作(3)---Google的reCaptcha验证码

    网上找了好久,也不知道怎么接入,后来看到一篇博客才搞好 reCaptcha官网:https://www.google.com/recaptcha/admin#site/344147946 参考博客:h ...

  3. (转) Ensemble Methods for Deep Learning Neural Networks to Reduce Variance and Improve Performance

    Ensemble Methods for Deep Learning Neural Networks to Reduce Variance and Improve Performance 2018-1 ...

  4. PTA编程总结3—抓老鼠啊~亏了还是赚了?

    题目: 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块奶酪(C),或者什么也不放(X).捕鼠夹 ...

  5. 浅谈Overload和Override

    Overload是重载的意思,Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). 重写Ove ...

  6. C# 虹软离线SDK引擎 人脸识别

    一,背景 整体来说虹软的人脸识别SDK还是不错的.我们测试过Face++的,百度的,腾讯的,都是在线联网的,需要把上传数据到它们服务器,不利商业用途:虹软SDK支持离线. 二,下载虹软SDK 1.注册 ...

  7. Axure 页面内多组内容切换的实现 + 利用一个内联框架实现百度地图访问

    Axure  页面内多组内容切换的实现,场景:点击某个元件的时候,会显示响应的页面 操作:将显示的页面设置为动态面板,如图所示应该设置动态面板的状态为三个状态,分别为点击qq账号.手机账号.邮箱账号时 ...

  8. conda基本操作

    准备环境 # 下载并安装conda基础环境 $ wget https://mirror.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.3.1 ...

  9. Matlab:高阶常微分三种边界条件的特殊解法(隐式Euler)

    函数文件1: function b=F(f,x0,u,h) b(1,1)=x0(1)-h*x0(2)-u(1); b(2,1)=x0(2)+h*x0(1)^2-u(2)-h*f; 函数文件2: fun ...

  10. Opencv-Android安装使用

    这是本人第一篇博客,写博客并不是说本人的技术能力有多强,或者说炫耀自己取得的进展.在当今这个数据信息大爆炸的年代,每个人肯定会接受很多新鲜事物,但不是所有东西你都能记得一清二楚,写博客一方面是为了巩固 ...