A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes. TREE ADT Tree is a non- linear data structure which organizes data in recursive hierarchical structure. In tree data structure, every individual element is called as Node . Node in a tree data structure stores the actual data of that particular element and link to next element in hierarchical structure.
STRUCTURE OF A TREE There is one and only one path between every pair of nodes in a tree.
TREE TERMINOLOGY
ROOT NODE In a tree data structure, the first node is called as Root Node . Every tree must have a root node. In any tree, there must be only one root node .
EDGE The connecting link between any two nodes is called as EDGE . In a tree with ' N ' number of nodes there will be a maximum of ' N- 1 ' number of edges.
PARENT NODES
CHILD NODES
ANCESTOR AND DESCENDANT Ancestors: Any node in the path from the current node to the root node. Descendant: Any node in the path from the current node to the leaf nodes.. Ancestor(J) = E,B,A Descendant(C) = G,K,H
SIBLINGS
LEAF NODES The leaf nodes are also called as External Nodes or Terminal Nodes
INTERNAL NODES
DEGREE The Degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree is called as ' Degree of Tree '
LEVEL In a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each step. Level- distance from the root node.
HEIGHT Height of all leaf nodes is 0.
DEPTH Depth of the root node is '0'
PATH Length of a Path is total number of edges in that path.
SUBTREE Each child node with a parent node forms a subtree recursively . Any connected structure below root is a subtree. Collection of subtrees makes a tree.
FOREST A forest is a set of disjoint trees .
COMPARISON
ReoI LeeÅ Noda &aval0 Levei 1
APPLICATION OF TREE
CLASSIFY THE TWO TRAVERSALS PERFORMED IN TREE ADT. A traversal of a tree requires that each node of the tree be visited once A typical reason to traverse a tree is to display the data stored at each node of the tree Traversal Types preorder Inorder Postorder level-order
LEVEL ORDER TRAVERSAL
Traversal Starts at the root node and explores as deep as possible along each branch and backtrack to sibling branch until all nodes are visited. It is a recursive approach for all subtrees.
PREORDER TRAVERSAL Processing order: Root ā Left ā Right Algorithm Visit the root Traverse the left sub tree i.e. call Preorder (left sub tree) Traverse the right sub tree i.e. call Preorder (right sub tree)
EXAMPLE
Traverse the entire tree starting iron the root rode keeping yourself to the left. Preorder Traversal : A , B , D , E , C , F , G
INORDER TRAVERSAL Processing order: Left ā Root ā Right Algorithm Traverse the left sub tree i.e. call Inorder (left sub tree) Visit the root Traverse the right sub tree i.e. call Inorder (right sub tree)
EXAMPLE
POSTORDER TRAVERSAL Processing order: Left ā Right ā Root Algorithm Traverse the left sub tree i.e. call Postorder (left sub tree) Traverse the right sub tree i.e. call Postorder (right sub tree) Visit the root
EXAMPLE
Postorder Traversal Shortcut
EXERCISE
Q1 1. Visit the following tree in a breadth first manner and print all the nodes.
ANSWER
Binary Tree Expression Tree Binary Search Tree Trees AVL Tree B Tree Heap Tree TYPES OF TREES
BINARY TREE AND ITS RELATED OPERATIONS A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. A Binary Tree node contains following parts. Data Pointer to left child Pointer to right child
BINARY TREE A node can have 0,1, and 2 children in a binary tree
CONT.. In a tree each node contains information and may or may not contain links.
PROPERTIES OF BINARY TREE At any level āiā, maximum number of nodes possible is 2^i
Maximum number of nodes of height h= CONT..
Minimum number of nodes of height h=h+1 CONT..
CONT.. Maximum height of the tree if minimum number of nodes is given is (n- 1)
CONT.. Minimum height of the tree if maximum number of possible nodes is given by
TYPES OF BINARY TREE Full/Strict/ Proper Binary Tree Complete Binary Tree Perfect Binary Tree Degenerate Binary Tree Balanced Binary Tree Unbalanced Binary Tree
FULL BINARY TREE It is a binary tree where each node will contains exactly two children except leaf node
Full Binary Tree
COMPLETE BINARY TREE It is a binary tree where all levels are completely filled (except possibly the last level) and the last level has nodes as left as possible
Complete binary tree
PERFECT BINARY TREE It is a binary tree where all internal nodes have 2 children and all leaves are at same level A perfect binary tree is a complete binary tree or full binary tree but vice versa is not true
DEGENERATE BINARY TREE It is a binary tree where all internal nodes are having only one child
BALANCED BINARY TREE Find Balance Factor, B=H L - H R H L - height of left subtree; H R - height of right subtree The height of the left and right subtrees differs by no more than one i.e., its balance factor of every node should be ā1, 0, or +1