UNIT-5 Tree

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

Unit-5 Tree

Mr. Nilesh Parmar


Assistant Professor, Dept. of Computer Engg.
UVPCE, Ganpat University, Mehsana
Tree
• Definition: A tree is a nonlinear hierarchical data structure that consists
of nodes connected by edges.
• Another definition: A tree consists of a root, and zero or more subtrees
T1, T2, … , Tk such that there is an edge from the root of the tree to the
root of each subtree.
• Node: A node is an entity that contains a
key or value and pointers to its child
nodes.
• Edge: It is the link between any two
nodes.
Why Tree Data Structure?
• 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.
• Real world examples of tree data structure are family tree, hierarchy
of employees in an organization, file system in an operating system
etc.
Basic Terminologies
• Parent Node: The node which is a predecessor of a node is called the
parent node of that node.
• Child Node: The node which is the immediate successor of a node is
called the child node of that node.
• Root Node: The topmost node of a tree or the node which does not have
any parent node is called the root node. A non-empty tree must contain
exactly one root node and exactly one path from the root to all other
nodes of the tree.
• Non-leaf node or Internal node: A node with at least one child is called
Internal Node.
• Leaf Node or External Node: The nodes which do not have any child
nodes are called leaf nodes.
Cntd…
• Ancestor of a Node: Any predecessor nodes on the path of the root to that
node are called Ancestors of that node.
• Descendant: Any successor node on the path from the leaf node to that node.
• Sibling: Children of the same parent node are called siblings.
• Neighbor of a Node: Parent or child nodes of that node are called neighbors
of that node.
• Subtree: Any node of the tree along with its descendant.
• Number of edges: An edge can be defined as the connection between two
nodes. If a tree has N nodes then it will have (N-1) edges. There is only one
path from each node to any other node of the tree.
Cntd..
• Path: It refers to the sequence of nodes along with the edges of a tree.
• Level of a node: The count of edges on the path from the root node to
that node. The root node has level 0. It is same as depth of a node.
• Depth of a node: The depth of a node is defined as the length of the
path from the root to that node. Each edge adds 1 unit of length to the
path. So, it can also be defined as the number of edges in the path from
the root of the tree to the node.
• Height of a node: The height of a node can be defined as the length of
the longest path from the node to a leaf node of the tree.
Properties of a Tree
• Height of the Tree: The height of a tree is the length of the longest path from
the root node to a leaf node. It is also called as depth of a tree.
• Degree of a Node: The total count of subtrees attached to that node is called
the degree of the node. The degree of a leaf node must be 0.
• Degree of a Tree: The degree of a tree is the maximum degree of a node
among all the nodes in the tree.
• Some more properties:
• It has no loop or cycle
• It has hierarchical structure.
Applications of Tree
• 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, B+Trees and T-Trees, which are
variants of the tree structure.
• Compilers use a syntax tree to validate the syntax of every program
you write.
Types of Tree
• General tree
• Binary Tree
• Binary Search Tree
• AVL Tree
• Heap
• Trie
• B Tree & B+ Tree
• Red-Black Tree
• Splay tree
• Treap
• Syntax tree
Basic Operations on Tree
• Create – Create a node/vertex of a tree
• Insert − Inserts data in a tree.
• Delete- Delete one or multiple nodes from Tree
• Search − Searches specific data in a tree to check it is present or not.
• Preorder Traversal – perform Traveling a tree in a pre-order manner in
data structure .
• In order Traversal – perform Traveling a tree in an in-order manner.
• Post order Traversal –perform Traveling a tree in a post-order manner.
Binary Tree
• A binary tree is a tree data structure whose all nodes have at most two
child nodes. These two children are generally referred to as left and
right child respectively.
• Here, each node can have either 0, 1 or 2 children.
• It is commonly used in computer science for efficient storage and
retrieval of data, with various operations such as insertion, deletion,
and traversal.

