bst

【2019.9.17】Za

本小妞迷上赌 提交于 2019-11-29 22:15:56
Za yyb Fibonacci的性质 \(gcd(f[i],f[i+1])=1\) 证明 \(gcd(f[i],f[i+1])\) \(=gcd(f[i+1]-f[i],f[i])\) \(=gcd(f[i-1],f[i])\) \(=....\) \(=gcd(f[1],f[2])=1)\) \(f[m+n]=f[m−1]f[n]+f[m]f[n+1]f[m+n]=f[m−1]f[n]+f[m]f[n+1]\) \(gcd(f[n+m],f[n])=gcd(f[n],f[m])gcd(f[n+m],f[n])=gcd(f[n],f[m])\) 由上面式子得到 \(gcd(f[n+m]=f[m−1]f[n]+f[m]f[n+1],f[n])gcd(f[n+m]=f[m−1]f[n]+f[m]f[n+1],f[n])\) \(=gcd(f[n+1]f[m],f[n])=gcd(f[n+1]f[m],f[n])\) \(=gcd(f[n+1],f[n])∗gcd(f[m],f[n])=gcd(f[n+1],f[n])∗gcd(f[m],f[n])\) \(=1∗gcd(f[m],f[n])=1∗gcd(f[m],f[n])\) \(=gcd(f[m],f[n])\) \(gcd(f[n],f[n+m])=f[gcd(n,n+m)]gcd(f[n],f[n+m])=f[gcd(n,n

二叉搜索树

笑着哭i 提交于 2019-11-29 19:20:20
目录 一、什么是二叉搜索树 二、二叉搜索操作的特别函数: 三、二叉查找树的查找操作:Find 四、查找最大和最小元素 五、二叉搜索树的插入 六、二叉搜索树的删除 6.1 删除的是叶结点 6.2 删除的结点只有一个孩子结点 6.3 删除的结点有左右子树 七、Python递归实现-二叉搜索树 更新、更全的《数据结构与算法》的更新网站,更有python、go、人工智能教学等着你: https://www.cnblogs.com/nickchen121/p/11407287.html 一、什么是二叉搜索树 首先让我们回顾之前说过的 查找问题 :上次我们之讲过了静态查找,这次我们将通过二叉搜索树实现动态查找。但是针对动态查找,数据该如何组织呢? 二叉搜索树(BST,Binary Search Tree) ,也称 二叉排序树或二叉查找树 二叉搜索树:一颗二叉树,可以为空;如果不为空,满足以下性质: 非空 左子树 的所有 键值小于其根节点 的键值 非空 右子树 的所有 键值大于其根节点 的键值 左、右子树都是二叉搜索树 二、二叉搜索操作的特别函数: Position Find(ElementType X, BinTree BST):从二叉搜索树BST中查找元素X,返回其所在结点的地址; Postion FindMin(BinTree BST):从二叉搜索树BST中查找并返回最小元素所在结点的地址

538. Convert BST to Greater Tree

妖精的绣舞 提交于 2019-11-29 04:37:05
link 思路 首先复习何谓binary search tree 简单来说,所有节点的right hand side值都比自己大;所有节点的left hand side值都比自己小。 根据以上的定义,开始剖析最小单元的binary tree,例如题目的example: root node为5,我们则先travel它的右子树,因我们知道右子树一定比root node还要大,那么root node的值则需要加上右子树的值。 加完后,观察此node的值(原5,加完后13)对于左子树来说,即是所有比他大的node value的总和了,此时再去travel左子树 C++ solution / * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* convertBST(TreeNode* root) { int sum = 0; travel(root, sum); return root; } void travel(TreeNode* node

230. Kth Smallest Element in a BST

大憨熊 提交于 2019-11-29 04:12:27
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 Output: 3 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? class Solution { public int kthSmallest(TreeNode root, int k) { List<Integer> list = new ArrayList();

二叉查询树(BST)

别来无恙 提交于 2019-11-28 22:26:59
二叉树 ,每个节点只有一个父节点(根节点除外),每个节点都只有左右两个链接,分别指向自己的左子节点和右子节点 二叉查找树 (BST)是一颗二叉树,并且每个节点的值都大于等于其左子树中的所有节点的值而小于等于右子树的所有节点的值。 二叉树遍历 前序遍历:中左右 中序编列:左中右 (BST中序编列是 递增顺序排序 ) 后续编列:左右中 基本实现 public class BST<Key extends Comparable<Key>, Value> { protected Node root; protected class Node { Key key; Value val; Node left; Node right; // 以该节点为根的子树节点总数 int N; Node(Key key, Value val, int N) { this.key = key; this.val = val; this.N = N; } } @Override public int size() { return size(root); } private int size(Node x) { if (x == null) return 0; return x.N; } protected void recalculateSize(Node x) { x.N = size(x.left) +

二叉查找树的实现与讲解(C++)

只愿长相守 提交于 2019-11-27 22:21:04
注:这篇文章源于: https://mp.csdn.net/postedit/99710904 , 无需怀疑抄袭,同一个作者,这是我在博客园的账号。 在二叉树中,有两种非常重要的条件,分别是两类数据结构的基础性质。 其一是“堆性质”,二叉堆以及高级数据结构中的所有可合并堆都满足“堆 性质”。 其二是 “BST性质”,它是二叉查找树(Binary Search Tree)以及所有平衡树的基础。 二叉查找树的定义 给定一棵二叉树,树上的每个节点都带有一个数值,成为节点的 “关键码” 。所谓BST性质是指,对于树中的任意一个节点: · 该节点的关键码不小于它的左子树(如果非空)中任意节点的关键码 · 该节点的关键码不大于它的右子树(如果非空)中任意节点的关键码 满足上述性质的二叉树就是一棵“二叉查找树”(BST)。 二叉查找树的中序遍历是一个关键码单调递增的节点序列。 二叉查找树的存储 用数组模拟二叉树 1 struct node { 2 int l, r;//左右子节点在数组中的下标 3 int val;//节点关键码 4 }tree[Size];//数组模拟链表 5 int tot;//使用过和正在使用的节点总数量 6 int root;//当前根节点编号,即数组下标 优点:编程复杂度低。不需要考虑分配内存和回收内存 缺点:内存利用率低 用指针表示二叉树 1 struct node

BST中的最小差值

送分小仙女□ 提交于 2019-11-27 18:17:23
给定一个确定根的二叉搜索树,返回树中任意两个不同节点的值的最小差。 样例 样例1: 输入: root = {4,2,6,1,3,#,#} 输出: 1 解释: 请留意,root是一个树节点的结构,而非一个普通数组。 给定的树{4,2,6,1,3,#,#}的样子如下图: 4 / \ 2 6 / \ 1 3 在这棵树中,最小数值差距为 1, 它出现在node 1 与 node 2 之间, 也同时存在 node 3 与 node 2之间 样例2: 输入: root = {5,1,8} 输出: 3 解释: 请留意,root是一个树节点的结构,而非一个普通数组。 给定的树{5,1,8}的样子如下图: 5 / \ 1 8 在这棵树中,最小数值差距为 3, 它出现在node 5与node 8之间. 注意事项 1.这棵二叉搜索树的大小在 2 到 100 之间。 2.这棵二叉搜索树是存在的,每个点的数值是一个整数,而且每个点的数值将会是不同的。 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } *

二叉查找树 BST

六眼飞鱼酱① 提交于 2019-11-27 03:20:30
一、定义: 要么二叉查找树是一棵空树 要么二叉查找树由根结点、左子树、右子树组成,其中左子树和右子树都是二叉查找树,且 左子树 上的所有节点的数据域都 小于或等于 根结点 的数据域, 右子树 上所有节点的数据域都 大于 根结点 的数据域。 二、二叉查找树的基本操作 2.1查找操作 void search(node* root,int x){ if(root==NULL){ printf("Failed!"); return; } if(x==root->data){ printf("%d\n",root->data); }else if(x>root->data){ search(root->right,x); }else{ search(root->left,x); } } 2.2 插入操作 void insert(node* &root,int x){ if(root==NULL){ root=newNode(x); return; } if(x==root->data){ return; }else if(x<root->data){ insert(root->left,x); }else{ insert(root->right,x); } } 2.3 建立二叉查找树 node* create(int data[],int n){ node* root=NULL; for

二叉搜索树的插入与删除实现

北慕城南 提交于 2019-11-27 00:29:22
BinTree Insert( BinTree BST, ElementType X ) { if ( !BST ){ /* 若原树为空,生成并返回一个结点的二叉搜索树 */ BST = (BinTree) malloc ( sizeof ( struct TNode)); BST->Data = X; BST->Left = BST->Right = NULL; } else { /* 开始找要插入元素的位置 */ if ( X < BST->Data ) BST->Left = Insert( BST->Left, X ); /*递归插入左子树*/ else if ( X > BST->Data ) BST->Right = Insert( BST->Right, X ); /*递归插入右子树*/ /* else X已经存在,什么都不做 */ } return BST; } BinTree Delete( BinTree BST, ElementType X ) { Position Tmp; if ( !BST ) printf ( "要删除的元素未找到" ); else { if ( X < BST->Data ) BST->Left = Delete( BST->Left, X ); /* 从左子树递归删除 */ else if ( X > BST->Data ) BST

1115 Counting Nodes in a BST (30 分)

淺唱寂寞╮ 提交于 2019-11-26 21:24:55
1115 Counting Nodes in a BST (30 分) 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 or equal to the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree. Input Specification: Each input file