算法-B樹和B+樹總結

2020-12-17 偶然與突然

簡介

在計算機科學中,B樹(英語:B-tree)是一種自平衡的樹,能夠保持數據有序。這種數據結構能夠讓查找數據、順序訪問、插入數據及刪除的動作,都在對數時間內完成。B樹,概括來說是一個一般化的二叉查找樹(binary search tree),可以擁有多於2個子節點。與自平衡二叉查找樹不同,B樹為系統大塊數據的讀寫操作做了優化。B樹減少定位記錄時所經歷的中間過程,從而加快存取速度。B樹這種數據結構可以用來描述外部存儲。這種數據結構常被應用在資料庫和文件系統的實現上。

定義

B-Tree is a self-balanced search tree with multiple keys in every node and more than two children for every node.

B樹是一種自平衡的搜索樹,每一個節點node都有多個keys,並且每個節點有2個子節點或者多於2個子節點。

B+ tree is an N-ary tree with a variable but often large number of children per node. A B+ tree consists of a root, internal nodes and leaves.The root may be either a leaf or a node with two or more children

B+樹是一個n叉排序樹,通常每個節點有多個孩子,一棵B+樹包含一個根節點、多個內部節點和葉子節點。根節點可能是一個葉子節點,也可能是一個包含兩個或兩個以上孩子節點的節點。

特徵

B-Tree of Order m has the following properties...

Property #1 - All the leaf nodes must be at same level.Property #2 - All nodes except root must have at least [m/2]-1 keys and maximum of m-1 keys.Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at least m/2 children.Property #4 - If the root node is a non leaf node, then it must have at least 2 children.Property #5 - A non leaf node with n-1 keys must have n number of children.Property #6 - All the key values within a node must be in Ascending Order.

搜尋方法

In a B-Ttree, the search operation is similar to that of Binary Search Tree. In a Binary search tree, the search process starts from the root node and every time we make a 2-way decision (we go to either left subtree or right subtree). In B-Tree also search process starts from the root node but every time we make n-way decision where n is the total number of children that node has. In a B-Ttree, the search operation is performed with O(log n) time complexity. The search operation is performed as follows...

Step 1: Read the search element from the userStep 2: Compare, the search element with first key value of root node in the tree.Step 3: If both are matching, then display "Given node found!!!" and terminate the functionStep 4: If both are not matching, then check whether search element is smaller or larger than that key value.Step 5: If search element is smaller, then continue the search process in left subtree.Step 6: If search element is larger, then compare with next key value in the same node and repeate step 3, 4, 5 and 6 until we found exact match or comparision completed with last key value in a leaf node.Step 7: If we completed with last key value in a leaf node, then display "Element is not found" and terminate the function.

大致的意思就是查詢的條件A,和root節點值比較,如果相等則success,不相等則判斷是否小於該節點,小於則查詢左子樹,大於則查詢此時節點的下一個值,以此類推,直到此時節點最後一個值還是小於A,則查詢右子樹,直到找到或者結束查找。

插入方法

Insertion Operation in B-Tree In a B-Tree, the new element must be added only at leaf node. That means, always the new keyValue is attached to leaf node only. The insertion operation is performed as follows...

Step 1: Check whether tree is Empty.Step 2: If tree is Empty, then create a new node with new key value and insert into the tree as a root node.Step 3: If tree is Not Empty, then find a leaf node to which the new key value cab be added using Binary Search Tree logic.Step 4: If that leaf node has an empty position, then add the new key value to that leaf node by maintaining ascending order of key value within the node.Step 5: If that leaf node is already full, then split that leaf node by sending middle value to its parent node. Repeat tha same until sending value is fixed into a node.Step 6: If the spilting is occuring to the root node, then the middle value becomes new root node for the tree and the height of the tree is increased by one.

刪除方法

參考8的連結,有圖有真相,這裡就摘錄一些重點文字條件吧。

