思路


二叉搜索树的插入

TreeNode InsertRec(rootNode, key) =

    if rootNode == NULL, return new Node(key)

    if key >= rootNode.data, rootNode.rightChild = InsertRec(rootNode.rightChild, key)

    if Key < rootNode.data, rootNode.leftChild = InsertRec(rootNode.leftChild, key)

Context is: currentNode的reference 和要插入的key.

把key插入一个以rootNode为根的子树中去

二叉搜索树的删除

TreeNode DeleteRec(rootNode, key) =

    if rootNode == NULL, return rootNode

    if key >= rootNode.data, rootNode.rightChild = DeleteRec(rootNode.rightChild, key)

    if Key < rootNode.data, rootNode.leftChild = DeleteRec(rootNode.leftChild, key)

Context is: currentNode的reference 和要删除的key.

把key从一个以rootNode为根的子树中删去

在base case里,

  1. 如果是叶子节点返回空即可,
  2. 如果是单子节点,如果左子为空就返回右子。如果右子为空,就返回左子。
  3. 如果被删节点有两个孩子,则组要借助util找到中序遍历的后继节点, take 其值,然后再递归删除successor节点。

实现


        /// <summary>
/// insert record to BST recursively
/// </summary>
/// <param name="root">current root node</param>
/// <param name="key">the value of the element to be inserted</param>
public TreeNode<T> InsertRec(TreeNode<T> rootNode, T key)
{
if (rootNode == null)
{
rootNode = new TreeNode<T>();
rootNode.data = key;
return rootNode;
} if (key.CompareTo(rootNode.data) < )
{
rootNode.leftChild = InsertRec(rootNode.leftChild, key);
}
else
{
rootNode.rightChild = InsertRec(rootNode.rightChild, key);
} return rootNode;
} public void DeleteKey(T key)
{
this.root = DeleteRec(root, key);
} public void InsertKey(T key)
{
this.root = InsertRec(root, key);
} /// <summary>
/// Delete record from BST recursively
/// </summary>
/// <param name="rootNode">root node</param>
/// <param name="Key">value of the element</param>
/// <returns></returns>
public TreeNode<T> DeleteRec(TreeNode<T> node, T key)
{
if (node == null)
{
return null;
} if (key.CompareTo(node.data) < )
{
node.leftChild = DeleteRec(node.leftChild, key);
}
else if (key.CompareTo(node.data) > )
{
node.rightChild = DeleteRec(node.rightChild, key);
}
else // find the node, node is the one to be deleted
{
if (node.leftChild == null)
{
return node.rightChild;
}
else if (node.rightChild == null)
{
return node.leftChild;
}
else
{
// need to handle the root?
T value = GetMinValue(node);
node.data = value;
node.rightChild = DeleteRec(node.rightChild, value);
}
} return node;
} private T GetMinValue(TreeNode<T> node)
{
if (node == null)
{
throw new Exception("node is null.");
} if (node.rightChild != null)
{
TreeNode<T> temp = node.rightChild;
while (temp.leftChild != null)
{
temp = temp.leftChild;
} return temp.data;
}
else
{
throw new Exception("successor node is not found");
}
}

二叉搜索树(BST)的插入和删除递归实现的更多相关文章

  1. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历   二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...

  2. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...

  3. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  4. 萌新笔记之二叉搜索树(BST)

    前言,以前搞过线段树,二叉树觉得也就那样= =.然后数据结构的课也没怎么听过,然后下周期中考... 本来以为今天英语考完可以好好搞ACM了,然后这个数据结构期中考感觉会丢人,还是好好学习一波. 二叉搜 ...

  5. 给定一个二叉搜索树(BST),找到树中第 K 小的节点

    问题:给定一个二叉搜索树(BST),找到树中第 K 小的节点. 出题人:阿里巴巴出题专家:文景/阿里云 CDN 资深技术专家. 考察点: 1. 基础数据结构的理解和编码能力 2.  递归使用 参考答案 ...

  6. 二叉搜索树-php实现 插入删除查找等操作

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...

  7. 二叉搜索树 (BST) 的创建以及遍历

    二叉搜索树(Binary Search Tree) : 属于二叉树,其中每个节点都含有一个可以比较的键(如需要可以在键上关联值), 且每个节点的键都大于其左子树中的任意节点而小于右子树的任意节点的键. ...

  8. 二叉搜索树(BST)学习笔记

    BST调了一天,最后遍历参数错了,没药救了-- 本文所有代码均使用数组+结构体,不使用指针! 前言--BFS是啥 BST 二叉搜索树是基于二叉树的一种树,一种特殊的二叉树. 二叉搜索树要么是一颗空树, ...

  9. 二叉搜索树(BST)

    (第一段日常扯蛋,大家不要看)这几天就要回家了,osgearth暂时也不想弄了,毕竟不是几天就能弄出来的,所以打算过完年回来再弄.这几天闲着也是闲着,就掏出了之前买的算法导论看了看,把二叉搜索树实现了 ...

  10. 数据结构---二叉搜索树BST实现

    1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...

随机推荐

  1. leetcode 703数据流中的第K大元素

    这里思路是堆排序,而且是小根堆.C++中包含在头文件<queue>的priority_queue本质就是堆排序实现的.其中priority_queue函数原型是 priority_queu ...

  2. react 入坑之罪

    componentDidMount :生命周期在react下只调用一次, render:比它先执行 componentWillRecvieProps(newProps) :能取到父组件的值 rende ...

  3. GetLastError 错误返回码

    (0)-操作成功完成.(1)-功能错误.(2)- 系统找不到指定的文件.(3)-系统找不到指定的路径.(4)-系统无法打开文件.(5)-拒绝访问.(6)-句柄无 效.(7)-存储控制块被损坏.(8)- ...

  4. 使用img2html把图片转为网页

    代码如下: # -*- coding: utf-8 -*- # Nola """ img2html : Convert image to HTML optional ar ...

  5. js操作对象

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. OSI,TCP/IP,五层协议的体系结构,以及各层协议

    OSI分层 (7层):物理层.数据链路层.网络层.传输层.会话层.表示层.应用层. TCP/IP分层(4层):网络接口层. 网际层.运输层. 应用层. 五层协议 (5层):物理层.数据链路层.网络层. ...

  7. linux安装命令出错(could not resolve host mirrorlist.centos.org)

    本次问题出现在虚拟机安装的centos7系统之后,使用yum命令安装wget命令(yum -y install wget),出现could not resolve host mirrorlist.ce ...

  8. layui从子iframe打开父iframe的tab选项卡

    数据表格字段: {field: 'novelId', title: '小说ID',width:100,templet: '<div><a href="javascript: ...

  9. Python 字典删除元素clear、pop、popitem

    同其它python内建数据类型一样,字典dict也是有一些实用的操作方法.这里我们要说的是字典删除方法:clear().pop()和popitem(),这三种方法的作用不同,操作方法及返回值都不相同. ...

  10. ImportError: No module named _tkinter, please install the python-tk package ubuntu运行tkinter错误

    这是由于Python的版本没有包含tkinter的模块,只需要把tk的package安装就可以了. 一般在Linux才出现,windows版本一般已经包含了tkinter模块. apt-get ins ...