6 Trees
6 Trees
By
H R Choudhary (Asstt. Professor)
Department of CSE
Engineering College Ajmer
• One special node is called tree root and others nodes are
hierarchically partitioned into disjoint subsets called Subtrees
• Example
A
B C
D
E
H I M
N P S
R
T
Introduction to trees cont..
Properties of tree
• Root: Top or first element in hierarchy
Example A
B
D
E
G
J N
O
Introduction to tree cont..
• Any node say D in given tree is the ancestor of node J and J is the
descendant of D if D is either the father of J or the father of some
ancestor of J.
• Strictly binary trees: All non –leaf nodes having non empty left
and right Subtree.
2
A A
1
B
D B
D
L
T E
G
U J N
Z
V W
Introduction to tree cont..
• A strictly binary tree with M leaves always contains 2m-1 nodes
• Complete binary tree : In a strictly binary tree if all its leaves are at
the level of its depth d. E.g. following is the complete binary tree of
depth 2
• A binary tree can contain at most 2l( 2 the power l) nodes at level
l
• A complete binary tree of depth d contains exactly 2l nodes at
each level between 0 and d. A
B
D
E
G
J N
Introduction to tree cont..
• A complete binary tree of depth d contains total number of
nodes given by the sum of the number of nodes at each level
between 0 and d.
• CBtn=20+ 21+22+…..2d
• CBtn=2d+1 -1
Binary tree representation
• The structure of each node of a binary tree contains three parts
struct node
{
struct node *lcp ; /* points to the left child */
int info; /* info field */
struct node *rcp; /* points to the right child */
}
Binary tree representation cont..
Representation of binary tree.
Array representation
• The nodes in array start storing from index 0 to MAXSIZE
• Root node is at index 0.
• Left child node is at next successive memory location then right
child node
• E.g.
Binary tree of 3 nodes Array
0 representation
A
A B D
1 B D
2 0 1 2
Binary tree representation cont..
In array representation we can identify index number of father , left
child and right child node for any node m, 0 ≤ m ≤ (MAXSIZE)
using
• Left child(lcp) of m: (2m+1)
• Right child(rcp) of m: (2m+2)
• Father of m if m is not equal to 0: floor((m-1)/2)
• The array representation is ideal for the complete binary tree but
not suitable for others
• Waste memory space E.g. Consider left skew binary tree
B A B - E - - -
E 0 1 2 3 4 5 6
Binary tree representation cont..
Linked list representation
• Each node contains the address of the left child and the right child.
• If any node has its left or right child empty then it will have in its
respective link field, a null value.
• A leaf node has null value in both of its links.
D → B → E → A→ F → C → G
Algorithm:
A→ B → D → E → C → F → G
Algorithm:
D→E→B→F→G→C→A
Post-order Traversal
Algorithm:
The value of the key of the left sub-tree is less than the
value of its parent (root) node's key.
We observe that the root node key (27) has all less-valued keys on the
left sub-tree and the higher valued keys on the right sub-tree.
Binary Search Tree(BST)..
Basic Operations:
Following are the basic operations of a tree −
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Node:
Define a node having some data, references to its
left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Binary Search Tree(BST)..
Search Operation:
Whenever an element is to be searched, start searching
from the root node. Then if the data is less than the key
value, search for the element in the left subtree.
Otherwise, search for the element in the right subtree.
Follow the same algorithm for each node.
Binary Search Tree(BST)..
Algorithm:
while(current->data != data){
if(current != NULL) {
printf("%d ",current->data);
Binary Search Tree(BST)..
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
Binary Search Tree(BST)..
//if tree is empty
if(root == NULL)
{
root = tempNode;
}
else
{
current = root;
parent = NULL;
Binary Search Tree(BST)..
while(1) {
parent = current;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
Binary Search Tree(BST)..
B-trees, the keys and the record values are stored in the
internal as well as leaf nodes. In B+ tree records, can be
stored at the leaf node, internal nodes will store the key
values only. The leaf nodes of the B+ trees are also linked
like linked list
B+Trees
This supports basic operations like searching, insertion, deletion. In each node,
the item will be sorted. The element at position i has child before and after it.
So children sored before will hold smaller values, and children present at right
will hold bigger values.
B+Trees
Advantages over B-Tree
l
Records can be fetched in equal number of disk
accesses
l
Height of the tree remains balanced, and less as
compared to B-Trees
l
As the leafs are connected like linked list, we can
search elements in sequential manner also
l
Keys are used for indexing
l
The searching is faster, as data are stored at leaf level
only.
Threaded Binary Trees
We know that the binary tree nodes may have at most two
children. But if they have only one children, or no children,
the link part in the linked list representation remains null.
Using threaded binary tree representation, we can reuse that
empty links by making some threads.
If one node has some vacant left or right child area, that
will be used as thread. There are two types of threaded
binary tree. The single threaded tree or fully threaded
binary tree. In single threaded mode, there are another
two variations. Left threaded and right threaded.
Threaded Binary Trees
In the left threaded mode if some node has no left child, then
the left pointer will point to its inorder predecessor, similarly
in the right threaded mode if some node has no right child,
then the right pointer will point to its inorder successor. In
both cases, if no successor or predecessor is present, then it
will point to header node.
For fully threaded binary tree, each node has five fields.
Three fields like normal binary tree node, another two
fields to store Boolean value to denote whether link of
that side is actual link or thread.
Threaded Binary Trees
Left threaded binary Trees
Threaded Binary Trees
Right threaded binary Trees
Threaded Binary Trees
Fully threaded binary Trees
Thank You