Professional Documents
Culture Documents
Trees
Trees
CO:4 Make use of linear data structures and nonlinear data structures
solving real-time applications.
Non-Linear Data Structure
• In linear data structures, data elements are organized sequentially and therefore
they are easy to implement in the computer’s memory.
• In nonlinear data structures, a data element can be attached to several other data
elements to represent specific relationships that exist among them.
Trees
• A tree is a finite nonempty set of
elements. Computers”R”U
• It is an abstract model of a hierarchical s
structure.
• consists of nodes with a parent-child
relation. Sale Manufacturin R&
• Applications: s g D
– Organization charts
– File systems
– Programming environments U Internationa Laptop Desktop
S l s s
A •Number of nodes
•Height
B C •Root Node
•Leaves
•Interior nodes
D E F •Ancestors of H
•Descendants of B
•Siblings of E
•Right subtree of A
G
•Degree of this tree
H I
Binary Tree
• A tree in which every node can have a maximum of two children is called as Binary Tree.
Binary Tree variants
In a Full Binary, the number of leaf nodes is the number of internal nodes plus 1
L=I+1
Where L = Number of leaf nodes, I = Number of internal nodes
Complete Binary Tree
• A Binary Tree is a complete Binary Tree if all levels are completely filled except
possibly the last level and the last level has all keys as left as possible.
• Number of nodes = 2d+1 – 1
• Number of leaf nodes = 2d
• Where, d – Depth of the tree
Perfect Binary Tree
• A Binary tree is Perfect Binary Tree in which all internal nodes have two children
and all leaves are at the same level.
A Perfect Binary Tree of height h (where height is the number of nodes on the path from
the root to leaf) has 2h – 1 node.the
Balanced Binary Tree
• A balanced binary tree is a binary tree of height h such that the height of any
node’s right subtree and left subtree differ no more than 1. So it doesn't say
anything about it having to be completed from left to right.
Left-Skewed and Right-Skewed Trees
• Binary tree has only left sub trees - Left Skewed Trees
• Binary tree has only right sub trees - Right Skewed Trees
Binary Tree Representation
• To represent a binary tree of depth ‘d' using array representation, we need one
dimensional array with a maximum size of 2d+1 - 1.
Sequential representation
• Advantages:
– Direct access to all nodes (Random access)
• Disadvantages:
– Height of tree should be known
– Memory may be wasted
– Insertion and deletion of a node is difficult
List representation
• Advantages:
– Height of tree need not be known
– No memory wastage
– Insertion and deletion of a node is done without affecting other nodes
• Disadvantages:
– Direct access to node is difficult
– Additional memory required for storing address of left and right node
Linked list representation
Binary tree traversal
• Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only
one logical way to traverse them, trees can be traversed in different ways. Following
are the generally used ways for traversing trees.
Inorder Traversal
• Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Uses of Inorder
• In case of binary search trees (BST), Inorder
traversal gives nodes in non-decreasing order.
• To get nodes of BST in non-increasing order, a
variation of Inorder traversal where Inorder
traversal s reversed can be used.
Preorder Traversal
• Algorithm Preorder(tree)
1. Visit the root
2.Traverse the left subtree, i.e., call Inorder(left-subtree)
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Uses of preorder
• Preorder traversal is used to create a copy
of the tree.
• Preorder traversal is also used to get prefix
expression on of an expression tree.
Postorder Traversal
• Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Traverse the right subtree, i.e., call Inorder(right-subtree)
3. Visit the root