Tree Data Structure
Tree Data Structure
Tree: A tree is a widely-used hierarchical data structure that emulates a tree structure
with a set of linked nodes. It is an acyclic and connected graph. It is a non-linear, two
dimensional data structure.
Graph: A graph is connected and it is cyclic in nature. Any node in the graph can be
reached from any other node in more than one path. They are non-hierarchical data
structures.
Terms in a Tree:
Node:
A node may contain a value or a condition or represent a separate data structure or
a tree of its own.
Leaf Node:
Nodes at the bottommost level of the tree are called leaf nodes. Since they
are at the bottommost level, they do not have any children. Here S, L, C, U, Z can be
called as leaf nodes.
Root Node:
The topmost node in a tree is called the root node. Being the topmost node,
the root node will not have parents. It is the node at which operations on the tree
commonly begin. All other nodes can be reached from it by following edges or links.
Here the Root node is R.
R
A T
P U Z
S L C
Child and Parent Nodes:
Each node in a tree has zero or more child nodes, which are
below it in the tree. A node that has a child is called the child's parent node. A node has
at most one parent.
Internal Nodes:
An internal node or inner node is any node of a tree that has child
nodes and is thus not a leaf node.
Subtree:
A subtree is a portion of a tree data structure that can be viewed as a complete
tree in it. The Tree on the left side of the picture that has the nodes A, P, S, L, and C
forms a subtree.
Types of Trees:
Free Tree:
A free tree is a connected acyclic graph. The main difference between a free
tree and a rooted tree is that, there is no Root node in a Free Tree. It is connected, in that
any node in the graph can be reached from any other node by exactly one path.
Binary Tree:
A binary Tree is a tree data structure in which each node has at most 2
children. Typically the child nodes are called left and right. Binary trees are commonly
used to implement binary search trees and binary heaps.
Traversals:
Traversing a tree is the order in which the elements of the tree are visited.
Each element may be visited only once.
In-order Traversal:
An In-order Traversal visits the nodes in ascending order. First you
visit the left subtree, then the root (or parent node), and then the right subtree
(recursively). The output for this traversal would be B, C, A, F, E, G, D, I, and H.
5 7
2 8 2 8
1 4
1 4 7
3 3 5
Two BST's. Only the one on the left is AVL. Why does the right one fail? (Fails
at node 7)
It has been shown that the height of an AVL tree is at most ≈ 1.44 log(N+2)-.328
(although in practice it is log(N+2)+0.25 -- not proven.) Example Fig 4.30, which shows
the the AVL tree of height 9 with the min # of nodes (143).. Note that 144 log
(143+2)-.328 = 10.01.
In Figure 4.30, the left subtree is a min AVL tree of height 7 and the right height
8. This implies that the min # of nodes in an AVL tree of height h is given by:
S(h) = S(h-1) + S(h-2) + 1, where S(0)=0, S(1) =2. Related to Fibonacci
numbers.
If we assume lazy deletion, then all tree operations except insertion can be
done in O(log N) time.
Insertion is problematic, since insertion can ruin the BST property. (If insert a 6
above...) Must restore the balance condition. Note that only those nodes on a path
from the insertion point to the root can have their balance altered by an insertion.
(only those have their subtrees altered.) Therefore, after insertion, follow the path back
to the root updating the height information at each node. If any node get an AVL
violation we solve it as follows.
If the tree is imbalanced at node a, there are four possibilities for how it happened:
1. insertion into left subtree of left child (left-left)
2. insertion into right subtree of left child (left-right)
3. insertion into the left subtree of right child (right-left)
4. insertion into the right subtree of right child (right-right)
k0 k0
k2
k1
k2
k1
Z
X
Y Z
Y
X
5 5
2 15 2 10
1 4 10 20 1 4 8 15
8 12 6 12 20
3 3
The picture shows an example. In the tree on the left, insertion of a "6" causes a
violation of the AVL condition at node 15. Therefore, 15 = k2, k1 = 10, X=8, Y=12,
Z=20.
When the algorithm is applied we get the tree at right. Note that X,Y, and Z are
all at the same level now, so the tree is AVL again.
This is for situation #1. #4 is symmetric.
k0
k2 k3
k2
k1 k1 k3
k1 k1
Z D
k2
k2
X A A B D
X C
Y
Z B C
This case an insertion in Y, the right subtree of the left child of k2, causes an
AVL violation in k2. Fig 4.34 and the two left above shows that single rotation will not
solve the problem.
15
15
10
8
5 12
5 10
3 8
3 6 12