From d23f5ea952ab1b9ebe22cc6c4af413958bc8a24b Mon Sep 17 00:00:00 2001 From: Jozef Maloch Date: Mon, 13 May 2019 00:35:05 +0200 Subject: [PATCH] Insertion (#30223) --- guide/english/algorithms/b-trees/index.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/guide/english/algorithms/b-trees/index.md b/guide/english/algorithms/b-trees/index.md index 69f1152e91b..8a313218b42 100644 --- a/guide/english/algorithms/b-trees/index.md +++ b/guide/english/algorithms/b-trees/index.md @@ -17,16 +17,17 @@ Properties of B-Tree: 7) B-Tree grows and shrinks from root which is unlike Binary Search Tree. Binary Search Trees grow downward and also shrink from downward. 8) Like other balanced Binary Search Trees, time complexity to search, insert and delete is O(Log(n)). -Search: -Search is similar to search in Binary Search Tree. Let the key to be searched be k. We start from root and recursively traverse down. For every visited non-leaf node, if the node has key, we simply return the node. Otherwise we recur down to the appropriate child (the child which is just before the first greater key) of the node. If we reach a leaf node and don’t find k in the leaf node, we return NULL. +### Search: +Search is similar to search in Binary Search Tree. Let the key to be searched be k. We start from root and recursively traverse down. For every visited non-leaf node, if the node has key, we simply return the node. Otherwise we recur down to the appropriate child (The child which is just before the first greater key) of the node. If we reach a leaf node and don’t find k in the leaf node, we return NULL. -Traverse: +### Traverse: Traversal is also similar to Inorder traversal of Binary Tree. We start from the leftmost child, recursively print the leftmost child, then repeat the same process for remaining children and keys. In the end, recursively print the rightmost child. -Insertion: -We insert into the bottom of a B-tree, similar to binary search trees. First, find an appropriate place at the bottom of the tree to insert a given key, and perform the insertion (also adding an additional empty child). If the node is too big (it has m keys and m + 1 (empty) children), split the node. +### Insert +First we search and find out to which node the key should belong to and we insert it into it. Afterwards we look for and fix these problems: +If the number of keys is too high(greater than t - 1) then we move the middle key to the nodes parent. We do this recursively up until root. If the number of keys in the root is too high, then we make the middle key to be the new root of the whole tree and connect it to the node it was in before. -Time Analysis for B-Tree: +### Time Analysis for B-Tree: Suppose a B-tree has n elements and M is the maximum number of children a node can have. What is the maximum depth the tree could have? What is the minimum depth the tree could have?