Madyson  Reilly

Madyson Reilly

1597252200

Climbing B-Tree Indexes in Postgres

In computer science, a B-tree is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. Postgres has a few index types but B-trees are by far the most common. They’re good for sorting and matching

Full Article: http://www.entradasoft.com/blogs/climbing-b-tree-indexes-in-postgres

Here is a quick overview of what types of index(s) available to use in your tables:

· B-Tree: The default for Postgres.

· GIN: For JSONB and arrays. As inverted indexes, they contain an index entry for each word, with a compressed list of matching locations.

· GiST : For full text search and geospatial datatypes. A GiST index is lossy, meaning that the index might produce false matches.

· SP-GiST : For larger datasets with natural but uneven clustering

· **BRIN **(Block Range Index) : For really large datasets that line up sequentially. E.g. orders might have a date column, and most of the time the entries for earlier orders appear earlier in the table.

· Hash: The query planner will consider using a hash index whenever an indexed column is involved in a comparison using the = operator.

The Structure of a B-tree

Basically, it’s a data structure that sorts itself. That’s why it’s self-balancing — it chooses it’s shape on it’s own.The B-tree is a generalization of a binary search tree in that a node can have more than two children. Unlike self-balancing binary search trees, the B- tree is optimized for systems that read and write large blocks of data.

B-trees are “balanced” because the distance from the root to any leaf node is the same. A leaf node is a node without children. The root node is the node at the top. B-Tree is a tree where each node can have multiple children, or better said, a B-Tree can have N children. While in binary search trees each node can have one value, B-Trees have the concept of keys. Keys are like a list of values, that each of the nodes will hold.

Let’s draw it out:

The most important thing about B-Trees is their balancing aspect. The concept revolves on the fact that each node has keys, like in the example above. The way B-Trees balance themselves is really interesting, and the keys are the most important aspect of this this functionality.

Image for post

Basically, whenever a new item (or, in our case, a number) is added, the B-Tree finds the appropriate place (or, node) for the item to go in. For example, if we want to add the number 6, the B-Tree will “ask the root node”, in what node should it push the number in. “Asking” is nothing more than comparing the new number with the keys of the node. Since the number 6 is larger then 5, but smaller then the number 10 (which are the root node keys), it will create a new node just below the root node:

Image for post

With this mechanism, the B-Tree is always ordered and looking up a value in it is rather cheap.

Now if we wanted to look up the key, 6, we’d compare 6 to values in the root node and see that it’s between 5 and 10. So we’d use the pointer between 5 and 10 to find the node containing 6.

This is an abstraction over Postgres’ implementation but you can already imagine why this is faster than iterating over every number in a table and checking if it equals 6.

This is why B-trees can search, insert and delete in O(logN) time.

Limitations

Almost in all use cases, the power of indexes is noticeable on large amounts of data. This means that the indexes will have to be as big as the actual data tables. Or does it?

Imagine if we are dealing with billions of records. This means that the index tables will have the billions of records stored in an ordered fashion. Okay, PostgreSQL could handle that. But, can you imagine how long would an INSERT command take? Adding the record in the data table will take really long, because the index will have to add the new record in the correct place, to keep the order of the indexes. Due to this limitation, the implementation of the B-Tree index keeps page files, which simply put, are nodes on a big B-Tree data structure.

Syntax:

— Create an index column using btree on the purchase table

— CREATE INDEX <<index_name>> ON <<table_name>> USEING <<index_type>>

#sql #search #postgres #b-tree #indexing

What is GEEK

Buddha Community

Climbing B-Tree Indexes in Postgres
Madyson  Reilly

Madyson Reilly

1597252200

Climbing B-Tree Indexes in Postgres

In computer science, a B-tree is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. Postgres has a few index types but B-trees are by far the most common. They’re good for sorting and matching

Full Article: http://www.entradasoft.com/blogs/climbing-b-tree-indexes-in-postgres

Here is a quick overview of what types of index(s) available to use in your tables:

· B-Tree: The default for Postgres.

· GIN: For JSONB and arrays. As inverted indexes, they contain an index entry for each word, with a compressed list of matching locations.

