A Tree Data Structure
A Tree Data Structure
TREE
A tree is a non linear data structure. The data structure is said to be non-linear if
its elements are arranged in a hierarchical order, in other words data containing
hierarchical relationship among the elements . The tree structure means a data is organize
so that items are related by means of branches. for eg. Tree data structure is commonly
used in investigation of genealogies.
A tree is a finite set of one or more nodes such that there is one special node called as
root node. The remaining nodes are partitioned into and greater than equal to zero disjoint
set, t1,t2,t3,…….tn are called as sub-tree of the root.
A
B C D
E F G H I J
K L M
Terminology:
1. There are 13 nodes in the tree.
2. Each item of information in above tree is single character.
3. Root of tree is A.
4. Number of sub tree of a node is called as degree , so the degree of A is 3,C is 1, F=0
6. Nodes which have degree zero , called as leaf or terminal nodes, for eg. K,L,F,G,M,I,J.
7. The nodes which have degree greater than equal to one is called as non-terminal nodes.
For eg: E,B,C,H,A.
8. The hierarchical relation between elements can be represented by child parent
relationship, for eg., children of D are H,I,J and parent of D is A, where H,I,J are the
siblings of D(children of same parents is known as siblings).
9. D is a grand parent of M.
10. Degree of D is the maximum degree of the node in the tree for eg. Degree of above
tree is 3.
11. Ancestor of any node are all nodes along the path from root to that node for eg.
Ancestor of L is E,B,A.
12. Level of node is defined as initially assuming root is at level no 1.,for eg. If thr node
is at level x then its children are at level x+1.
13. The height or depth of tree is defined as a maximum level of any node in the tree. For
eg., the height or depth of above tree is 4.
14. Forest is a set of n>= disjoint tree.
15. There are some other ways to draw the trees for eg. Above tree can be drawn using
list or shown below:
(A(B(E(K,L),F),C(G),D(X(M)I,J)))
Binary Tree:
A binary tree is a connected acyclic graph such that the degree of each vertex is no more
than 3. It can be shown that in any binary tree, there are exactly two more nodes of
degree one than there are of degree three, but there can be any number of nodes of degree
two. A rooted binary tree is such a graph that has one of its vertices of degree no more
than 2 singled out as the root.
With the root thus chosen, each vertex will have a uniquely defined parent, and up to two
children; however, so far there is insufficient information to distinguish a left or right
child. If we drop the connectedness requirement, allowing multiple connected
components in the graph, we call such a structure a forest.
A tree is defined as a nonempty finite set of labeled nodes such that there is only one
node called the root of the tree, and the remaining nodes are partitioned into subtrees. If
the tree is either empty or each of its nodes has not more than two subtrees, it is called a
binary tree. Hence each node in a binary tree has either no children, one left child, one
right child, or a left child and a right child, each child being the root of a binary tree
called a sub-tree.
Every node (object) in a binary tree contains information divided into two parts. The first
one is proper to the structure of the tree, that is, it contains a key field (the part of
information used to order the elements), a parent field, a leftchild field, and a rightchild
field. The second part is the object data itself. It can be endogenous (that is, data resides
inside the tree) or exogenous (this means that nodes only contains a references to the
object's data). The root node of the tree has its parent field set to nil. Whenever a node
does not have a right child or a left child, then the corresponding field is set to nil.
A binary search tree is a binary tree with more constraints. If x is a node with key value
key[x] and it is not the root of the tree, then the node can have a left child (denoted by
left[x]), a right child (right[x]) and a parent (p[x]). If each node of a tree has the
following Binary Search Tree properties:
For Eg:
AVL Trees
The Concept
These are self-adjusting, height-balanced binary search trees and are named after the
inventors: Adelson-Velskii and Landis. A balanced binary search tree has Theta(lg n)
height and hence Theta(lg n) worst case lookup and insertion times. However, ordinary
binary search trees have a bad worst case. When sorted data is inserted, the binary search
tree is very unbalanced, essentially more of a linear list, with Theta(n) height and thus
Theta(n) worst case insertion and lookup times. AVL trees overcome this problem.
Definitions
The height of a binary tree is the maximum path length from the root to a leaf. A single-
node binary tree has height 0, and an empty binary tree has height -1. As another
example, the following binary tree has height 3.
/ \
3 12
/ / \
2 10 20
/ \
9 11
An AVL tree is a binary search tree in which every node is height balanced, that is, the
difference in the heights of its two subtrees is at most 1. The balance factor of a node is
the height of its right subtree minus the height of its left subtree. An equivalent definition,
then, for an AVL tree is that it is a binary search tree in which each node has a balance
factor of -1, 0, or +1. Note that a balance factor of -1 means that the subtree is left-heavy,
and a balance factor of +1 means that the subtree is right-heavy. For example, in the
following AVL tree, note that the root node with balance factor +1 has a right subtree of
height 1 more than the height of the left subtree. (The balance factors are shown at the top
of each node.)
The basic difference between a binary tree and the threaded binary tree is that in the
binary trees the nodes are null if there is no child associated with it and so there is no way
to traverse back.
But in a threaded binary tree we have threads associated with the nodes i.e they either are
linked to the predecessor or successor in the inorder traversal of the nodes. This helps us
to traverse further or backward in the inorder traversal fashion.
1) Single Threaded :- i.e nodes are threaded either towards its inorder predecessor or
successor.
2) Double threaded:- i.e nodes are threaded towards both the inorder predecessor and
successor.