k:刪除的值x: k所在的節點x.n: k所在節點的長度t: k所在節點的層級

If k is in the node x which is a leaf and x.n>=t,Here you can straightaway delete k from x.If k is in the node x which is a leaf and x.n == (t-1)Find the immediate sibling y of x, the extreme key m of y, the parent p of x and the parent key l of k If y.n >= t: Move l into x Move m into p Delete k from xFind the immediate sibling y of x, the extreme key m of y, the parent p of x and the parent key l of k If y.n == t-1: Merge x and y Move down l to the new node as the median key Delete k from the new nodeIf k is in the node x and x is an internal node (not a leaf)Find the child node y that precedes k (the node which is on the left side of k) If y.n >= t: Find the key k』 in y which is the predecessor of k Delete k』 recursively. (Here k』 can be another internal node as well. So we have to delete it recursively in the same way) Replace k with k』 in xFind the child node y that precedes k If y.n < t (or y.n == (t-1)): Find the child node z that follows k (the node which is on the right side of k) If z.n >= t: Find k』』 in z which is the successor of k Delete k』』 recursively Replace k with k』』 in xFind the child node y that precedes k and the child node z that follows kIf y.n == (t-1) AND z.n == (t-1): Merge k and z to y Free memory of node z Recursively delete k from y

B-樹和B+樹區別

B和B+樹的區別在於,B+樹的非葉子結點只包含key信息,不包含data,每個節點的指針上限為2t而不是2t+1.所有的葉子結點和相連的節點使用鍊表相連,便於區間查找和遍歷。

B樹和B+樹的區別

綜述

磁碟存儲和mysql的索引這一塊用的比較多,以空間換時間來提升查找速度。(圖片基本是從參考連結那邊拿過來的,站在前人的肩膀上。)

參考

https://zh.wikipedia.org/wiki/B%E6%A0%91http://blog.codinglabs.org/articles/theory-of-mysql-index.htmlhttp://btechsmartclass.com/DS/U5_T3.htmlhttp://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.htmlhttps://www.geeksforgeeks.org/b-tree-set-1-introduction-2/https://www.geeksforgeeks.org/b-tree-set-1-insert-2/https://www.geeksforgeeks.org/b-tree-set-3delete/https://medium.com/@vijinimallawaarachchi/all-you-need-to-know-about-deleting-keys-from-b-trees-9090f3334b5c

