Introduction To Trees: Linear Data Structures
Introduction To Trees: Linear Data Structures
Introduction to Trees
N
0 1 2 3 4
List as an Array List as a Linked List
Disadvantage: Disadvantage:
• Fixed Size • Random Access is
• Expansion Time consuming
• Shrink
• Random Insertion
& Deletion is Time
Consuming
ADVANCED DATA STRUCTURES
Introduction to Trees
Linear organization of
data doesn’t help in
quick retrieval of Go for Non Linear
elements randomly Organization !!!
ADVANCED DATA STRUCTURES
Binary Trees
B C
D E F
G H I
ADVANCED DATA STRUCTURES
Binary Trees: Terminologies
D E F G D E F G N2
ADVANCED DATA STRUCTURES
Binary Trees: Terminologies
• Level of a node
Root has level 0; level of any other node is one more
than its parent
• Depth of a tree
Maximum level of any leaf in the tree (path length from
the deepest leaf to the root)
• Depth of a node
Path length from the node to the root
A Height of Tree: 2
Height of Node A : 2
B C
Height of Node B : 0
D Height of Node C : 1
Height of Node D : 0
ADVANCED DATA STRUCTURES
Binary Trees: Terminologies
A A
B C B C
D E D E
F F G
C Level 1
B Binary Tree
of depth 3
D E F G Level 2
H I J K L M N O Level 3
ADVANCED DATA STRUCTURES
Binary Trees: Terminologies
A A A
B C B C B C
D E D E F G D E F G
F G H I J K H I J
Not Complete Binary Trees Complete Binary Tree
ADVANCED DATA STRUCTURES
Binary Tree Properties
Linked implementation
Here every node will have its own info along with the links
to left child and right child
3 6
2 4 8
1 7 9
ADVANCED DATA STRUCTURES
Binary Tree Traversals
Steps:
• Root Node is visited before the subtrees
• Left subtree is traversed in preorder
• Right subtree is traversed in preorder
F
F B A DC EG I H
B G
A D I
C E H
ADVANCED DATA STRUCTURES
Binary Tree Traversal: Inorder
Steps:
• Left subtree is traversed in Inorder
• Root Node is visited
• Right subtree is traversed in Inorder
F
A B C D E F G HI
B G
A D I
C E H
ADVANCED DATA STRUCTURES
Binary Tree Traversal: Postorder
Steps:
• Left subtree is traversed in postorder
• Right subtree is traversed in postorder
• Root Node is visited
F
A CED BHIG F
B G
A D I
C E H
ADVANCED DATA STRUCTURES
Binary Search Tree - Deletion
Balance Factor
• In a binary tree the balance factor of a node X is defined to
be the height difference
BF(X) := Height(RightSubtree(X)) - Height(LeftSubtree(X))
of its two child sub-trees
• A binary tree is defined to be an AVL tree if the invariant
• BF(X) = {-1,0,1} holds for every node X in the tree
• A node X with BF(X) < 0 is called "left-heavy", one
with BF(X) > 0 is called "right-heavy", and one with
BF(X) = 0 is sometimes simply called "balanced"
https://en.wikipedia.org/wiki/AVL_tree
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: AVL Tree
https://en.wikipedia.org/wiki/AVL_tree
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: AVL Tree
https://en.wikipedia.org/wiki/AVL_tree
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: AVL Tree
Applications
• AVL trees are used extensively in database applications in
which insertions and deletions are fewer but there are
frequent lookups for data required
• It is used in applications that require improved searching
apart from the database applications
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
https://en.wikipedia.org/wiki/Red-black_tree
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
https://en.wikipedia.org/wiki/Red-black_tree
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
https://www.geeksforgeeks.org/red-black-tree-set-2-insert/
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
Algorithm:
Let x be the newly inserted node.
1. Perform standard BST insertion and make the colour of
newly inserted nodes as RED
https://www.geeksforgeeks.org/red-black-tree-set-2-insert/
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
Algorithm contd:
https://www.geeksforgeeks.org/red-black-tree-set-2-insert/
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
Algorithm contd:
https://www.geeksforgeeks.org/red-black-tree-set-2-insert/
ADVANCED DATA STRUCTURES
Self-Balancing Binary Search Trees: Red Black Tree
• Balanced trees are great things – data access and insertion are
both O(log n) fast
• The downside is that implementation can be hard. This is a
legitimate reason not to use a data structure!
• Data should satisfy BST property, Priority should satisfy Max heap property
• If new node is inserted on Left Sub Tree and new node priority is greater than
parent node priority then Right Rotate parent
• If new node is inserted on Right Sub Tree and new node priority is greater than
parent node priority then Left Rotate parent
Note: In the below example, alphabet represents data and digit represents
priority. Insert based on data, rotate (when max heap property fails) based on
priority. Insert A5, B1, C4, H3, P7, T2
ADVANCED DATA STRUCTURES
Treap - Insertion
https://en.wikipedia.org/wiki/Treap
ADVANCED DATA STRUCTURES
Treap - Deletion
Alternatively, a node can be deleted as follows:
1) If node is a leaf, delete it
3) If node has both children as non-NULL, find max of left and right
children
a) If priority of right child is greater, perform left rotation at node
b) If priority of left child is greater, perform right rotation at node
https://en.wikipedia.org/wiki/Trie
ADVANCED DATA STRUCTURES
Trie - Worksheet
Construct a Trie for the words: algorithm, data, datum, all, self, sea, search
ADVANCED DATA STRUCTURES
Trie
TRIE* getnode()
{
TRIE *temp;
temp=(TRIE*)malloc(sizeof(TRIE));
temp->isLeaf=0;
for(int i=0;i<26;i++)
temp->child[i]=NULL;
return temp;
}
ADVANCED DATA STRUCTURES
Trie
void insert_pattern(TRIE *root,char *pattern) {
TRIE *cur=root;
while(*pattern) //pattern[i]!='\0'
{
if(cur->child[*pattern-'a']==NULL)
cur->child[*pattern-'a']=getnode();
cur=cur->child[*pattern-'a'];
pattern++;
}
cur->isLeaf=1;
}
ADVANCED DATA STRUCTURES
Trie
int search(TRIE *root,char *pattern)
{
TRIE *cur=root;int flag=0;
while(*pattern)
{
if(cur->child[*pattern-'a']==NULL)
{
flag=1;
break;
}
cur=cur->child[*pattern-'a'];
pattern++;
}
if(flag || cur->isLeaf==0)
return 0;
return 1;
ADVANCED DATA STRUCTURES
Suffix Tree - Worksheet
Construct a suffix trie for the word banana and show its suffix tree
Suffixes are:
ADVANCED DATA STRUCTURES
Suffix Tree
• In computer science, a suffix tree (also called PAT tree or, in an
earlier form, position tree) is a compressed trie containing all
the suffixes of the given text as their keys and positions in the text
as their values
• Suffix trees allow particularly fast implementations of many
important string operations
• The construction of such a tree for the string S takes time and
space linear in the length of S. Once constructed, several
operations can be performed quickly, for instance locating
a substring in S, locating a substring if a certain number of mistakes
are allowed, locating matches for a regular expression pattern etc
• Suffix trees also provide one of the first linear-time solutions for
the longest common substring problem. These speedups come at a
cost: storing a string's suffix tree typically requires significantly
more space than storing the string itself
ADVANCED DATA STRUCTURES
Suffix Tree - Applications
Suffix trees can be used to solve a large number of string problems that occur in
text-editing, computational biology and other application areas. Primary
applications include:
• String search, in O(m) complexity, where m is the length of the sub-string (but
with initial O(n) time required to build the suffix tree for the string)
• Finding the longest repeated substring
• Finding the longest common substring
• Finding the longest palindrome in a string
• Suffix trees are often used in bioinformatics applications, searching for patterns
in DNA or protein sequences (which can be viewed as long strings of characters)
• Suffix trees are also used in data compression; they can be used to find repeated
data, and can be used for the sorting stage of the Burrows–Wheeler transform
• A suffix tree is also used in suffix tree clustering, a data clustering algorithm used
in some search engines
https://en.wikipedia.org/wiki/Suffix_tree#Applications
ADVANCED DATA STRUCTURES
Van Emde Boas Tree
Preliminary Approaches
Approach 1 : Direct Addressing
Approach 2 : Superimposing a binary tree structure
Approach 3 : Superimposing a tree of constant height
ADVANCED DATA STRUCTURES
Van Emde Boas Tree
https://en.wikipedia.org/wiki/X-fast_trie
ADVANCED DATA STRUCTURES
x-fast trie
• An x-fast trie is a bitwise trie: a binary tree where each subtree
stores values whose binary representations start with a common
prefix
• Each internal node is labeled with the common prefix of the
values in its subtree and typically, the left child adds a 0 to the
end of the prefix, while the right child adds a 1
https://en.wikipedia.org/wiki/X-fast_trie
ADVANCED DATA STRUCTURES
x-fast trie
• All values in the x-fast trie are stored at the leaves
• Internal nodes are stored only if they have leaves in their subtree
• If an internal node would have no left child, it stores a pointer to
the smallest leaf in its right subtree instead, called
a descendant pointer
• Likewise, if it would have no right child, it stores a pointer to the
largest leaf in its left subtree
• Each leaf stores a pointer to its predecessor and successor,
thereby forming a doubly linked list
• Finally, there is a hash table for each level that contains all the An x-fast trie containing the
nodes on that level. Together, these hash tables form the integers 1 (0012), 4 (1002) and
level-search structure (LSS) 5 (1012). Blue edges indicate
descendant pointers
• To guarantee the worst-case query times, these hash tables
should use dynamic perfect hashing or cuckoo hashing
https://en.wikipedia.org/wiki/X-fast_trie
ADVANCED DATA STRUCTURES
x-fast trie
x-fast tries support the following operations
• find(k): Find the element k in trie
• successor(k) and predecessor(k): Successor is the node with
smallest element greater than or equal to k, while predecessor is
largest element smaller than or equal to k in trie
• insert(k): insert the element k
• delete(k): delete the element k
ADVANCED DATA STRUCTURES
y-fast trie
https://en.wikipedia.org/wiki/Y-fast_trie
ADVANCED DATA STRUCTURES
y-fast trie
https://en.wikipedia.org/wiki/Y-fast_trie