· GiST : For full text search and geospatial datatypes. A GiST index is lossy, meaning that the index might produce false matches.

· SP-GiST : For larger datasets with natural but uneven clustering

· **BRIN **(Block Range Index) : For really large datasets that line up sequentially. E.g. orders might have a date column, and most of the time the entries for earlier orders appear earlier in the table.

· Hash: The query planner will consider using a hash index whenever an indexed column is involved in a comparison using the = operator.

The Structure of a B-tree

Basically, it’s a data structure that sorts itself. That’s why it’s self-balancing — it chooses it’s shape on it’s own.The B-tree is a generalization of a binary search tree in that a node can have more than two children. Unlike self-balancing binary search trees, the B- tree is optimized for systems that read and write large blocks of data.

B-trees are “balanced” because the distance from the root to any leaf node is the same. A leaf node is a node without children. The root node is the node at the top. B-Tree is a tree where each node can have multiple children, or better said, a B-Tree can have N children. While in binary search trees each node can have one value, B-Trees have the concept of keys. Keys are like a list of values, that each of the nodes will hold.

Let’s draw it out:

The most important thing about B-Trees is their balancing aspect. The concept revolves on the fact that each node has keys, like in the example above. The way B-Trees balance themselves is really interesting, and the keys are the most important aspect of this this functionality.

Image for post

Basically, whenever a new item (or, in our case, a number) is added, the B-Tree finds the appropriate place (or, node) for the item to go in. For example, if we want to add the number 6, the B-Tree will “ask the root node”, in what node should it push the number in. “Asking” is nothing more than comparing the new number with the keys of the node. Since the number 6 is larger then 5, but smaller then the number 10 (which are the root node keys), it will create a new node just below the root node:

Image for post

With this mechanism, the B-Tree is always ordered and looking up a value in it is rather cheap.

Now if we wanted to look up the key, 6, we’d compare 6 to values in the root node and see that it’s between 5 and 10. So we’d use the pointer between 5 and 10 to find the node containing 6.

This is an abstraction over Postgres’ implementation but you can already imagine why this is faster than iterating over every number in a table and checking if it equals 6.

This is why B-trees can search, insert and delete in O(logN) time.

Limitations

Almost in all use cases, the power of indexes is noticeable on large amounts of data. This means that the indexes will have to be as big as the actual data tables. Or does it?

Imagine if we are dealing with billions of records. This means that the index tables will have the billions of records stored in an ordered fashion. Okay, PostgreSQL could handle that. But, can you imagine how long would an INSERT command take? Adding the record in the data table will take really long, because the index will have to add the new record in the correct place, to keep the order of the indexes. Due to this limitation, the implementation of the B-Tree index keeps page files, which simply put, are nodes on a big B-Tree data structure.

Syntax:

— Create an index column using btree on the purchase table

— CREATE INDEX <<index_name>> ON <<table_name>> USEING <<index_type>>

#sql #search #postgres #b-tree #indexing

Dexter  Goodwin

Dexter Goodwin

1657258020

Level-tree-index: Tree indexer for Leveldb / Levelup

A streaming tree structure index for leveldb.

Reference every value in your leveldb to its parent, e.g. by setting value.parentKey to the key of the parent, then level-tree-index will keep track of the full path for each value, allow you to look up parents and children, stream the entire tree or a part thereof and even perform streaming search queries on the tree.

This is useful for implementing e.g. nested comments.

level-tree-index works for all keyEncodings. It works for the json valueEncoding automatically and works for other valueEncodings if you provide custom functions for the opts.pathProp and opts.parentProp options. level-tree-index works equally well with string and buffer paths.

level-tree-index automatically keeps the tree updated as you add, change or delete from the database.

Usage

// db contains your data and idb is used to store the index
var tree = treeIndexer(db, idb);

