Tree 1
Tree 1
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.
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) }
{
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
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