Data Structure ❑ Tree is a non-linear data structure which organizes Definition data in hierarchical structure and this is a recursive definition.
❑ In linear data structure data is organized in sequential
order and in non-linear data structure data is organized in random order
❑ Tree data structure is a collection of data (Node) which
is organized in hierarchical structure recursively Tree Terminologies 1. Root Tree Terminologies 2. Edge
➢ In a tree with 'N' number of nodes
there will be a maximum of 'N-1' number of edges. Tree Terminologies 3. Parent
➢ In simple words, the node which has a branch
from it to any other node is called a parent node. Tree Terminologies 4. Child Tree Terminologies 5. Siblings Tree Terminologies 6. Leaf
➢ Leaf nodes are also called External
Nodes or 'Terminal' node Tree Terminologies 7. Internal Nodes
In a tree, height of all leaf nodes is '0'.
Tree Terminologies 8. Degree Tree Terminologies 9. Level Tree Terminologies 10. Height
In a tree, depth of the root node is '0'.
Tree Terminologies 11. Depth Tree Terminologies 12. Path Tree Terminologies 13. Sub Tree
• In a tree data structure, each child
from a node forms a subtree recursively.
• Every child node will form a subtree
on its parent node. Exercise From the given Tree, 1. Which is the root node 2. What is the number of edges 3. Which nodes are parent nodes 4. Identify the child nodes 5. Which are the leaf nodes 6. What is the Degree of node B 7. What is the level of nodes I,J and K 8. What is the height of the tree 1.A tree data structure can be represented in two Tree methods Representations 1. List Representation 2. Left Child - Right Sibling Representation • Consider the following tree... • In this representation, we use two types of nodes one for 1. List representing the node with data called 'data node' and another for representing only references called 'reference Representation node’. • We start with a 'data node' from the root node in the tree. Then it is linked to an internal node through a 'reference node' which is further linked to any other node directly. 2. Left Child - Right Sibling Representation
• In this representation, we use a list with
one type of node which consists of three fields namely Data field, Left child reference field and Right sibling reference field. • Data field stores the actual value of a node, left reference field stores the address of the left child and right reference field stores the address of the right sibling node • In this representation, every node's data field stores the actual value of that node. If that node has left a child, then left reference field stores the address of that left child node otherwise stores NULL. If that node has the right sibling, then right reference field stores the address of right sibling node otherwise stores NULL. • A tree in which every node can have a maximum of two children is called Binary Tree.
i. Strictly / Full Binary/Proper Binary Tree
• A binary tree in which every node has either two or zero number of children is called Strictly Binary Tree
ii. Complete/Perfect Binary Tree
• A binary tree in which every internal node has exactly two children and all leaf nodes are at same level is called Complete Binary Tree.
iii. Extended Binary Tree
• The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary Tree. Strictly Binary Tree - used to represent mathematical • A binary tree in which every node has either two or zero number of children is expressions. called Strictly Binary Tree 2. Complete Binary Tree
• In a binary tree, every node can have a maximum
of two children. • But in strictly binary tree, every node should have exactly two children or none • In complete binary tree all the nodes must have exactly two children and at every level of complete binary tree there must be 2level number of nodes. • For example at level 2 there must be 22 = 4 nodes and at level 3 there must be 23 = 8 nodes 3. Extended • The full binary tree obtained by adding dummy Binary Tree nodes to a binary tree is called as Extended Binary Tree. Binary Tree Representations 1. Array Representation of Binary Tree
• In array representation of a binary tree, we use one-
dimensional array (1-D Array) to represent a binary tree. • To represent a binary tree of depth 'n' using array representation, we need one dimensional array with a maximum size of 2n + 1. 2. Linked List Representation of Binary Tree
• We use a double linked list to
represent a binary tree. In a double linked list, every node consists of three fields. • First field for storing left child address, • second for storing actual data • third for storing right child address. Binary Tree Traversals • Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal. There are three types of binary tree traversals. 1.In - Order Traversal ( leftChild - root - rightChild )
I-D-J-B-F-A-G-K-C–H
2. Pre - Order Traversal ( root - leftChild - rightChild )
A-B-D-I-J-F-C-G-K–H
3. Post - Order Traversal ( leftChild - rightChild - root )
I-J-D-F-B-K-G-H-C–A Inorder Traversal: (Left -> Root -> Right) Preorder Traversal: Root -> Left -> Right Postorder Traversal: (Left -> Right -> Root Exercise
Given the following Binary tree
•Perform an in-order traversal (LEFT *ROOT* RIGHT) of the
tree and provide the sequence of nodes visited.
•Perform a pre-order traversal(*ROOT* LEFT RIGHT) of the tree
and provide the sequence of nodes visited.
•Perform a post-order traversal (LEFT RIGHT *ROOT*) of the
tree and provide the sequence of nodes visited. Solution