db.put('1', {name: "foo"}, function(err) {
  if(err) fail(err);

  db.put('2', {parentKey: '1', name: "bar"}, function(err) {
    if(err) fail(err);

    db.put('3', {parentKey: '2', name: "baz"}, function(err) {
      if(err) fail(err);

      // wait for index to finish building
      setTimeout(function() {

        // stream child-paths of 'foo' recursively
        var s = tree.stream('foo');

        s.on('data', function(data) {
          console.log(data.path, data.key, data.value);
        });

      }, 500);
    });
  });
});

Read the unit tests in tests/ for more.

API

treeIndex(db, idb, [opts]) [constructor]

  • db: Your database to be indexed
  • idb: Database to use for storing the tree index

opts:

pathProp: 'name' // property used to construct the path
parentProp: 'parentKey' // property that references key of parent
sep: 0x1f, // path separator. can be string or unicode/ascii character code
pathArray: false, // for functions that output paths, output paths as arrays
ignore: false, // set to a function to selectively ignore 
listen: true, // listen for changes on db and update index automatically
uniquefy: false, // add uniqueProp to end of pathProp to ensure uniqueness
uniqProp: 'unique', // property used for uniqueness
uniqSep: 0x1e, // like `sep` but separates pathProp from uniqProp
levelup: false // if true, returns a levelup instance instead
orphanPath: 'orphans' // parent path of orphans

Both pathProp and parentProp can be either a string, a buffer or a function.

If a function is used then the function will be passed a value from your database as the only argument. The pathProp function is expected to return a string or buffer that will be used to construct the path by joining multiple returned pathProp values with the opts.sep value as separator. The parentProp function is expected to return the key in db of the parent.

opts.sep can be a buffer of a string and is used as a separator to construct the path to each node in the tree.

opts.ignore can be set to a function which will receive the key and value for each change and if it returns something truthy then that value will be ignored by the tree indexer, e.g:

Setting orphanPath to a string, buffer or array will cause all orphaned rows to have orphanPath as their parent path. Setting orphanPath to null will cause orphaned rows to be ignored (not indexed). An orphan is defined as a row with its parentProp set to a non-falsy value but where the referenced parent does not exist in the database. This can happen e.g. if a parent is deleted but its children are left in the database.

// ignore items where the .name property starts with an underscore
ignore: function(key, value) {
  if(typeof value === 'object') {
    if(typeof value.name === 'string') {
      if(value.name[0] === '_') {
        return true;
      }     
    }
  }
  return false;
} 

If opts.listen is true then level-tree-index will listen to operations on db and automatically update the index. Otherwise the index will only be updated when .put/.del/batch is called directly on the level-tree-index instance. This option is ignored when opts.levelup is true.

If opts.levelup is true then instead of a level-tree-index instance a levelup instance will be returned with all of the standard levelup API + the level-tree-index API. All calls to .put, .del or .batch will operate on the database given as the db argument and only call their callbacks once the tree index has been updated.

Limitations when using levelup:true:

  • Chained batch mode is not implemented.
  • It is currently not possible not to wait for the tree index to update so it will take longer before the .put, .del and .batch callbacks are called.
  • Key and value encoding happens before the data gets to level-tree-index so opts.pathProp and opts.parentProp must be set to functions and if you're using valueEncoding:'json' then those functions will receive the stringified json data.

See tests/levelup.js for how to use the levelup:true mode.

.getRoot(cb)

Get the path and key of the root element. E.g:

tree.getRoot(function(err, path, key) {
  console.log("Path of root element:", path);
  console.log("Key of root element:", key);
});

.stream([parentPath], [opts])

Recursively stream descendants starting from parentPath. If parentPath is falsy then the entire tree will be streamed to the specified depth.

Opts:

depth: 0, // how many (grand)children deep to go. 0 means infinite
paths: true, // output the path for each child
keys: true, // output the key for each child
values: true, // output the value for each child
pathArray: undefined, // output paths as arrays
ignore: false, // optional function that returns true for values to ignore
match: null, // Stream only matching elements. A string, buffer or function.
matchAncestors: false, // include ancestors of matches if true
gt: undefined, // specify gt directly, must then also specify lt or lte
gte: undefined, // specify gte directly, must then also specify lt or lte
lt: undefined, // specify lt directly, must then also specify gt or gte
lte: undefined // specify lte directly, must then also specify lt or gte