CHAPTER 5 11
Representation of Binary Tree
There are two primary ways to represent binary trees in memory:
• Linked Node Representation (Linked list representation)
• Array Representation (Sequential representation)
Linked Node Representation
• In linked node representation of a binary tree, structure of node
contains 3 elements-
1) Data item that is the data stored in the node
2) Address of the left child
3) Address of the right child
Array Representation
• Array Representation is useful when the tree is complete (all levels are
fully filled except possibly the last, which is filled from left to right).
In this method:
• The tree is stored in an array.
• For any node at index i:
Left Child located at index=2 * i + 1
Right Child located at index=2 * i + 2
• Root Node: Stored at index 0
• If child node index is known then parent node index is: int ((i-1)/2)
Properties of Binary Tree
• The level or height of root node is 0.
• At each level- i, the maximum number of nodes is 2 i.
• The height of the tree is defined as the longest path from the root node to the leaf
node. The maximum number of nodes at height 3 is equal to (1+2+4+8) = 15. In
general, the maximum number of nodes possible at height h is-
20 + 21 + 22+….+2h = 2h+1 -1.
• Maximum possible internal (non-leaf) nodes are= 2 h-1
• Maximum total possible external (leaf) nodes are= 2 h
• Maximum possible total nodes= internal nodes + external nodes= 2 h+2h-1=2h+1-1
• Minimum number of nodes required for tree with height h is= h+1.
Cntd…
Height of a binary Tree
• Consider N= number of nodes and h= height of a tree
If N is maximum then we can say that,
N= 2h+1 -1
N+1 = 2h+1
Taking log on both the sides,
log2(N+1) = log2(2h+1)
log2(N+1) = h+1
h= log2(N+1) -1
• In general for any value of N, h = log2(N+1) - 1
If N is minimum then we can say that,
N = h+1
h= N-1
Types of Binary Tree
• Full/proper/strict Binary tree: The full binary tree is also known as a
strict binary tree. The tree can only be considered as the full binary tree if
each node must contain either 0 or 2 children. The full binary tree can
also be defined as the tree in which each node must contain 2 children
except the leaf nodes.
• In full Binary Tree:
Number of Leaf nodes = Number of Internal nodes + 1
Cntd…
• Complete Binary tree: The complete binary tree is a tree in which all
the levels are completely filled except the last level. In the last level,
all the nodes must be as left as possible. In a complete binary tree, the
nodes are added from the left.
Cntd…
• Perfect Binary tree: A tree is a perfect binary tree if all the internal
nodes have 2 children, and all the leaf nodes are at the same level.
• Number of nodes= 2h+1-1 where h is height
Cntd…
• Degenerate (Pathological) Binary tree: The degenerate binary tree is
a tree in which all the internal nodes have only one child. Degenerate
trees are inefficient for search, insertion, and deletion operations.
Cntd…
• Skewed Binary Tree: It is a special case of a degenerate tree where all
nodes lean to one side, either left or right.
• Two Types of skewed tree:
• Left-Skewed Tree: Each node has only a left child.
• Right-Skewed Tree: Each node has only a right child.
Cntd…
• Balanced Binary Tree: It tree is a tree in which height of the left and
the right sub-trees of every node may differ by at most 1. AVL and
Red-Black trees are balanced binary tree. The given tree is a balanced
binary tree because the difference between the left subtree and right
subtree is one.
Tree Traversal
• Inorder=9, 8, 4, 2, 10, 5, 10, 1, 6, 3, 13, 12, 7
• Preorder=1, 2, 4, 8, 9, 5, 10, 10, 3, 6, 7, 12, 13

• Preorder Traversal-
• 100 , 20 , 10 , 30 , 200 , 150 , 300
• Inorder Traversal-
• 10 , 20 , 30 , 100 , 150 , 200 , 300
Tree Traversal
• inorder = {4, 8, 2, 5, 1, 6, 3, 7}
• postorder = {8, 4, 5, 2, 6, 7, 3, 1}

• Inorder : { 4, 2, 1, 7, 5, 8, 3, 6 }
Postorder : { 4, 2, 7, 8, 5, 6, 3, 1 }
Binary Search Tree (BST)
• Definition: It is the binary tree in which the value of left node must be
smaller (or equal) than the parent node, and the value of right node
must be greater than the parent node.
• This rule is applied recursively to the left and right subtrees of the
root.
• Advantages of Binary search tree
• Searching an element in the Binary search tree is easy as we always
have a hint that which subtree has the desired element.
• As compared to array and linked lists, insertion and deletion
operations are faster in BST.
Operations on BST
• Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
• Create binary search tree from above elements.
• Operations to be performed on binary search tree:
1) Create
2) Search
3) Insert
4) Delete
Example
• Construct a Binary Search Tree by inserting the following sequence of
numbers: 10, 12, 5, 4, 20, 8, 7, 15 and 13
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage
space. If a binary tree consists of n nodes then n+1 link fields contain
NULL values.
• So in order to effectively manage the space, a method was devised by
Perlis and Thornton in which the NULL links are replaced with special
links known as threads.
• Such binary trees with threads are known as threaded binary trees.
Each node in a threaded binary tree either contains a link to its child
node or thread to other nodes in the tree.
Types of Threaded Binary Tree
• There are two types of threaded Binary Tree:
• One-way threaded Binary Tree
• Two-way threaded Binary Tree
One-way threaded Binary trees
• In one-way threaded binary trees, a thread will appear either in the right or left
link field of a node.
• If it appears in the right link field of a node then it will point to the node which
is Inorder successor. Such trees are called Right threaded binary trees.
• If thread appears in the left field of a node then it will point to the node which
is Inorder predecessor. Such trees are called Left threaded binary trees.
• Left threaded binary trees are used less often as they don't yield the last
advantages of right threaded binary trees.
• In one-way threaded binary trees, the right link field of last node and left link
field of first node contains a NULL. In order to distinguish threads from normal
links they are represented by dotted lines.
Cntd…
Two-way threaded Binary Trees
• In two-way threaded Binary trees, the right link field of a node
containing NULL values is replaced by a thread that points to nodes
inorder successor and left field of a node containing NULL values is
replaced by a thread that points to nodes inorder predecessor.
Cntd…

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy