0% found this document useful (0 votes)
39 views30 pages

Tree 1

Trees are hierarchical data structures where elements are arranged in a branching structure. A tree consists of vertices (nodes) connected by edges, with one root node at the top. There are different ways to traverse a tree, including inorder (left, root, right), postorder (left, right, root), and preorder (root, left, right) traversals. Binary trees restrict nodes to having at most two children, and full binary trees require all nodes except leaves to have two children.

Uploaded by

Salman Qureshi
Copyright
© Attribution Non-Commercial (BY-NC)
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)
39 views30 pages

Tree 1

Trees are hierarchical data structures where elements are arranged in a branching structure. A tree consists of vertices (nodes) connected by edges, with one root node at the top. There are different ways to traverse a tree, including inorder (left, root, right), postorder (left, right, root), and preorder (root, left, right) traversals. Binary trees restrict nodes to having at most two children, and full binary trees require all nodes except leaves to have two children.

Uploaded by

Salman Qureshi
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 30

Trees

Tree is a non-leaner data structure and its elements are arranged in sorted order.
A tree structure means data is organized in branches.
It is useful in manipulating data and to protect hierarchical relationship among data.
A tree structure is one in which items of data are related by edges.
The algebric expression can be represented with a tree. For example:
z= (J – K) / ((L * M) + N)
The operators of the equation have different priority. The priority of the operations can
be represented by the tree structure. The operators of high priority are at low level and
operator and associated operands are represented in the tree structure.

/
+
-
* N
J K L M
BASIC TERMINOLOGY
A tree is a non-empty collection of vertices and edges that satisfies certain
requirements.
A vertex is a simple object (also referred to as a node) that can have a name and
can carry other associated information.
An edge is a connection between two vertices.
A tree may, therefore, be defined as a finite set of zero or more vertices such that
• there is one specially designated vertex called ROOT, and
• the remaining vertices are partitioned into a collection of sub- trees, each of
which is also a tree.
The nodes of a tree have a parent-child relationship.
The root does not have a parent; but each one of the other nodes has a parent
node associated to it. A node may or may not have children. A node that has no
children is called a leaf node.
A line from a parent to a child node is called a branch or an edge. If a tree has n
nodes, one of which is the root then there would be n-1 branches. It follows from
the fact that each branch connects some node to its parent, and every node
except the root has one parent. Nodes with the same parent are called siblings.
K, L, and M are all siblings. B, C, D are also
siblings.
A path in a tree is a list of distinct vertices in
which successive vertices are connected by
edges in the tree. There is exactly one path
between the root and each of the other nodes
in the tree.
Nodes with no children are called leaves, or
terminal nodes. Nodes with at least one child
are sometimes called nonterminal nodes.
Sometime refer to nonterminal nodes as
internal nodes and terminal nodes as external
nodes.
The length of a path is the number of branches
on the path. Further if there is path from n,
then n is predecessor of i and ni is a successor
of n. Also there is a path of length zero from
every node to itself, and there is exactly one
path from the root to each node.
A path from A to K is A-D-G-J-K and the length of this path is 4.
A is predecessor of K and K is successor of A. All the other nodes on the path are also
children of A and ancestors of K.
The depth of any node ni is the length of the path from root to n. Thus the root is at
depth 0(zero). The height of a node n is the longest path from ni to a leaf. Thus all
leaves are at height zero. Further, the height of a tree is same as the height of the root.
F is at height 1 and depth 2. D is at height 3 and depth 1. The height of the tree is 4.
Depth of a node is sometimes also referred to as level of a node.
A set of trees is called a forest; for example, if remove the root and the edges
connecting it from the tree, left with a forest consisting of three trees rooted at A, D
and G.

PROPERTIES OF A TREE
1. Any node can be root of the tree each node in a tree has the property that there is
exactly one path connecting that node with every other node in the tree. Technically,
our definition in which the root is identified, pertain to a rooted tree in which the root
is not identified is called a free tree.
2. Each node, except the root ,has a unique parents and every edge connects a node
to its parents. Therefore, a tree with N nodes has N-1 edges.
BINARY TREES
A Binary tree is a tree which is either empty or consists of a root node and two disjoint
binary trees called the left subtree and right subtree. A binary tree T is depicted with a left
subtree, L(T) and a right subtree R(T).In a binary tree, no node can have more than two children.

PROPERTIES OF BINARY TREES:


1. A binary tree with N internal nodes has maximum
of (N + 1) external nodes : Root is considered as an
internal node.
2. The external path length of any binary tree with N
internal nodes is 2N greater than the internal path
length.
3. The height of a full binary tree with N internal
nodes is about log2N. Note: while every binary tree is
a tree, not every tree is a binary tree.
A full binary tree or a complete binary tree is a binary
tree in which all internal nodes have degree and all
leaves are at the same level.
The degree of a node is the number of non empty
sub trees it has. A leaf node has a degree zero.
TRAVERSALS OF A BINARY TREE
A traversal of a graph is to visit each node exactly once. It is useful in many
applications. For example, in searching for particular nodes. Compilers commonly build
binary trees in the process of scanning, parsing, generating code and evaluation of
arithmetic expression. Let T be a binary tree. There are a number of different ways to
proceed. The four different traversals of T are In order, Post order, Preorder and Level-
by-level traversal.
IN ORDER TRAVERSAL
It follows the general strategy of Left-Root-Right. In this traversal, if T is not empty,
first
traverse (in order) the left sub tree; Then visit the root node of T, and then traverse (in
order) the right sub tree. This is an example of an expression tree
for (A + B*C)-(D*E).
A binary tree can be used to represent
arithmetic expressions if the node value
can be either operators or operand values
and are such that:
. each operator node has exactly two
branches
. each operand node has no branches,
such trees are called expression trees.
Tree, T, at the start is rooted at '_';
Since left(T) is not empty; current T becomes rooted at +;
Since left(T) is not empty; current T becomes rooted at 'A'.
Since left(T) is empty; root i.e. A.
Access T' root i.e. '+'.
perform in order traversal of right(T).
Current T becomes rooted at '*'.
Since left(T) is not empty; Current T becomes rooted at 'B' since left(T) is empty; visit its root i.e.
B; cheek for right(T) which is empty, therefore, move back to parent tree. Visit its root i.e. '*'.
Now in order traversal of right(T)is performed; which would give 'C'. Visit T's root i.e. 'D' and
perform in order traversal of right(T); which would give '* and E'. Therefore, the complete listing
is A+B*C-D*E
Note that expression is in infix notation. The in order traversal produces a(parenthesized) left
expression, then prints out the operator at root and then a(parenthesized) right expression.
The pascal procedure for in order traversal
procedure INORDER (TREE: BINTREE);
begin
if TREE <>nil
then begin
INORDER (TREE^LEFT);
Write ln ( TREE^DATA);
INORDER (TREE ^ RIGHT);
end
End;
Trace of in order traversal of tree
POST ORDER TRAVERSAL
In this traversal first traverse left(T) (in post order); then traverse Right(T) (in post
order); and finally visit root. It is a Left-Right-Root strategy, i.e.
Traverse the left sub tree In Post order.
Traverse the right sub tree in Post order.
Visit the root.
For example, a post order traversal of the tree would be
ABC*+DE*-
it is the postfix notation of the expression (A + (B*C)) -(D*E)
PREORDER TRAVERSAL
In this traversal, visit root first; then recursively perform preorder traversal of Left(T);
followed by pre order. traversal of Right(T) i.e. a Root-Left-Right traversal, i.e.
Visit the root
Traverse the left sub tree preorder.
Traverse the right sub tree preorder.
A preorder traversal of the tree would yield
- +A*BC*DE
It is the prefix notation of the expression (A+ (B*C)) - (D*E)
LEVEL BY LEVEL TRAVERSAL
In this method traverse level-wise i.e. first visit node root at level 'O' i.e. root. There
would be just one. Then visit nodes at level one from left to right. There would be at
most two. Then visit all the nodes at level '2' from left to right and so on. For example
the level by level traversal of the binary tree will yield
ABCDEFGHIJK
BINARY SEARCH TREES (BST)
A Binary Search Tree, BST, is an ordered binary tree T such that either it
is an empty tree or
. each data value in its left sub tree less than the root value,
. each data value. in its right sub tree greater than the root value, and
. left and right sub trees are again binary search trees.
INSERTION IN BST
Inserting a node to the tree:
To insert a node in a BST, check whether the tree already contains any nodes.
If tree is empty, the node is placed in the root node.
If the tree is not empty, then the proper location is found and the added node
becomes either a left or a right child of an existing node.
add-node (node, value) left child point to it,.
{ return (SUCCESS);
if (two values are same) }
}
{
else if (value stored in current node)
duplicate., {
return (FAILURE) if (right child exists)
} {
else if (value) add-node (right child, value);
{ }
if (left child exists) else
{ {
add-node (left child, value); allocate new node and make right child
point to it;
}
return (SUCCESS);
else }
{ }
allocate new node and make }
Let us consider a BST

If we want to insert 5 in the above BST, we first search the tree. If the
key to be inserted is found in tree, we do nothing (since duplicates are
not allowed), otherwise a nil is returned. In case a nil is returned, we
insert the data at the last point traversed. In the above a search
operation will return nil on not finding a right, sub tree of tree rooted at
4. Therefore, 5 must be inserted as a right child of 4.
DELETION OF A NODE
When the node to be deleted is searched in BST. If found, consider the
following possibilities:
(i) If node is a leaf, it can be deleted by making its parent pointing to nil.
The deleted node is now unreferenced and may be disposed off.
(ii) If the node has one child; its parent's pointer needs to be adjusted.
For example for node 1 to be deleted from BST ; the left pointer of node
6 is made to point to child of node 1 i.e. node 4 and the new structure
would be
(iii) If the node to be deleted has two children; then the value is
replaced by the smallest value in the right sub tree or the largest key
value in the left sub tree; subsequently the empty node is recursively
deleted. Consider the BST If the node 6 is to be deleted then first its
value is replaced by smallest value in its right subtree i.e. by 7. Now
delete this empty node.
(iv) Therefore, the final
structure would be
SEARCH FOR A KEY IN BST
To search the binary tree for a particular node, use procedures similar to those used
when adding to it. Beginning at the root node, the current node and the entered key
are compared. If the values are equal success is output.
If the entered value is less than the value in the node, then it must be in the left-child
sub tree. If there is no left-child sub tree, the value is not in the tree i.e. a failure is
reported. If there is a left-child subtree, then it is examined the same way. Similarly, it
the entered value is greater than the value in the current node, the right child is.
searched. For example the path through the tree followed in the search for the key H.
find-key (key value, node)
{
if (two values are same)
{
print value stored in node;
return (SUCCESS);
}
else if (key value stored in current node)
{
if (left child exists)
{
find-key (key-value, left hand);
}
else find-key (key-value, rot child);
{ }
there is no left subtree., else
return (string not found) {
} there is no right subtree;
} return (string not found)
else if (key-value value stored in current node) }
{ }
if (right child exists) }
{

HEIGHT BALANCED TREE


A binary tree of height h is completely balanced or balanced if all leaves occur at
nodes of level h or h-1 and if all nodes at levels lower than h-1 have two. children.
According to this definition, the tree in figure1is balanced, because all leaves occur at
levels 3 considering at level 1 and all node's at levels 1 and 2 have two children.
Intuitively consider a tree to be well balanced if, for each node, the longest paths from
the left of the node are about the same length as the longest paths on the right. More
precisely, a tree is height balanced if, for each node in the tree, the height of the left
subtree differs from the height, of the right subtree by no more than 1. The tree in
figure 2 height balanced, but it is not completely balanced. On the other hand, the
tree in figure 1 is completely balanced tree.
FIGURE 1

FIGURE 2
An almost height balanced tree is called an AYL tree after the Russian mathematician
G. M. Adelson Velskii and E. M. Lendis, who first defined and studied this form of a
tree. AVL Tree may or may not be perfectly balanced.
Let us determine how many nodes might be there in a balanced tree of height h.
The, root will be the only node at level 1;
Each subsequent levels will be as full as possible i.e. 2 nodes at level 2, 4 nodes at level
3 and so on, i.e. in general there will be 21-1 nodes at level 1. Therefore the number of
nodes from level 1 through level h-1 will be
1 + 2 + 22 + 23 + ... + 2h-2 = 2h-1 - 1
The number of nodes at level h may range from a single node to a maximum of 2h-1
nodes. Therefore, the total number of nodes n of the tree may range for (2h-1-1+1) to
(2h-1-1+2h-1) or 2h-1 to 2h -1.
BUILDING HEIGHT BALANCED TREE
Each node of an AVL tree has the Property that the height of the left subtree is either
one more, equal, or one less than the height of the right subtree. When define a
balance factor (BF) as BF = (Height of Right- subtree - Height of Left- subtree)
Further If two subtree are of same height BF = 0
if Right subtree is higher BF = +1
if Left subtree is higher BF = -1
For example balance factor are stated near the nodes in Figure 3. BF of the root node
is zero because height of the right subtree and the left subtree is three. The BF at the
node DIN is - 17because the height of its left subtree is 2 and of right subtree is 1 etc.
Let the values given were in the order BIN, FEM, IND, NEE, LAL, PRI, JIM, AMI, HEM,
DIN and to make the height balanced tree. It would work out as follows:
Begin at the root of the tree since the tree is initially empty

FEM to be added. It would be on the right of the already existing


tree. Therefore
The resulting tree is still height balanced. Now to add IND ie. on the, further right of
FEM.

Since BF of one of the nodes is other than 0, + 1, or -1,


to rebalance the tree. In such a case, when the new
node goes to the longer side, need to rotate the
structure counter clockwise i.e. a rotation is carried
out in counter clockwise direction around the closest
parent of the inserted node with BF = + 2. In this case
We now have a balanced tree. On adding NEE, To regain balance need to rotate the
tree counter clockwise at IND

Since all the nodes have B. F. < + 2 continue


with the next node. Now to add LAL
On adding PRI
On adding JIM, Now rotate the tree at FEN (i.e. the
closest parent to AMI with BF= +2) in
clockwise direction. On rotating it once

The tree is still balanced. Now add AMI


Now HEM is to be added. On doing so the Tree is still balance. Now to add DIN,
structures we will get
B-TREE
A B-tree is a balanced M-way tree. A node of the tree may contain many records or
keys and pointers to children. A B-tree is also known as the balanced sort tree. It finds
its use in external sorting. It is not a binary tree.
To reduce disk accesses, several conditions of the tree must be true;
. the height of the tree must be kept to a minimum,
. there must be no empty subtrees above the leaves of the tree;
. the leaves of the tree must all be on the same level; and
. all nodes except the leaves must have at least some minimum number of children.
B-Tree of order M has following properties:
1. Each node has a maximum of M children and a minimum of M/2 children or any no.
from 2 to the maximum.
2. Each node has one fewer keys than children with a maximum of M-1 keys.
3. Keys are arranged in a defined order within the node. All keys in the subtree to the
left of a key are predecessors of the key and that on the right are successors of the key.
4. When a new key is to be inserted into a full node, the node is split into two nodes,
and the key with the median value is inserted in the parent node. In case the parent
node is the root, a new root is created.
5. All leaves are on the same level i.e. there is no empty subtree above the level of the
leaves. The order imposes a bound on the business of the tree. While root and
terminal nodes are special cases, normal nodes have between M/2 and M children.
For example, a normal node of tree of order 11 has at least 6 more than 11 children.
B-TREE INSERTION
First the search for the place where the new record must be put is done. If the node
can accommodate the new record insertion is simple. The record is added to the node
with an appropriate pointer so that number of pointes remain one more that the
number of records. If the node overflows because there is an upper bound on the size
of a node, splitting is required. The node is spilt into three parts. The middle record is
passed upward and inserted into the parent, leaving two children behind where there
was one before. Splitting may propagate up the tree because the parent into which a
record to be split in its child node, may overflow. Therefore it may also split. If the root
is required to be split, a new root is created with just two children, and the tree grows
taller by one level.
Example: Consider building a B-tree of degree 4 that is a balanced four-way tree
where each node can hold three data values and have four branches. Suppose it needs
to contain the following values. 1 5 6 2 8 11 13 18 20 7 9
The first value 1 is placed in a new node which can accommodate the next two values
also i.e.

when the fourth value 2 is to be added, the


node is split at a median value 5 into two leaf
nodes with a parent at 5.
The following item 8 is to be added in a The remaining items may also be added
leaf node. A search for its appropriate following the above procedure. The final
place puts it in the node containing 6. result is
Next, 11 is also put in the same. So

Now 13 is to be added. But the right leaf


node, where 1-3 finds appropriate plane,
is full. Therefore it is split at median value
8 and this it moves up to the parent. Also
it splits up to make two nodes,

Note that the tree built up in this manner


is balanced, having all of its leaf nodes at
one level. Also the tree appears to grow at
its root, rather than at its leaves as was
the case in binary tree.
A B-tree of order 3 is popularly known as
2-3 tree and that of order 4 as 2-3-4 tree.
B-TREE DELETION
As in the insertion method, the record to be deleted is first searched for. If the record
is in a terminal node, deletion is simple. The record along with an appropriate pointer
is deleted. If the record is not in terminal node, it is replaced by a copy of its successor,
that is, a record with a next, higher value. The successor of any record not at the
lowest level will always be in a terminal node. Thus in all cases deletion involves
removing a record from a terminal node.
If on deleting the record, the new node size is not below the minimum, the deletion is
over. If the new node size is lower than the minimum, an underflow occurs.
Redistribution is carried out if either of adjacent siblings contains more than the
minimum number of records. For redistribution, the contents of the node which has
less than minimum records, the contents of its adjacent sibling which has more the
minimum records, and the separating record from parent are collected. The central
record from this collection is written back to parent. The left and right halves are
written back to the two siblings.
In case the node with less than minimum number of records has no adjacent sibling
that is more than minimally full. Concatenation is used. In this case the node is merged
with its adjacent sibling and the separating record from its parent.
It may be solved by redistribution or concatenation.
1. Delete 'h'. This is a simple deletion 3. Delete 'P'. The node contains less than
minimum numbers of keys required The
slibing can spare a key. So ,t, moves up and
's' moves down.

2. Delete, 'r'. 'r' is not at a leaf node . 4. Deletion 'd'. Again node is less than
Therefore its successor Is, is moved up minimal required. This leaves the parent
'r' moved down and deleted. with only one key. Its slibing cannot
contribute. therefore f,j, m and t are
combined to form the new root. Therefore
the size of tree shrinks by one level.
* ALL THE BEST *

- Manoj

You might also like