If parentPath is not specified then .gt/.gte and .lt/.lte must be specified.

opts.depth is currently not usable at the same time as opts.match.

If more than one of opts.paths, opts.keys and opts.values is true then the stream will output objects with these as properties.

opts.ignore can be set to a function. This function will receive whatever the stream is about to output (which depends on opts.paths, opts.keys and opts.values) and if the function returns true then those values will not be emitted by the stream.

opts.match allows for streaming search queries on the tree. If set to a string or buffer it will match any path that contains that string or buffer. If set to a RegEx then it will run a .match on the path with that RegEx (only works for string paths). If set to a function then that function will be called with the path as first argument and with the second argument depending on the values of opts.paths, opts.keys and opts.values, e.g:

match: function(path, o) {
  if(o.value.name.match("cattens")) {
   return true;
  }
  return false;
}

Setting opts.matchAncestors to true modifies the behaviour of opts.match to also match all ancestors of matched elements. Ancestors of matched elements will then be streamed in the correct order before the matched element. This requires some buffering so may slow down matches on very large tree indexes.

When using opts.lt/opts.lte you can use the convenience function .lteKey(key). E.g. to stream all paths that begin with 'foo.bar' you could run:

levelTree.stream({
  gte: 'foo.bar',
  lte: levelTree.lteKey('foo.bar')
});

Keep in mind that the above example would also return paths like 'foo.barbar'.

.lteKey(key)

Convenience function that, according to leveldb alphabetical ordering, returns the last possible string or buffer that begins with the specified string or buffer.

.parentStream(path, [opts])

Stream tree index ancestor paths starting from path. Like .stream() but traverses ancestors instead of descendants.

Opts:

height: 0, // how many (grand)children up to go. 0 means infinite
includeCurrent: true, // include the node specified by path in the stream 
paths: true, // output the path for each child
keys: true, // output the key for each child
values: true, // output the value for each child
pathArray: undefined, // output paths as arrays

.parents(path, [opts], cb)

Same as .parentStream but calls back with the results as an array.

.getFromPath(path, cb)

Get key and value from path.

Callback: cb(err, key, value)

.path(key, [opts], cb)

Get tree path given a key.

opts.pathArray: undefined // if true, split path into array 

Callback: cb(err, path)

.parentFromValue(value, cb)

Get parent value given a value.

Callback: cb(err, parentValue)

.parentPath(key, [opts], cb)

Get parent path given a key.

opts.pathArray: undefined // if true, split path into array

Callback: cb(err, parentPath)

.parentPathFromValue(key, [opts], cb)

Get parent path given a value.

opts.pathArray: undefined // if true, split path into array

Callback: cb(err, parentPath)

.parentFromPath(path, cb)

Get parent value given a path.

Callback: cb(err, parentValue)

.parentPathFromPath(path, [opts], cb)

Get parent path given a path.

opts.pathArray: undefined // if true, split path into array

Note: this function can be called synchronously

Callback: cb(err, parentPath)

.children(path, [opts], cb)

Get array of children given a value.

Same usage as .stream but this version isn't streaming.

Callback: cb(err, childArray)

.childrenFromKey(path, [opts], cb)

Same as .children but takes a key as input.

Same usage as .stream but this version isn't streaming.

Callback: cb(err, childArray)

.pathStream(parentPath, [opts])

Same as .stream with only opts.paths set to true.

.keyStream(parentPath, [opts])

Same as .stream with only opts.keys set to true.

.valueStream(parentPath, [opts])

Same as .stream with only opts.values set to true.

.clear(cb)

Clear the index. Deletes all of the index's data in the index db.

.build(cb)

Build the index from scratch.

Note: You will likely want to .clear the index first or call .rebuild instead.

.rebuild(cb)

Clear and then build the index.

.put(key, value, [opts], cb)

If you need to wait for the tree index to update after a .put operation then you can use .put directly on the level-tree-index instance and give it a callback. Calling .put this way is much less efficient so if you are planning to use this feature most of the time then you should look into using level-tree-index with the levelup:true option instead.

