0% found this document useful (0 votes)
261 views

Tree Data Structure

Tree is a hierarchical data structure where nodes are connected via parent-child relationships. It is acyclic and connected. A tree has a root node, internal nodes, leaf nodes, and child/parent relationships between nodes. There are different types of trees like binary trees where each node has at most 2 children. Traversals like breadth-first, preorder, inorder and postorder visit nodes in different orders. Balanced binary search trees like AVL trees ensure fast lookup by balancing the tree during insertions and deletions.

Uploaded by

sel&nit
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

Tree Data Structure

Tree is a hierarchical data structure where nodes are connected via parent-child relationships. It is acyclic and connected. A tree has a root node, internal nodes, leaf nodes, and child/parent relationships between nodes. There are different types of trees like binary trees where each node has at most 2 children. Traversals like breadth-first, preorder, inorder and postorder visit nodes in different orders. Balanced binary search trees like AVL trees ensure fast lookup by balancing the tree during insertions and deletions.

Uploaded by

sel&nit
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Depth and Height:


The height of a node is the length of the longest downward path to a
leaf from that node. The height of the root is the height of the tree. The depth of a node is
the length of the path to its root.

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.

Binary Search Tree:


A binary tree has a finite set of nodes that are either empty or
consist of exactly one root node and at most two disjoint binary trees (left and right).
- Each node may have at most two children.
- Each node has a value
- The left subtree of a node contains only values less than the node's value
- The right subtree of a node contains only values greater than or equal to the node's value

Traversals:
Traversing a tree is the order in which the elements of the tree are visited.
Each element may be visited only once.

Breadth First Traversal:


A Breadth First Traversal visits all the nodes at a particular
level from left to right and then moves to the next level. The output will be A, B, D, C, E,
H, F, G, H, and I.
Depth First Traversal:
A Depth First Traversal visits nodes throughout the levels until it
reaches the bottom and then starts over with another subtree.

There are a total of 8 depth first traversal methods.


We will talk about three:
 In-order
 Pre-order
 Post-order

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.

Pre Order Traversal:


Pre-order Traversal processes each node as it is visited. First you
visit the root (or parent node), then the left subtree, and then the right subtree
(recursively). The output for this traversal would be A, B, C, D, E, F, G, H, and I.

Post Order Traversal:


Post-order Traversal does not process a node until all of its
children have been visited. First you visit the left subtree, then the right subtree, and then
the root (or parent node) (recursively). The output for this traversal would be C, B, F, G,
E, I, H, D, and A.
AVL Trees (t107)
AVL (Adelson-Velskii and Landis) is a BST with a balance condition. Ensure
that the height of tree is O(log N).
This is a method for balancing a binary tree. Balancing means that we add a
constraint to the tree to ensure that it is as "shallow" or "balanced" as possible.
Note that requiring left and right subtrees to be of same height not good enough.
Also, requiring the subtrees to be of equal height too strong. Only full trees
would comply.
THe AVL tree is a compromise. It is a BST with the condition that at every
node, the height of the left and right subtrees can vary by at most 1. Height
information is kept with each node. With this condition, the depth of any node is
guaranteed to be O(log N).

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)

Conditions 1 and 4 are symmetrical and are handled by "single rotations".


Situations 2 and 3 are also symmetrical, and are handled by "double rotations".
Note that an insertion must cause a violation of the AVL condition two nodes
away from the problem. If insertion at a node caused a problem at that node, it would
have to be in violation anyway.

Consider single rotation


Then we have the situation described in Fig 4.31. In this case, a violation of the
AVL property occured at a node because an insertion was made in an "outside" subtree
(i.e. left subtree of left child, or right subtree of right child). Consider the case when the
insertion was in the left subtree of the left child.

k0 k0

k2
k1

k2
k1
Z

X
Y Z
Y
X

So we have the following elements when the violation is left-left


1. Let k2 be the node that has an AVL violation
2. Let k1 be the child of k2 in the affected subtree.
3. Let k0 be the parent of k2 (if it exists).
4. Let X, Y be the left and right subtrees of k1, Z be the other subtree of k2.

To fix the AVL problem, we:


1. let k1 be the right child of k0 (or the root, if k2 had no parent).
2. let k2 be the right child of k1
3. move Y to the left child position of k2

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.

Work example inserting 3,4,7,2,1,5 in that order.

Consider double rotation


Just want to look at a double rotation case.

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.

The elements of a double rotation (left-right)


1. k3 be the node with a AVL violation, k1 be its child (the root of the subtree with the
problem), and k2 be the child of k1 (the root of that subtree with the problem)
2. move k2 to k3's position
3. k1 and k3 become children of k2
4. the former left and right subtrees of k2 become the right and left subtrees of k1 and
k3 respectively.

15
15

10
8

5 12
5 10

3 8
3 6 12

You might also like