bst

LeetCode 938. Range Sum of BST

女生的网名这么多〃 提交于 2019-12-12 09:25:31
原题链接在这里: https://leetcode.com/problems/range-sum-of-bst/ 题目: Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Example 1: Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 Output: 32 Example 2: Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 Output: 23 Note: The number of nodes in the tree is at most 10000 . The final answer is guaranteed to be less than 2^31 . 题解: If current node is null, return 0. If node value < L, return range sum from its right.

算法原理系列:2-3查找树

你。 提交于 2019-12-08 04:22:30
2-3查找树 第一次接触它是在刷数据结构那本书时,有它的介绍。而那时候只是单纯的理解它的节点是如何分裂,以及整个构建过程,并不清楚它的实际用处,所以看了也就忘了。而当看完《算法》查找章节时,顿时有种顿悟,喔,原来如此啊。所以,提出来的这些有趣的结构千万不能割裂来看,它的演变如此诱人,细节值得品味。 结构缘由 首先,搞清楚2-3查找树为什么会出来,它要解决什么样的问题?假设我们对它的基本已经有所了解了。先给它来个简单的定义: 2-3查找树: 一种保持有序结构的查找树。 可以维持动态平衡的有序查找树。 从上述定义就可以看出,它到底是为了解决什么问题,在上一篇文章中,介绍了【查找】的演变过程,详细请参看博文 这里 。其中最后优化到了BST这种树的结构。但我们都知道BST它对数据的输入是敏感的,如最坏情况下,每次 put() 的 key 是有序的,那么构造出来的 BST 树,就相当于一个链表,那么对于每个元素的查找,它的性能就相当糟糕。而2-3树就是为了规避上述问题而设计发明出来的模型。现在请思考该如何设计它呢? 这里我们从BST遇到的实际问题出发,提出设计指标,再去思考利用些潜在的性质来构建2-3树。这部分内容,没有什么理论根据,而是我自己尝试去抓些字典的性质来构建,而2-3树的诞生过程并非真的如此,所以仅供参考。 构建2-3树 字典的两个主要操作为:查找和插入。而在前面一篇文章说到

算法练习笔记(七)——在BST树中的遍历

守給你的承諾、 提交于 2019-12-07 04:46:27
一般来说,BST(二叉排序树)有分三种遍历,前序遍历,中序遍历和后序遍历。 前序遍历顺序是根在前的根,左子树,右子树(也是我们一般来表示一棵树的方法) 中序:左子树,根,右子树 后序:左子树,右子树,根 值得注意的是在BST树的中序遍历之中,结果必然是递增的。 于是对于下面这一道题目: 源地址:https://leetcode.com/problems/kth-smallest-element-in-a-bst/#/description 题目: Kth Smallest Element in a BST 描述: Given a binary search tree, write a function kthSmallest to find the k th smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

BST and Heap详解

和自甴很熟 提交于 2019-12-05 12:19:30
BST(Binary Search Tree) 基本特点: 二叉树 集合中的数据具有可比较大小的关键码 数据之间满足BST特性 中序遍历可得到一个递增的数据序列(可作为判断一棵二叉树是否是BST的方法) 同一个数据集合,可存在多个不同形态的BST树 基本操作 问题描述+求解动机+算法思想+算法步骤+性能分析 进行操作,都需要: 先找到要操作的数(位置),进行操作,保证BST的特性,保障优的算法性能。 查找(logn) 插入(logn ~ n) 若给定值 小于 根结点的关键字,则继续 在 左 子树上进行查找 若给定值 小于 根结点的关键字,则继续在 左 子树上进行插入; 将返回值(结点指针)设置为(当前)根结点的左孩子 若给定值 大于 根结点的关键字,则继续 在 右 子树上进行查找 若给定值 大于 根结点的关键字,则继续在 右 子树上进行插入; 将返回值(结点指针)设置为(当前)根结点的右孩子 若给定值等于根结点的关键字,则查找 成功 /   删除操作:   从最简单的情况开始—— 删除最小值 ; 由BST特性可知,BST中最小值一定在左子树的最左边取到,于是去get到它。 BST删除最小值算法思想(非递归) 1)定义一个指向BST树结点的临时指针变量p和pp。 2)从BST的根结点开始;将其值赋值给p。 3)若p的左孩子不等于空指针,将p的值赋值给pp;然后

二叉搜索树BST

心已入冬 提交于 2019-12-04 21:20:03
数据域大小关系:   根>左,根<右 假设所有二叉树的所有结点数据都是正数,且两两不同 arr 6,3,8,2,5,1,7 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct node{ 5 int data; 6 struct node* left; 7 struct node* right; 8 } Node; 9 10 typedef struct{ 11 Node* root; 12 } Tree; 13 14 void insert(Tree* tree, int value) 15 { 16 Node* node= malloc(sizeof(Node)); 17 node -> data = value; 18 node -> left = NULL; 19 node -> right = NULL; 20 21 if(tree -> root == NULL){ 22 tree -> root = node; 23 return ; 24 } 25 else 26 { 27 Node* temp = tree -> root; 28 while(temp != NULL){ 29 if(temp -> data > value){ 30 //走左边有两种情况 31 if(temp -> left

【algo&ds】【吐血整理】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、B树、字典树、红黑树、跳表、散列表

前提是你 提交于 2019-12-04 20:51:38
本博客内容耗时4天整理,如果需要转载,请注明出处,谢谢。 1.树 1.1树的定义 在计算机科学中,树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合。它是由n(n>0)个有限节点组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。它具有以下的特点: 每个节点都只有有限个子节点或无子节点; 没有父节点的节点称为根节点; 每一个非根节点有且只有一个父节点; 除了根节点外,每个子节点可以分为多个不相交的子树; 树里面没有环路(cycle) 1.2常见术语 节点的度 :一个节点含有的 子树的个数 称为该节点的度; 树的度 :一棵树中,最大的节点度称为树的度; 叶节点 或 终端节点 :度为零的节点; 非终端节点 或 分支节点 :度不为零的节点; 父亲节点 或 父节点 :若一个节点含有子节点,则这个节点称为其子节点的父节点; 孩子节点 或 子节点 :一个节点含有的子树的根节点称为该节点的子节点; 兄弟节点 :具有相同父节点的节点互称为兄弟节点; 节点的 层次 :从根开始定义起,根为第1层,根的子节点为第2层,以此类推; 深度 :对于任意节点n,n的深度为从根到n的唯一路径长,根的深度为0; 高度 :对于任意节点n,n的高度为从n到一片树叶的最长路径长,所有树叶的高度为0;

[LeetCode] 701. Insert into a Binary Search Tree_Medium_tag: Binary Search Tree

旧巷老猫 提交于 2019-12-04 16:59:40
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them. For example, Given the tree: 4 / \ 2 7 / \ 1 3 And the value to insert: 5 You can return this binary search tree: 4 / \ 2 7 / \ / 1 3 5 This tree is also valid: 5 / \ 2 7 / \ 1 3 \ 4这个题目利用recursive的方式

二叉搜索树BST(C语言实现可用)

徘徊边缘 提交于 2019-12-04 16:26:41
1:概述 搜索树是一种可以进行插入,搜索,删除等操作的数据结构,可以用作字典或优先级队列。二叉搜索树是最简单的搜索树。其左子树的键值<=根节点的键值,右子树的键值>=根节点的键值。 如果共有n个元素,那么每次操作需要的O(log n)的时间. 常用知识点 满二叉树 : 一棵深度为k,且有2^k-1个节点的二叉树,称为满二叉树。这种树的特点是每一层上的节点数都是最大节点数。 完全二叉树 : 而在一棵二叉树中,除最后一层外,若其余层都是满的,并且最后一层要么是满的,要么在右边缺少连续若干节点,则此二叉树为完全二叉树。具有n个节点的完全二叉树的深度为floor(log2n)+1。深度为k的完全二叉树,至少有2^(k-1)个叶子节点,至多有2^k-1个节点。 2.基本操作 查找(search) 插入(insert) 删除(remove) 3:操作原理    查找 假设查找的值为x,从根节点的值开始比对,如果小于根节点的值,则往左儿子继续查找,如果大于根节点的值,则往右儿子继续查找.依次类推.直到当前节点的值等于要查找的值.   以查找数值10为例 插入 按照查找的步骤即可找到插入值应该在的位置 以插入数值6为例 删除: 有四种情况: 1: // 当前节点无左节点 ,右字节点7覆盖5, : 3: // 当前节点无右节点 ,右字节点7覆盖5, : 4: // 删除节点5的左节点没有右节点,

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

房东的猫 提交于 2019-12-04 05:22:43
Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than or equal to the node's key. Both the left and right subtrees must also be binary search trees. If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST. Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or

浙大 PAT 1043. Is It a Binary Search Tree (25)

倖福魔咒の 提交于 2019-12-04 05:21:40
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than or equal to the node's key. Both the left and right subtrees must also be binary search trees. If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST. Now given a sequence of