相關焦點

  • b樹和b+樹有什麼不同 b樹和b+樹特點區別匯總
    首頁 > 問答 > 關鍵詞 > b樹最新資訊 > 正文 b樹和b+樹有什麼不同 b樹和b+樹特點區別匯總
  • b樹和b+樹的區別是什麼?b+樹數據結構詳細介紹
    B樹即二叉搜索樹,所有非葉子結點至多擁有兩個兒子(Left和Right,所有結點存儲一個關鍵字,非葉子結點的左指針指向小於其關鍵字的子樹,右指針指向大於其關鍵字的子樹。那麼b+樹和b+樹的區別是什麼?以下是b+樹數據結構詳細介紹。
  • 為什麼Mongodb索引用B樹,而MySQL用B+樹?
    B樹和B+樹開頭,我們先回憶一下,B樹和B+樹的結構以及特點,如下所示:B樹注意一下B樹的兩個明顯特點B+樹注意一下B+樹的兩個明顯特點針對上面的B+樹和B樹的特點,我們做一個總結(1)B樹的樹內存儲數據,因此查詢單條數據的時候,B樹的查詢效率不固定,最好的情況是O(
  • 決策樹學習筆記(三):CART算法,決策樹總結
    ,旨在從最簡單的決策樹開始學習,循序漸進,最後理解並掌握複雜模型GBDT,Xgboost,為要想要深入了解機器學習算法和參加數據挖掘競賽的朋友提供幫助。下面將詳細介紹CART算法的來龍去脈。CART生成算法CART剪枝算法CART算法小結決策樹算法優缺點總結▍CART生成算法為什麼叫CART算法呢?這還要從它的英文單詞說起。
  • 什麼B+樹索引,為什麼MySQL使用B+樹索引,而MongoDB缺還是使用B樹索引
    其根本原因是沒有去研究問題本身什麼是B+樹索引,而是簡單只是去背書上或別人博客裡列出的特性列表。要回答什麼是B+樹,首先需要什麼是B樹索引(也有被翻譯成B-樹了,其實2個是一回事,之所以會被翻譯成B-樹,其實是英文裡面是叫B-tree index,-其實是英文的連接符,如果要翻譯成B-樹,那B+就應該叫B+-樹了,所以B-的翻譯是不對的,但是大家看到了需要知道其實就是B樹索引)。
  • 算法連載之紅黑樹
    插入指定元素算法1、先按二叉搜索樹算法插入指定元素,並標記為紅色。插入key為<4>的節點。(為簡化圖形,省略哨兵節點)2、新增節點的父節點位於祖父節點的左子樹中。,時間複雜度是常量情況三:一次旋轉後結束循環,時間複雜度是常量所以,最壞情況時間複雜度是O(lgn)刪除指定元素算法1、二叉搜索樹刪除節點有4種情況,同樣紅黑樹也具有4種刪除情況,但額外需要增加紅黑樹性質調整
  • 【算法】決策樹與ID3算法
    決策樹(Decision Tree)算法是機器學習(Machine Learning)中分類算法中的一個重要算法,屬於監督學習(Supervised Learning)算法。決策樹算法是一種逼近離散函數值的方法。它是一種典型的分類方法,首先對數據進行處理,利用歸納算法生成可讀的規則和決策樹,然後使用決策對新數據進行分析。
  • 有關樹遍歷的javascript實現【前端-總結-leetcode算法題】
    在這裡插入圖片描述前言二月的第一天,總結一下近段時間刷的有關樹遍歷的leetcode算法題,希望寫完這篇文章的我和看完文章的你都有些收穫吧。全篇主要講的是有關樹的遍歷,使用前端javascript語言實現。當然有關樹的操作還有很多需要的深入理解和總結的。今天就暫時先把樹遍歷理解全吧。
  • MySQL索引原理-B樹和B+樹的區別
    ,它維護有序數據並允許以對數時間進行搜索,順序訪問,插入和刪除。B樹是二叉搜索樹的一般化,因為節點可以有兩個以上的子節點。[1]與其他自平衡二進位搜索樹不同,B樹非常適合讀取和寫入相對較大的數據塊(如光碟)的存儲系統。它通常用於資料庫和文件系統。
  • IBM SPSS Modeler算法系列-----決策樹CHAID算法
    (包括C5.0、CHAID、QUEST、C&R和決策列表)的區別,這可以幫助大家在選用算法的時候有一些參考。    然後開始計算卡方值,卡方值的計算公式為:K^2 = n (ad - bc) ^ 2 / [(a+b)(c+d)(a+c)(b+d)] 其中a、b、c、d分別對應的值如下圖:(其中n=a+b+c+
  • 比較全面的Adaboost算法總結(二)
    總結本文詳細總結了AdaBoost算法的相關理論,第一篇文章相當於是入門AdaBoost算法,本文是第二篇文章,該文詳細推導了AdaBoost算法的參數求解過程以及討論了模型的過擬合問題。AdaBoost算法是一種迭代算法,樣本權重和學習器權重根據一定的公式進行更新,第一篇文章給出了更新公式,但是並沒有解釋原因,本節用前向分布算法去推導樣本權重和學習器權重的更新公式。
  • 數據結構與算法大總結
    數據結構知識綜述一、前言經過一個學期對數據結構與算法的學習,對這門課程有了更深入的了解,了解到了數據結構的相關知識,明白了,數據結構的重要性及應用,回顧一個學期,通過這個小論文,來總結一學期的學習成果,數據結構這門課程,具有一定的深度和廣度,有其複雜性,我們一起來分析一下
  • 貪心算法:最小生成樹
    【前言】前幾天發的《一文搞懂貪心算法》中提到了使用使用貪心算法來計算最短路問題,今天接著給大家分享下在最小生成樹的兩種算法中的貪心思想。希望能對大家有所幫助。如果G的子圖G』是一棵包含G的所有頂點的樹,則稱G』為G的生成樹。生成樹上各邊權的總和稱為生成樹的耗費。在G的所有生成樹中,耗費最小的生成樹稱為G的最小生成樹。構造最小生成樹的兩種方法:Prim算法和Kruskal算法。
  • 《算法》筆記 11 - 最小生成樹
    這樣,一條橫切邊就是連接該集合的一個頂點和不在該集合中的另一個頂點的一條邊。切分定理切分定理的內容為:在一副加權圖中,給定任意的切分,它的橫切邊中的權重最小者必然屬於圖的最小生成樹。切分定理是最小生成樹算法的理論依據。
  • 決策樹-ID3算法和C4.5算法
    它通過對已有樣本的學習生成一顆決策樹(可看成if-then規則集合),從而能對新樣本作出相應分類。本文重點闡述如何選擇特徵建立決策樹,並給出理解算法的具體實例。什麼是決策樹?ID3算法詳解2.1 什麼是熵2.2 ID3算法2.3 ID3算法的缺點C4.5算法詳解3.1 第一個問題的改進辦法3.2 第二個問題的改進辦法決策樹:通過對已知樣本的學習,一步一步將特徵進行分類,從而將整個特徵空間進行劃分,進而區分出不同類別的算法
  • 【數據結構與算法圖文動畫詳解】終於可以徹底弄懂:紅黑樹、B-樹、B+樹、B*樹、滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜索樹
    圖 1(A)用廣義表表示為:(A , ( B ( E ( K , L ) , F ) , C ( G ) , D ( H ( M ) , I , J ) ) )1.4 樹的應用場景1.xml,html等,那麼編寫這些東西的解析器的時候,不可避免用到樹2.路由協議就是使用了樹的算法3.mysql資料庫索引4.
  • 數據結構與算法分析筆記——B樹
    數據結構與算法分析筆記——B樹B樹AVL樹的特徵是,通過在插入或刪除時的微調,使得整個樹中任意節點的左子樹和右子樹之間的高度差不超過1。這樣的結果是,整棵樹的高度不至於太高。那麼,如果我們想要得到一棵更低的樹?該如何呢?
  • 樹 Story —— B 樹 / B+ 樹
    B樹減少定位記錄時所經歷的中間過程,從而加快存取速度。B樹這種數據結構可以用來描述外部存儲。這種數據結構常被應用在資料庫和文件系統的實現上。B 樹的數據(或指向數據的指針)存在每個節點裡,而 B+ 樹的數據(或指向數據的指針)僅存在葉子節點裡,非葉子節點只有索引。
  • 乾貨||鍊表的技巧和算法總結
    但是還是想在這裡基於python做一下總結,順便總結一下鍊表的各種操作。首先先看一下leetcode上面的題目:反轉一個單鍊表。示例:輸入: 1->2->3->4->5->NULL輸出: 5->4->3->2->1->NULL進階:你可以迭代或遞歸地反轉鍊表。
  • 深度學習之DNN與反向傳播算法總結
    在深度神經網絡(DNN)模型與前向傳播算法中,我們對DNN的模型和前向傳播算法做了總結,這裡我們更進一步,對DNN的反向傳播算法(Back Propagation,BP)做一個總結。在了解DNN的反向傳播算法前,我們先要知道DNN反向傳播算法要解決的問題,也就是說,什麼時候我們需要這個反向傳播算法?