Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Trees

Click to add text


Tree Terminologies
• Node
• A node is an entity that contains a key or value and
pointers to its child nodes.
• The last nodes of each path are called leaf nodes or
external nodes that do not contain a link/pointer to
child nodes.
• The node having at least a child node is called
an internal node.
• Edge
It is the link between any two nodes.
• Root
It is the topmost node of a tree.
• Height of a Node
The height of a node is the number of edges
from the node to the deepest leaf (ie. the
longest path from the node to a leaf node).
Height of node 1 = 2
Longest Path for 1 (1-2-4 and 1-3)
Depth of a Node
• The depth of a node is the number of edges
from the root to the node.
Depth of 5(1-2-5) = 2 Depth o 3=1
• Height of a Tree
• The height of a Tree is the height of the root
node or the depth of the deepest node.
Height of Tree=2
The degree of a node is the total number of
branches of that node.
• Degree of a Node
• The degree of a node is the total number of
branches of that node.
Degree of 2= 2 Degree of 3=0
The node which is a descendant of some node is
called as a child node.
All the nodes except root node are child nodes.
Nodes which belong to the same parent are called
as siblings.
The node which has at least one child is called as
an internal node.
Internal nodes are also called as non-terminal
nodes.
Every non-leaf node is an internal node.
1) Child nodes
2) Siblings
3) Leaf nodes
4) Internal nodes
5) Degree of A, B, F
6) Level of Tree
7) Height of tree
8) Depth of node J and C
9) Height of C, J and E
10) Depth of B and C, E and K
• Tree traversal is the process of visiting each
node in the tree exactly once. Visiting each
node in a graph should be done in a
systematic manner. If search result in a visit to
all the vertices, it is called a traversal. There
are basically three traversal techniques for a
binary tree that are,
• Preorder traversal
• Inorder traversal
• Postorder traversal
• 1) Preorder traversal
• To traverse a binary tree in preorder,
following operations are carried out:
• Visit the root.
• Traverse the left sub tree of root.
• Traverse the right sub tree of root.
Preorder(root,left,right) :
1, 12, 5, 6, 9
• 2) Inorder traversal
• To traverse a binary tree in inorder traversal,
following operations are carried out:
• Traverse the left most sub tree.
• Visit the root.
• Traverse the right most sub tree.
Inorder(left,root,right) :
5, 12, 6, 1, 9
• 3) Postorder traversal
• To traverse a binary tree in postorder
traversal, following operations are carried out:
• Traverse the left sub tree of root.
• Traverse the right sub tree of root.
• Visit the root.
Postorder(left,right,root) :
5, 6, 12, 9, 1
AVl Trees
AVL tree is a self-balancing binary search tree in
which each node maintains extra information
called a balance factor whose value is either -1, 0
or +1. 3-2=

1-2= 0-1=

1-0=

Balance factor = height of left subtree – height of


right subtree
AVl Trees

Figure-2
AVl Trees
100, 50, 150,25,75, 125, 175, 65,85

3,1, 4, 2, 6, 5

14, 15,4, 9,7, 18,3,5, 16, 20, 17

13, 10,15, 5, 11, 16, 4, 6


AVl Trees
100, 50, 150,25,75, 125, 175, 65,85
AVl Trees

You might also like