0% found this document useful (0 votes)
47 views50 pages

Tree

A tree is recursively defined as a set of nodes where one node is designated as the root and the remaining nodes are partitioned into sub-trees. A binary tree is a data structure where each node has at most two children. It can be used to store elements hierarchically and for operations like searching. Common terminology includes root, leaf nodes, ancestors, descendants, sub-trees, and paths between nodes.

Uploaded by

soumaadak.23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views50 pages

Tree

A tree is recursively defined as a set of nodes where one node is designated as the root and the remaining nodes are partitioned into sub-trees. A binary tree is a data structure where each node has at most two children. It can be used to store elements hierarchically and for operations like searching. Common terminology includes root, leaf nodes, ancestors, descendants, sub-trees, and paths between nodes.

Uploaded by

soumaadak.23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Tree

Tree

• A tree is recursively defined as a set of one or more nodes where one


node is designated as the root of the tree and all the remaining nodes
can be partitioned into non-empty sets each of which is a sub-tree of
the root.
node A is the root node; nodes B, C, and D are children of the root node
and form sub-trees of the tree rooted at node A.
Basic Terminology

• Root node: The root node R is the topmost node in the tree.
If R = NULL, then it means the tree is empty.
• Sub-trees: If the root node R is not NULL, then the trees T1 , T2 , and
T3 are called the sub-trees of R.
• Leaf node: A node that has no children is called the leaf node or the
terminal node. Path A sequence of consecutive edges is called a path.
For example, the path from the root node A to node I is given as, I leaf
node.
• Ancestor node: An ancestor of a node is any predecessor node on the
path from root to that node. The root node does not have any
ancestors. In the tree given in previous slide nodes A, C, and G are the
ancestors of node K.
Basic Terminology

• Descendant node: A descendant node is any successor node on any path


from the node to a leaf node. Leaf nodes do not have any descendants. In the
tree given nodes C, G, J, and K are the descendants of node A.
• Level number: Every node in the tree is assigned a level number in such a way
that the root node is at level 0, children of the root node are at level number
1. Thus, every node is at one level higher than its parent. So, all child nodes
have a level number given by parent’s level number + 1.
• Degree: Degree of a node is equal to the number of children that a node has.
The degree of a leaf node is zero.
• In-degree: In-degree of a node is the number of edges arriving at that node.
• Out-degree: Out-degree of a node is the number of edges leaving that node
TYPES OF TREES

1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees
General Trees

• General trees are data structures that store elements hierarchically.


The top node of a tree is the root node and each node, except the
root, has a parent. A node in a general tree (except the leaf nodes)
may have zero or more sub-trees. General trees which have 3 sub-
trees per node are called ternary trees. However, the number of sub-
trees for any node may be variable. For example, a node can have 1
sub-tree, whereas some other node can have 3 sub-trees
• General trees can be represented as ADTs, there is always a problem
when another sub-tree is added to a node that already has the
maximum number of sub-trees attached to it. Even the algorithms for
searching, traversing, adding, and deleting nodes become much more
complex as there are not just two possibilities for any node but
multiple possibilities.
Forests
• A forest is a disjoint union of trees. A set of disjoint trees (or forests) is
obtained by deleting the root and the edges connecting the root node
to nodes at level 1. We have already seen that every node of a tree is
the root of some sub-tree. Therefore, all the sub-trees immediately
below a node form a forest.
Forests
• A forest can also be defined as an ordered set of zero or more general
trees. While a general tree must have a root, a forest on the other
hand may be empty because by definition it is a set, and sets can be
empty.
Binary Trees
• A binary tree is a data structure that is defined as a collection of elements called
nodes. In a binary tree, the topmost element is called the root node, and each node
has 0, 1, or at the most 2 children.
• A node that has zero children is called a leaf node or a terminal node. Every node
contains a data element, a left pointer which points to the left child, and a right
pointer which points to the right child. The root element is pointed by a 'root' pointer.
• If root = NULL, then it means the tree is empty. Figure shows a binary tree. In the
figure, R is the root node and the two trees T1 and T2 are called the left and right sub-
trees of R. T1 is said to be the left successor of R. Likewise, T2 is called the right
successor of R
Binary Trees
• A binary tree is recursive by definition as every node in the tree
contains a left sub-tree and a right sub-tree. Even the terminal nodes
contain an empty left sub-tree and an empty right sub-tree. Look at
Fig. nodes 5, 8, 9, 10, 11, and 12 have no successors and thus said to
have empty sub-trees.
Terminology
• Parent If N is any node in T that has left successor S1 and right successor S2, then N is called the parent of
S1 and S2. Correspondingly, S1 and S2 are called the left child and the right child of N. Every node other
than the root node has a parent.
• Level number: Every node in the binary tree is assigned a level number. The root node is defined to be at
level 0. The left and the right child of the root node have a level number 1. Similarly, every node is at one
level higher than its parents. So all child nodes are defined to have level number as parent's level number
+ 1.
• Degree of a node: It is equal to the number of children that a node has. The degree of a leaf node is zero.
For example, in the tree, degree of node 4 is 2, degree of node 5 is zero and degree of node 7 is 1.
• Sibling All nodes that are at the same level and share the same parent are called siblings (brothers). For
example, nodes 2 and 3; nodes 4 and 5; nodes 6 and 7; nodes 8 and 9; and nodes 10 and 11 are siblings
Binary Trees
• Leaf node: A node that has no children is called a leaf node or a terminal node.
The leaf nodes in the tree are: 8, 9, 5, 10, 11, and 12.
• Similar binary trees: Two binary trees T and T’ are said to be similar if both
these trees have the same structure. Figure shows two similar binary trees.
• Copies Two binary trees: T and T’ are said to be copies if they have similar
structure and if they have same content at the corresponding nodes. Figure
shows that T’ is a copy of T.
Binary Trees
• Edge: It is the line connecting a node N to any of its successors. A binary tree of n nodes
has exactly n – 1 edges because every node except the root node is connected to its
parent via an edge.
• Path: A sequence of consecutive edges. For example, the path from the root node to the
node 8 is given as: 1, 2, 4, and 8.
• Depth: The depth of a node N is given as the length of the path from the root R to the
node N. The depth of the root node is zero.
• Height: of a tree It is the total number of nodes on the path from the root node to the
deepest node in the tree. A tree with only a root node has a height of 1.
Binary Trees

• A binary tree of height h has at least h nodes and at most 2h – 1 nodes. This
is because every level will have at least one node and can have at most 2
nodes.
• So, if every level has two nodes then a tree with height h will have at the
most 2h – 1 nodes as at level 0, there is only one element called the root.
• The height of a binary tree with n nodes is at least log2 (n+1) and at most n.
• In-degree/out-degree of a node: It is the number of edges arriving at a
node. The root node is the only node that has an in-degree equal to zero.
Similarly, out-degree of a node is the number of edges leaving that node.
• Binary trees are commonly used to implement binary search trees,
expression trees, tournament trees, and binary heaps.
Complete Binary Trees

• A complete binary tree is a binary tree that satisfies two properties.


First, in a complete binary tree, every level, except possibly the last, is
completely filled. Second, all nodes appear as far left as possible.
• In a complete binary tree Tn , there are exactly n nodes and level r of T
can have at most 2r nodes.
Complete Binary Trees
• The formula can be given as—if K is a parent node, then its left child
can be calculated as 2 × K and its right child can be calculated as 2 × K
+ 1. For example, the children of the node 4 are 8 (2 × 4) and 9 (2 × 4
+ 1). Similarly, the parent of the node K can be calculated as | K/2 |.
Given the node 4, its parent can be calculated as | 4/2 | = 2.

• The height of a tree Tn having exactly n nodes is given as:


Hn = | log2 (n + 1) |
Extended Binary Trees
• A binary tree T is said to be an extended binary tree (or a 2-tree) if
each node in the tree has either no child or exactly two children.
• In an extended binary tree, nodes having two children are called
internal nodes and nodes having no children are called external
nodes.
ADT of binary trees
struct node {
struct node *left;
int data;
struct node *right;
};
ADT of binary tree

• A binary tree is a structure of nodes of the same type. Each node is the root
of a sub-tree, having a left and right child which are binary trees. Each node
has a key field that identifies the entry.
• Operations:
init_t(t) – initialize an empty tree.
empty_t(t) – boolean function to return true if the tree t is empty.
height(t) – function to return the height of tree t.
make_root(n) – procedure to make node n the root of a tree.
addleft(t, n) – add node n as a left child of tree t.
addright(t, n) – add node n as a right child of tree t. d
deleteleaf(n) – delete the leaf node n.
Binary tree
typedef struct nt
{ T info;
struct nt *left, *right;
} bintree;
bintree *t, *cur;
bintree *init t() { return NULL; }
int empty_t(bintree *t) { return (t == NULL) ;}
bintree *makeroot(T n) {
bintree *t;
if((t = malloc(sizeof(bintree))) == NULL)
print error(“malloc error”);
else {
t -> info = n;
t -> left =NULL;
t -> right = NULL; }
return t; }
Binary tree

• void addleft ( bintree *t , T n ) {


t -> left = makeroot ( n ); }
void addright ( bintree *t, T n ) {
t -> right = makeroot ( n ); }
int height ( bintree *t) {
if ( empty_t (t) )
return 0;
else
return(1 + max(height(t -> left), height ( t -> right)));
}
Binary tree

void preorder_traverse ( bintree *t ) {


if ( !(empty_t (t))) {
visit (t);
preorder_traverse (t -> left);
preorder_traverse (t ->right);
}
}
Binary Search Trees

• A binary search tree, also known as an ordered binary tree, is a


variant of binary tree in which the nodes are arranged in an order.
TRAVERSING A BINARY TREE

• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way. Unlike linear data structures in
which the elements are traversed sequentially, tree is a nonlinear data
structure in which the elements can be traversed in many different
ways. There are different algorithms for tree traversals. These
algorithms differ in the order in which the nodes are visited.
Pre-order Traversal

• To traverse a non-empty binary tree in pre-order, the following


operations are performed recursively at each node. The algorithm
works by:
Step 1. Visiting the root node,
Step 2. Traversing the left sub-tree, and finally
Step 3. Traversing the right sub-tree.
Pre-order Traversal
In-order Traversal

Step 1. Traversing the left sub-tree,


Step 2. Visiting the root node, and finally
Step 3. Traversing the right sub-tree.
In-order Traversal
Post-order Traversal

Step 1. Traversing the left sub-tree,


Step 2. Traversing the right sub-tree, and finally
Step 3. Visiting the root node.
Post-order Traversal
Binary Search Tree

• The left subtree of a node contains only nodes with keys lesser than the node’s
key.
• The right subtree of a node contains only nodes with keys greater than the
node’s key.
• The left and right subtree each must also be a binary search tree.
• BST) is a data structure that is commonly used to implement efficient searching,
insertion, and deletion operations.
Insert a value in a Binary Search Tree:

• Check the value to be inserted (say X) with the value of the current
node (say val):
• If X is less than val move to the left subtree.
• Otherwise, move to the right subtree.
• Once the leaf node is reached, insert X to its right or left based on the
relation between X and the leaf node’s value.
Algorithm to search for a key in a given Binary Search Tree:

• want to search for the number X, We start at the root. Then:


• We compare the value to be searched with the value of the root.
• If it’s equal we are done with the search if it’s smaller we know that we need
to go to the left subtree because in a binary search tree all the elements in
the left subtree are smaller and all the elements in the right subtree are
larger.
• Repeat the above step till no more traversal is possible
• If at any iteration, key is found, return True. Else False.
Multiway Trees

