3. What is Tree?
• A tree is a nonlinear hierarchical data structure that consists
of nodes connected by edges.
3
4. Why Tree Data Structure?
4
• Other data structures such as arrays, linked list, stack, and queue
are linear data structures that store data sequentially.
• In order to perform any operation in a linear data structure, the
time complexity increases with the increase in the data size. But,
it is not acceptable in today's computational world.
• Different tree data structures allow quicker and easier access to the
data as it is a non-linear data structure.
5. 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.
• Edge It is the link between any two nodes.
5
6. Tree Terminologies
6
• 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).
• Depth of a Node The depth of a node is the number of edges
from
the root to the node.
• Height of a Tree The height of a Tree is the height of the root
node or the depth of the deepest node.
8. Tree Terminologies
• Degree of a Node The degree of a node is the total number of
branches of that node.
• Forest A collection of disjoint trees is called a forest.
🞄Creating forest from a tree
🞄 You can create a forest by cutting the root of a
tree.
8
9. Types of Tree
9
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
10. Tree Traversal
10
• In order to perform any operation on a tree, you need to reach to
the specific node. The tree traversal algorithm helps in visiting
a required node in the tree.
• Traversing a tree means visiting every node in the tree. You might,
for instance, want to add all the values in the tree or find the largest
one. For all these operations, you will need to visit each node of
the tree.
• Linear data structures like arrays, stacks, queues, and linked
list have only one way to read the data. But a hierarchical
data structure like a tree can be traversed in different ways.
11. Types of Traversal
11
• Depending on the order in which we do this, there can be three
types of traversal.
🞄Inorder traversal
🞄Preorder traversal
🞄Postorder traversal
12. Inorder traversal
12
• First, visit all the nodes in the left subtree
• Then the root node
• Visit all the nodes in the right subtree
🞄 Inorder(root->left)
🞄Display(root->data)
🞄 Inorder(root->right)
13. Preorder traversal
13
• Visit root node
• Visit all the nodes in the left subtree
• Visit all the nodes in the right subtree
🞄display(root->data)
🞄preorder(root->left)
🞄preorder(root->right)
14. Postorder traversal
14
• Visit all the nodes in the left subtree
• Visit all the nodes in the right subtree
• Visit the root node
🞄postorder(root->left)
🞄postorder(root->right)
🞄display(root->data)
15. Tree Applications
15
• Binary Search Trees(BSTs) are used to quickly check whether an
element is present in a set or not.
• Heap is a kind of tree that is used for heap sort.
• A modified version of a tree called Tries is used in modern routers
to store routing information.
• Most popular databases use B-Trees and T-Trees, which are
variants of the tree structure we learned above to store their
data
• Compilers use a syntax tree to validate the syntax of every
program you write.
16. Binary Tree
• A binary tree is a tree data structure in which each parent node
can have at most two children. For example,
16
17. Types of Binary Tree
17
• Full Binary Tree
• Perfect Binary Tree
• Complete Binary Tree
• Balanced Binary Tree
• Degenerate or Pathological Tree
18. Full Binary
Tree
• A full Binary tree is a special type of binary tree in which
every parent node/internal node has either two or no children.
18
19. Perfect Binary
Tree
• A perfect binary tree is a type of binary tree in which every
internal node has exactly two child nodes and all the leaf nodes
are at the same level.
19
20. Complete Binary
Tree
• A complete binary tree is just like a full binary tree, but with
two major differences
🞄 Every level must be completely filled
🞄 All the leaf elements must lean towards the left.
🞄 The last leaf element might not have a right sibling i.e. a complete
binary
tree doesn't have to be a full binary tree.
20
21. Balanced Binary
Tree
• It is a type of binary tree in which the difference between the left
and the right subtree for each node is either 0 or 1.
21
23. Binary Tree Representation
• A node of a binary tree is represented by a class containing a
data part and two objects to other classes of the same type.
🞄 Class Node{ int data; Node left; Node right; };
23
24. Basic Operations
24
• Following are the basic operations of a tree −
🞄 Search − Searches an element in a tree.
🞄 Insert − Inserts an element in a tree.
🞄 Pre-order Traversal − Traverses a tree in a pre-order manner.
🞄 In-order Traversal − Traverses a tree in an in-order manner.
🞄 Post-order Traversal − Traverses a tree in a post-order
manner.
25. Binary Tree Applications
25
• For easy and quick access to data
• In router algorithms
• To implement heap data structure
• Syntax tree