.del(key, [opts], cb)

Allows you to wait for the tree index to finish building using a callback. Same as .put above but for deletion.

Uniqueness

The way level-tree-index works requires that each indexed database entry has a globally unique path. In other words no two siblings can share the same pathProp.

You might get into a situation where you really need multiple siblings with an identical pathProp. Then you might wonder if you coulds just append e.g. a random string to each pathProp before passing it to level-tree-index and then strip it away again before e.g. showing the data to users.

Well, level-tree-index provides helpers for exactly that. You can set opts.uniquefy to true in the constructor. You will then need database each entry to have a property that, combined with its pathProp, makes it unique. This can be as simple as a long randomly generated string. As with pathProp you will have to inform level-tree-index about this property with uniqProp.

You will then run into the problem that you no longer know the actual path names since they have the uniqueness added. You can either get the actual path name using the synchronous function .getPathName(val) where val is the value from the key-value pair for which you want the path. Or you can call .put or .batch directly on your level-tree-index instance and they will pass your callback a second argument which for .put is the actual path name and for .batch is an array of path names for all put operations.

When uniqefy is turned on any functions returning paths will now be returning paths with the uniqueness data appended. You can use the convenience function .nicify(path) to convert these paths into normal paths without the uniqueness data. For .stream and any functions described as "same as .stream but ..." you can add set opts.nicePaths to true and in you will receive the nicified paths back with each result.

Async quirks

Note that when you call .put, .del or .batch on your database level-tree-index will not be able to delay the callback so you cannot expect the tree index to be up to date when the callback is called. That is why you see the setTimeout used in the usage example above. You can instead call .put, .del or .batch directly on the level-tree-index instance and your callback will not be called until the index has finished building. This works but if opts.listen is set to true then an inefficient and inelegant workaround is used (in order to prevent the change listener from attempting to update the already updated index) which could potentially slow things down.

If you want to wait for the index to update most of the time then you should probably either set opts.listen to false or use the levelup mode by calling the constructor with opts.levelup set to true, though that has its own drawbacks, especially if using valueEncoding:'json'. See the constructor API documentation for more.

Technical explanation

I normal operation (opts.levelup == false) level-tree-index will listen for any changes on your database and update its index every time a change occurs. This is implemented using leveup change event listeners which run after the database operation has already completed.

When running .put or .del directly on level-tree-index the operation is performed on the underlying database then the tree index is updated and then the callback is called. Since we can't turn off the change event listeners for a specific operation, level-tree-index has to remember the operations performed directly through .put or .del on the level-tree-index instance such that the change event listener can ignore them to prevent the tree-index update operation from being called twice. This is done by hashing the entire operation, saving the hash and then checking the hash of each operation picked up by the change event listeners agains the saved hash. This is obviously inefficient. If this feature is never used then nothing is ever hashed nor compared so performance will not be impacted.

ToDo

Before version 1.0

  • Get opts.depth working with opts.match.

Author: Biobricks
Source Code: https://github.com/biobricks/level-tree-index 
License: AGPLv3

#javascript #tree #index #leveldb 

Remove all leaf nodes from a Generic Tree or N-ary Tree

Given a Generic tree, the task is to delete the leaf nodes from the tree.

** Examples:**

Input: 
              5
          /  /  \  \
         1   2   3   8
        /   / \   \
       15  4   5   6 

Output:  
5 : 1 2 3
1 :
2 :
3 :

Explanation: 
Deleted leafs are:
8, 15, 4, 5, 6

Input:      
              8
         /    |    \
       9      7       2
     / | \    |    / / | \ \
    4  5 6    10  11 1 2  2 3
Output:  
8: 9 7 2
9:
7:
2:

**Approach: **Follow the steps given below to solve the problem

  • Take tree into the vector.
  • Traverse the tree and check the condition:
  • If current node is leaf then
  • Delete the leaf from vector
  • Else
  • Recursively call for every child.

Below is the implementation of the above approach:

#data structures #recursion #tree #n-ary-tree #tree-traversal #data analysis

Mitchel  Carter