A multiway tree is a tree that can have more than two children. A multiway tree of order
m (or an m-way tree) is one in which a tree can have m children. As with the other trees
that have been studied, the nodes in an m-way tree will be made up of key fields, in this
case m-1 key fields, and pointers to children.

Here M = 3. So a node can store a maximum of two key values and can contain pointers to
three sub-trees. In our example, we have taken a very small value of M so that the concept
becomes easier for the reader, but in practice, M is usually very large. M-way search tree of
order 3
B tree

• B Tree is a specialized m-way tree that can be widely used for disk
access. A B-Tree of order m can have at most m-1 keys and m
children. One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key values by
keeping the height of the tree relatively small.
B tree properties

• Every node in a B-Tree contains at most m children.


• Every node in a B-Tree except the root node and the leaf node contain
at least m/2 children.
• The root nodes must have at least 2 nodes.
• All leaf nodes must be at the same level.
B+ TREES

• A B+ tree is a variant of a B tree which stores sorted data in a way that


allows for efficient insertion, retrieval, and removal of records, each
of which is identified by a key. While a B tree can store both keys and
records in its interior nodes, a B+ tree, in contrast, stores all the
records at the leaf level of the tree; only keys are stored in the interior
nodes.
• The leaf nodes of a B+ tree are often linked to one another in a linked
list. This has an added advantage of making the queries simpler and
more efficient.
B+ tree
Inserting a New Element in a B Tree
• In a B tree, all insertions are done at the leaf node level. A new value is
inserted in the B tree using the algorithm given below.
Step1: Search the B tree to find the leaf node where the new key value should
be inserted.
Step 2: If the leaf node is not full, that is, it contains less than m–1 key values,
then insert the new element in the node keeping the node’s elements ordered.
Step 3: If the leaf node is full, that is, the leaf node already contains m–1 key
values, then
(a) insert the new value in order into the existing set of keys,
(b) split the node at its median into two nodes (note that the split
nodes are half full), and
(c) push the median element up to its parent’s node. If the parent’s
node is already full, then split the parent node by following the same steps.
B tree of order 5 given below and insert 8, 9, 39, and 4 into it.

Insert 39
Deleting an Element from a B Tree

Step 1: Locate the leaf node which has to be deleted.


Step 2: If the leaf node contains more than the minimum number of key values (more than
m/2 elements), then delete the value.
Step 3: Else if the leaf node does not contain m/2 elements, then fill the node by taking an
element either from the left or from the right sibling.
(a) If the left sibling has more than the minimum number of key values, push its largest key into its
parent’s node and pull down the intervening element from the parent node to the leaf node where the
key is deleted.
(b)Else, if the right sibling has more than the minimum number of key values, push its smallest key into
its parent node and pull down the intervening element from the parent node to the leaf node where
the key is deleted.
Step 4: Else, if both left and right siblings contain only the minimum number of elements,
then create a new leaf node by combining the two leaf nodes and the intervening element
of the parent node (ensuring that the number of elements does not exceed the maximum
number of elements a node can have, that is, m). If pulling the intervening element from
the parent node leaves it with less than the minimum number of keys in the node, then
propagate the process upwards, thereby reducing the height of the B tree.
B tree of order 5 and delete values 93, 201, 180, and 72 from it
Inserting a New Element in a B+ Tree

Step 1: Insert the new node as the leaf node.


Step 2: If the leaf node overflows, split the node and copy the middle
element to next index node.
Step 3: If the index node overflows, split that node and move the
middle element to next index.
Consider the B+ tree of order 4 given and insert 33 in it.
Deleting an Element from a B+ Tree

Step 1: Delete the key and data from the leaves.


Step 2: If the leaf node underflows, merge that node with the sibling
and delete the key in between them.
Step 3: If the index node underflows, merge that node with the
sibling and move down the key in between them.
Consider the B+ tree of order 4 given below and delete node 15 from it.

You might also like