Presentation is loading. Please wait.

Presentation is loading. Please wait.

Different Tree Data Structures for Different Problems

Similar presentations


Presentation on theme: "Different Tree Data Structures for Different Problems"— Presentation transcript:

1 Different Tree Data Structures for Different Problems

2 Data Structures Data Structures have to be adapted for each problem:
A specific subset of operations (Example: Search is needed, Insert is not) A specific set of requirements or usage patterns (Example: Search occurs frequently, Insert occurs rarely) Special Data Structures Hybrid data structures Augmented data structures

3 Problem 1 Problem: Dictionary, where every key has the same probability to be searched for and the structure must be dynamic (inserts and deletes) Solution: we have seen that we can use Balanced Binary Search Trees

4 Problem 2 Problem: Dictionary, where every key has a different, known probability to be searched for; the structure is not dynamic. Example: A dictionary must contain words: bag, cat, dog, school. It is known that school gets searched 40% of the time, bag 30% of the time, dog 20% of the time and cat 10% of the time. The keys which are more frequent searched for must be near the root. Balancing for reducing height does not help here ! Solution: build the Optimal Binary Search Tree

5 Optimal Binary Search Tree
Balanced BST 0.1 Optimal BST 0.4 cat school 0.3 0.2 0.3 bag dog bag 0.2 0.4 dog school 0.1 cat 0.1 * *1+ 0.2* *2 = 1.3 0.4 * *1+ 0.2* *3 = 1.0

6 Problem 3 Problem: A dictionary structure, used to store a dynamic set of words that may contain common prefixes. Solution: We can exploit the common prefixes of the words, and associate the words with paths in a tree instead of nodes of a tree Solution: Trie trees (Prefix trees, Retrieval trees) Multipath trees If the alphabet has R symbols, a node may have R children

7 Trie Tree B S E E H Y A L E E N L Word set: BE, BY, SEA, SEE, SEEN,
SELL, SHE

8 Problem 4 Problem: we need a data structure having features similar to the search trees (search, insert, delete) but on the secondary storage (disk). What is different with disks? Disks are slow, thus read/write happens in bulk (several items at once, forming a page) Solution: Adapt the BST data structure to a search tree that may contain in a node several keys, up to fill up a page => B-Trees

9 B-Tree 11 15 Minimum degree of a B-tree: t>=2, such that every node, except the root, must have n keys and n+1 children, where t<=n+1<=2t 2-3-4 trees are actually B-trees of min degree 2 The value of t must be in concordance with the size of the disk page

10 B-Trees – formal definition
A B-tree T is a rooted tree (whose root is T.root) having the properties: Every node x has the following attributes: x.n, the number of keys currently stored in node x, the x.n keys themselves, x.key1, x.key2, …, x.keyx.n, stored in nondecreasing order, so that x.key1 <= x.key2 <= … <= x.keyx.n x.leaf , a boolean value that is TRUE if x is a leaf and FALSE if x is an internal node. Each internal node x also contains x.n+1 pointers x.c1, x.c2, … x.cx.n+1 to its children. Leaf nodes have no children, and so their ci attributes are undefined. The keys x.keyi separate the ranges of keys stored in each subtree: if ki is any key stored in the subtree with root x.ci, then k1 <= x.key1 <= k2 <= x.key2 <= … <= X.keyx.n <= kx.n+1 All leaves have the same depth, which is the tree’s height h. Nodes have lower and upper bounds on the number of keys they can contain. We express these bounds in terms of a fixed integer t >=2 called the minimum degree of the B-tree: Every node other than the root must have at least t -1 keys. Every internal node other than the root thus has at least t children. If the tree is nonempty, the root must have at least one key. Every node may contain at most 2t -1 keys. Therefore, an internal node may have at most 2t children.

11 Problem 5 Problem: we need a data structure combining the features of both Binary Search Trees and Heaps, holding nodes that have both a search key and a priority Binary Search Trees and Heaps both come in form of Binary Trees Solution: Treap – a Binary Tree where the nodes satisfy both the BST property and the Heap property ! Why is this structure important ? Building a Treap by using random generated values as priorities is a method for building a balanced binary tree

12 Treap


Download ppt "Different Tree Data Structures for Different Problems"

Similar presentations


Ads by Google