Mitchel Carter

1597791600

Sum of previous numbers that are greater than current number for given array

Given an array A[], for each element in the array, the task is to find the sum of all the previous elements which are strictly greater than the current element.

Examples:

_Input: _A[] = {2, 6, 4, 1, 7}

_Output: _0 0 6 12 0

Explanation:

For 2 and 6 there is no element greater to it on the left.

For 4 there is 6.

For 1 the sum would be 12.

For 7 there is again no element greater to it.

_Input: _A[] = {7, 3, 6, 2, 1}

Output:_ 0 7 7 16 18_

Explanation:

_For 7 there is no element greater to it on the left. _

For 3 there is 7.

For 6 the sum would be 7.

For 2 it has to be 7 + 3 + 6 = 16.

For 1 the sum would be 7 + 3 + 6 + 2 = 18

Naive Approach: For each element, the idea is to find the elements which are strictly greater than the current element on the left side of it and then find the sum of all those elements.

Below is the implementation of the above approach:

  • C++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Max Element of the Array

**const** **int** maxn = 1000000;

// Function to find the sum of previous

// numbers that are greater than the

// current number for the given array

**void** sumGreater(``**int** ar[], **int** N)

{

// Loop to iterate over all

// the elements of the array

**for** (``**int** i = 0; i < N; i++) {

// Store the answer for

// the current element

**int** cur_sum = 0;

// Iterate from (current index - 1)

// to 0 and check if ar[j] is greater

// than the current element and add

// it to the cur_sum if so

**for** (``**int** j = i - 1; j >= 0; j--) {

**if** (ar[j] > ar[i])

cur_sum += ar[j];

}

// Print the answer for

// current element

cout << cur_sum << " "``;

}

}

// Driver Code

**int** main()

{

// Given array arr[]

**int** ar[] = { 7, 3, 6, 2, 1 };

// Size of the array

**int** N = **sizeof** ar / **sizeof** ar[0];

// Function call

sumGreater(ar, N);

**return** 0;

}

Output:

0 7 7 16 18

_Time Complexity: _O(N2)

_Auxiliary Space: _O(1)

Efficient Approach: To optimize the above approach the idea is to use Fenwick Tree. Below are the steps:

  1. Traverse the given array and find the sum(say total_sum) of all the elements stored in the Fenwick Tree.
  2. Now Consider each element(say arr[i]) as the index of the Fenwick Tree.
  3. Now find the sum of all the elements(say curr_sum) which is smaller than the current element using values stored in Tree.
  4. The value of total_sum – curr_sum will give the sum of all elements which are strictly greater than the elements on the left side of the current element.
  5. Update the current element in the Fenwick Tree.
  6. Repeat the above steps for all the elements in the array.

#arrays #competitive programming #tree #binary indexed tree #bit #segment-tree

Check if all the Nodes in a Binary Tree having common values are at least D distance apart

Given a Binary Tree and an integer D, the task is to check if the distance between all pairs of same node values in the Tree is ? D or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input:_ D = 7 _

                1
              /   \ 
             2     3
            / \   /  \ 
           4   3  4   4

Output:_ Yes _

Explanation:

_The repeated value of nodes are 3 and 4. _

_The distance between the two nodes valued 3, is 3. _

_The maximum distance between any pair of nodes valued 4 is 4. _

Therefore, none of the distances exceed 7

Input:_ D = 1 _

          3
         / \
        3   3
             \
              3

Output:_ No _

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach: **

The idea is to observe that the problem is similar to finding the distance between two nodes of a tree. But there can be multiple pairs of nodes for which we have to find the distance. Follow the steps below:

  1. Perform the Post Order Traversal of the given tree and find the distance between the repeated pairs of nodes.
  2. Find the nodes that are repeated in the tree using unordered_map.
  3. For each repeated node of a particular value, find the maximum possible distance between any pair.
  4. If that distance is > D, print “No”.
  5. If no such node value is found having a pair containing that value, exceeding **D, **then print “Yes”.

#greedy #recursion #searching #tree #binary tree #frequency-counting #postorder traversal #tree-traversal