SlideShare a Scribd company logo
Unit-V
Tree
 A tree is a finite sets of one or more nodes such that:
 There is a special node called the root of the tree.
 And its remaining nodes are partitioned into number of disjoint
sets, where each set is a tree.
 A tree is a non-linear data structure in which items are arranged
in a sorted sequence. It is used to represent hierarchical
relationship existing amongst nodes. Figure 1 shows an
example of such tree.
 A binary tree T is defined as a finite set of elements, called nodes,
such that:
 T is empty (called the null tree or empty tree), or
 T contains a distinguished node R, called the root of T, and the
remaining nodes of T from two disjoint binary trees T1 and T2
which are called left sub-tree and the right-subtree.
Binary Trees
Basic Terminologies
Unit-VStackStackStackStackStackStack.pptx
 Level: Level is the rank of the hierarchy and root node is
termed as in level 0. If a node is at level l then its child is at
level l + 1 and parent is at level l – 1. This is true for all nodes
except the root node. Level of the root node is zero. In Figure
5, levels of various nodes are shown.
 Height: Maximum number of nodes that is possible in a path
starting from root node to a leaf node is called the height or
depth of tree. For example, in the Figure 5, the longest path is
A-C-H-K and hence the height of this tree is 4. It can be easily
obtained that h = lmax + 1, where h is the height and lmax is
the maximum level of the tree and the maximum number of
nodes possible in binary tree of height h is nmax = 2h
– 1.
 Degree: Maximum number of children that is possible for a
node is known as the degree of a node. For example, the
degree of each node of the tree as shown in the Figure 5 is 2.
 Sibling: The nodes which have the same parent are called
siblings. For example, in the Figure 5, D and E are siblings
Example 1: Consider any algebraic expression E involving only binary operations,
such as
E= (a – b) / ((c*d) + e)
 A binary tree is said to be complete binary tree, if all its levels,
except possibly the last, have the maximum number of
possible nodes and all the nodes at the last level appear as far
as left as possible.
Complete Binary Trees
 A binary tree T is said to be a 2-tree or an extended binary tree if each
node N has either 0 or 2 children. In such a case, the nodes with 2 children
are called internal nodes, and the nodes with 0 children are called external
nodes. The nodes are usually shown in diagrams by using circles for
internal nodes and squares for external nodes.
Extended Binary Trees
 A binary tree represent a hierarchical relationship between a
parent node and child nodes. There are two methods used for
representing this structure.
◦ By using array
◦ By using linked list
Representing Binary Trees in Memory
 Consider a binary tree T as shown in the Figure 9(a), the corresponding
array/sequential representation is shown in Figure 9(b). We require 7
locations in the array TREE even though binary tree T has only 6 nodes.
 Sequential representation is usually inefficient unless, the binary tree T is
complete or nearly complete. For example, if the tree T consists of 11
nodes and depth 5, which means it would require an array with
approximately 26
– 1 = 63 elements.
Array/Sequential Representation of
Binary Trees
Linked Representation of Binary
Trees
 Tree traversal is one of the most common operations performed on tree data
structure. It is a way in which each node in the tree is visited exactly once. There
are three ways of traversing a binary tree T with root R. These are called preorder,
inorder and postorder, are as follows:
a) Preorder (Root – Left Sub-tree – Right Sub-tree)
◦ Process/Visit the root R.
◦ Traverse the left sub-tree of R in preorder.
◦ Traverse the right sub-tree of R in preorder.
b) Inorder (Left Sub-tree – Root – Right Sub-tree)
Traverse the left sub-tree of R in inorder.
Process/Visit the root R.
Traverse the right sub-tree of R in inorder.
c) Postorder (Left Sub-tree –Right Sub-tree – Root)
Traverse the left subtree of R in postorder.
Traverse the right subtree of R in postorder.
Process/Visit the root R
Traversing Binary Trees
 A) The preorder traversal of T processes A, traverses left sub-tree
and traverses right sub-tree. The preorder traversal of left sub-tree
processes the root B and the preorder traversal of right sub-tree
processes the root C. Hence ABC is the preorder traversal of T.
 B) The inorder traversal of T traverses left sub-tree, processes A
and traverses right sub-tree. The inorder traversal of left sub-tree
processes B and the inorder traversal of right sub-tree processes C.
Hence BAC is the inorder traversal of T.
 C) The postorder traversal of T traverses left sub-tree, traverses
right sub-tree, and processes A. The postorder traversal of left sub-
tree processes B, and the postorder traversal of right sub-tree
processes C. Hence BCA is the postorder traversal of T.
Let E denote the following algebraic expression:
[A + (B – C)] * [(D – E) / ( F + G – H)]
a) Preorder Traversal
 Step 1: Proceed/Traverse down the left-most path
rooted at PTR, processing each node N on the path
and pushing each right child RIGHTC(N), if any onto
STACK, The traversing ends after a node N with no
left child LEFTC(N) is processed. PTR is updated
using the assignment PTR := LEFTC|PTR], and the
traversing stops when LEFTC[PTR] = NULL.
 Step 2: Pop and assign to PTR the top element on
STACK. If .PTR ≠ NULL, then return to Step 1;
otherwise Exit;
Traversal Algorithms Using Stacks
Unit-VStackStackStackStackStackStack.pptx
 Inorder Traversal
 Step 1: Proceed down the left-most path rooted at
PTR, pushing each node N onto STACK and stopping
when a node N with no left child is pushed onto
STACK.
 Step 2: Pop and process the nodes on STACK. If
NULL is popped, then Exit. If a node N with a right
child RIGHTC(N) is processed, set PTR = RIGHTC(N)
and return to Step 1.
Unit-VStackStackStackStackStackStack.pptx
 Step 1: Proceed down the left-most path rooted at
PTR. At each node N of the path, push N onto STACK
and, if N has a right child RIGHTC(N), push –
RIGHTC(N) onto STACK.
 Step 2: Pop and process positive nodes on STACK. If
NULL is popped, then Exit. If a negative node is
popped, i.e. If PTR = –N for some node N, then set
PTR = N and return to Step 1.
Postorder Traversal
Unit-VStackStackStackStackStackStack.pptx
 Suppose a binary tree T is maintained in memory by means of a linked list
representation. Sometimes an extra node, called a header node, is added at
the beginning of T. When this extra node is used, the tree pointer variable,
which we will call HEAD instead of ROOT, will point to the header node,
and the left pointer of the header node will point to the root of T. Figure
18(b) shows a linked representation with a header node of binary tree T
shown in Figure 18(a).
Header Nodes
 Consider the linked list representation of a binary tree T
shown in the Figure 18(b). Half of the entries in the pointer
fields LEFTC and RIGHTC will contain null elements. This
space may be more efficiently used by replacing the null
entries by some other type of information.
 We will replace certain null entries by special pointers which
point to nodes higher in the tree. These special pointers are
called threads, and binary trees with such pointers are called
threaded trees
Threads
 There are many ways to thread a binary tree T, but each
threading will correspond to a particu­
lar traversal of T. The
threading will correspond to the inorder traversal of T. There
are two types of threading:
 One way threading
 Two way threading
Types of Threading
Unit-VStackStackStackStackStackStack.pptx
 If the value at node N is greater than every value in the
left subtree of node N and is less than every value in
the right subtree of node N, then the binary tree is
termed binary search tree.
Binary Search Trees
Example 12: Consider the binary search tree T shown
in the Figure 22(a). Suppose ITEM = 20 is given.
Example 13: Consider the following six numbers, which are
inserted in order, into an empty binary search tree:
40, 60, 50, 33, 55, 11
 Case 1: Node N has no children. Then N is deleted
from T by simply replacing the pointer of N in the
parent node PAR(N) by the NULL value.
 Case 2: Node N has exactly one child. Then N is
deleted from T by simply replacing the pointer of N in
PAR(N) by the location of the only one child of N.
 Case 3: Node N has two children. Then N is deleted
from T by first deleting SUCC(N) from T (by using
Case 1 or Case 2) and then replacing node N in T by
the node SUCC(N).
Deleting in a Binary Search
Tree
Unit-VStackStackStackStackStackStack.pptx
Unit-VStackStackStackStackStackStack.pptx
 Suppose H is a complete binary tree with n elements. Then it will be called
as heap tree if it satisfies the following properties.
 1. For each node N in H, the value at N is greater than or equal to the value
at each of the children of N.
 2. N has the value which is greater than or equal to the value of every
successor of N.
 Such a heap tree is called as max heap. Any node N has value less than or
equal to the value of any of the successors of N is called as min heap. In a
max heap the root node contains the largest data value whereas in a min
heap it contains the smallest data value.
Heap Sort
Representation of Heap Tree in
Memory
Inserting into a Heap Tree
 Build a heap H from the following list of
numbers:
44, 30, 50, 22, 60, 55, 77, 55
 Apply the heapsort algorithm to sort the
numbers
Deleting the Root of a Heap
 An extended binary tree or a 2-tree is a binary tree T in
which each node has either 0 or 2 children. The nodes
with zero children are called external nodes, and the
nodes with two children are called internal nodes.
Path Length
 A solution to the problem of finding a binary tree with
minimum weighted external path length was given by D.
Huffman. The tree that can be constructed with the Huffman's
algorithm is guaranteed to be a minimum weighted binary tree
such trees and also called as Huffman tree.
Huffman Algorithm
Unit-VStackStackStackStackStackStack.pptx

More Related Content

PPTX
trees in data structure
PPTX
PPT
Lecture 5 tree.pptx
PPTX
Tree structure and its definitions with an example
PPTX
Why Tree is considered a non-linear data structure?
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PDF
Lecture notes data structures tree
PPTX
Tree.pptx
trees in data structure
Lecture 5 tree.pptx
Tree structure and its definitions with an example
Why Tree is considered a non-linear data structure?
UNIT III Non Linear Data Structures - Trees.pptx
Lecture notes data structures tree
Tree.pptx

Similar to Unit-VStackStackStackStackStackStack.pptx (20)

PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
Unit 6 tree
PDF
Chapter 5_Trees.pdf
PPTX
Introduction to Tree_Data Structure.pptx
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
binary tree.pptx
PPT
PPT
Data Structure and Algorithms Binary Tree
PDF
Binary Trees
PPT
ds 10-Binary Tree.ppt
PPT
Data Structure And Algorithms for Computer Science
PPT
9. TREE Data Structure Non Linear Data Structure
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PPT
PPT
Lecture 5 trees
PPTX
Lecture 8 data structures and algorithms
PPT
358 33 powerpoint-slides_10-trees_chapter-10
PPTX
tree Data Structures in python Traversals.pptx
PPTX
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
PPTX
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
UNIT III Non Linear Data Structures - Trees.pptx
Unit 6 tree
Chapter 5_Trees.pdf
Introduction to Tree_Data Structure.pptx
Trees, Binary Search Tree, AVL Tree in Data Structures
binary tree.pptx
Data Structure and Algorithms Binary Tree
Binary Trees
ds 10-Binary Tree.ppt
Data Structure And Algorithms for Computer Science
9. TREE Data Structure Non Linear Data Structure
Data structures and Algorithm analysis_Lecture4.pptx
Lecture 5 trees
Lecture 8 data structures and algorithms
358 33 powerpoint-slides_10-trees_chapter-10
tree Data Structures in python Traversals.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
Ad

More from nakshpub (7)

PPT
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
PPT
Predicting Stock Market Trends Using Machine Learning and Deep Learning Algor...
PPT
Chapter 5Electronic MailElectronic Mail.ppt
PPT
Chapter 4Application AuthonicationApplication Authonication.ppt
PPT
Chapter 3CrypotgraphyCrypotgraphyCrypotgraphy.ppt
PPT
Chapter 7Web SecurityWeb SecurityWeb Security.ppt
PPTX
UNIT-VIQueueQueueQueueQueueQueueQueueQueueQueue.pptx
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
Predicting Stock Market Trends Using Machine Learning and Deep Learning Algor...
Chapter 5Electronic MailElectronic Mail.ppt
Chapter 4Application AuthonicationApplication Authonication.ppt
Chapter 3CrypotgraphyCrypotgraphyCrypotgraphy.ppt
Chapter 7Web SecurityWeb SecurityWeb Security.ppt
UNIT-VIQueueQueueQueueQueueQueueQueueQueueQueue.pptx
Ad

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
flutter Launcher Icons, Splash Screens & Fonts
PDF
Queuing formulas to evaluate throughputs and servers
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PPTX
web development for engineering and engineering
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Internship_Presentation_Final engineering.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPT
Drone Technology Electronics components_1
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
TE-AI-Unit VI notes using planning model
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
International Journal of Information Technology Convergence and Services (IJI...
CH1 Production IntroductoryConcepts.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
flutter Launcher Icons, Splash Screens & Fonts
Queuing formulas to evaluate throughputs and servers
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
web development for engineering and engineering
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Internship_Presentation_Final engineering.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Drone Technology Electronics components_1
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Simulation of electric circuit laws using tinkercad.pptx
TE-AI-Unit VI notes using planning model
Chapter 6 Design in software Engineeing.ppt
Lesson 3_Tessellation.pptx finite Mathematics
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
International Journal of Information Technology Convergence and Services (IJI...

Unit-VStackStackStackStackStackStack.pptx

  • 2.  A tree is a finite sets of one or more nodes such that:  There is a special node called the root of the tree.  And its remaining nodes are partitioned into number of disjoint sets, where each set is a tree.  A tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst nodes. Figure 1 shows an example of such tree.
  • 3.  A binary tree T is defined as a finite set of elements, called nodes, such that:  T is empty (called the null tree or empty tree), or  T contains a distinguished node R, called the root of T, and the remaining nodes of T from two disjoint binary trees T1 and T2 which are called left sub-tree and the right-subtree. Binary Trees
  • 6.  Level: Level is the rank of the hierarchy and root node is termed as in level 0. If a node is at level l then its child is at level l + 1 and parent is at level l – 1. This is true for all nodes except the root node. Level of the root node is zero. In Figure 5, levels of various nodes are shown.  Height: Maximum number of nodes that is possible in a path starting from root node to a leaf node is called the height or depth of tree. For example, in the Figure 5, the longest path is A-C-H-K and hence the height of this tree is 4. It can be easily obtained that h = lmax + 1, where h is the height and lmax is the maximum level of the tree and the maximum number of nodes possible in binary tree of height h is nmax = 2h – 1.  Degree: Maximum number of children that is possible for a node is known as the degree of a node. For example, the degree of each node of the tree as shown in the Figure 5 is 2.  Sibling: The nodes which have the same parent are called siblings. For example, in the Figure 5, D and E are siblings
  • 7. Example 1: Consider any algebraic expression E involving only binary operations, such as E= (a – b) / ((c*d) + e)
  • 8.  A binary tree is said to be complete binary tree, if all its levels, except possibly the last, have the maximum number of possible nodes and all the nodes at the last level appear as far as left as possible. Complete Binary Trees
  • 9.  A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children. In such a case, the nodes with 2 children are called internal nodes, and the nodes with 0 children are called external nodes. The nodes are usually shown in diagrams by using circles for internal nodes and squares for external nodes. Extended Binary Trees
  • 10.  A binary tree represent a hierarchical relationship between a parent node and child nodes. There are two methods used for representing this structure. ◦ By using array ◦ By using linked list Representing Binary Trees in Memory
  • 11.  Consider a binary tree T as shown in the Figure 9(a), the corresponding array/sequential representation is shown in Figure 9(b). We require 7 locations in the array TREE even though binary tree T has only 6 nodes.  Sequential representation is usually inefficient unless, the binary tree T is complete or nearly complete. For example, if the tree T consists of 11 nodes and depth 5, which means it would require an array with approximately 26 – 1 = 63 elements. Array/Sequential Representation of Binary Trees
  • 12. Linked Representation of Binary Trees
  • 13.  Tree traversal is one of the most common operations performed on tree data structure. It is a way in which each node in the tree is visited exactly once. There are three ways of traversing a binary tree T with root R. These are called preorder, inorder and postorder, are as follows: a) Preorder (Root – Left Sub-tree – Right Sub-tree) ◦ Process/Visit the root R. ◦ Traverse the left sub-tree of R in preorder. ◦ Traverse the right sub-tree of R in preorder. b) Inorder (Left Sub-tree – Root – Right Sub-tree) Traverse the left sub-tree of R in inorder. Process/Visit the root R. Traverse the right sub-tree of R in inorder. c) Postorder (Left Sub-tree –Right Sub-tree – Root) Traverse the left subtree of R in postorder. Traverse the right subtree of R in postorder. Process/Visit the root R Traversing Binary Trees
  • 14.  A) The preorder traversal of T processes A, traverses left sub-tree and traverses right sub-tree. The preorder traversal of left sub-tree processes the root B and the preorder traversal of right sub-tree processes the root C. Hence ABC is the preorder traversal of T.  B) The inorder traversal of T traverses left sub-tree, processes A and traverses right sub-tree. The inorder traversal of left sub-tree processes B and the inorder traversal of right sub-tree processes C. Hence BAC is the inorder traversal of T.  C) The postorder traversal of T traverses left sub-tree, traverses right sub-tree, and processes A. The postorder traversal of left sub- tree processes B, and the postorder traversal of right sub-tree processes C. Hence BCA is the postorder traversal of T.
  • 15. Let E denote the following algebraic expression: [A + (B – C)] * [(D – E) / ( F + G – H)]
  • 16. a) Preorder Traversal  Step 1: Proceed/Traverse down the left-most path rooted at PTR, processing each node N on the path and pushing each right child RIGHTC(N), if any onto STACK, The traversing ends after a node N with no left child LEFTC(N) is processed. PTR is updated using the assignment PTR := LEFTC|PTR], and the traversing stops when LEFTC[PTR] = NULL.  Step 2: Pop and assign to PTR the top element on STACK. If .PTR ≠ NULL, then return to Step 1; otherwise Exit; Traversal Algorithms Using Stacks
  • 18.  Inorder Traversal  Step 1: Proceed down the left-most path rooted at PTR, pushing each node N onto STACK and stopping when a node N with no left child is pushed onto STACK.  Step 2: Pop and process the nodes on STACK. If NULL is popped, then Exit. If a node N with a right child RIGHTC(N) is processed, set PTR = RIGHTC(N) and return to Step 1.
  • 20.  Step 1: Proceed down the left-most path rooted at PTR. At each node N of the path, push N onto STACK and, if N has a right child RIGHTC(N), push – RIGHTC(N) onto STACK.  Step 2: Pop and process positive nodes on STACK. If NULL is popped, then Exit. If a negative node is popped, i.e. If PTR = –N for some node N, then set PTR = N and return to Step 1. Postorder Traversal
  • 22.  Suppose a binary tree T is maintained in memory by means of a linked list representation. Sometimes an extra node, called a header node, is added at the beginning of T. When this extra node is used, the tree pointer variable, which we will call HEAD instead of ROOT, will point to the header node, and the left pointer of the header node will point to the root of T. Figure 18(b) shows a linked representation with a header node of binary tree T shown in Figure 18(a). Header Nodes
  • 23.  Consider the linked list representation of a binary tree T shown in the Figure 18(b). Half of the entries in the pointer fields LEFTC and RIGHTC will contain null elements. This space may be more efficiently used by replacing the null entries by some other type of information.  We will replace certain null entries by special pointers which point to nodes higher in the tree. These special pointers are called threads, and binary trees with such pointers are called threaded trees Threads
  • 24.  There are many ways to thread a binary tree T, but each threading will correspond to a particu­ lar traversal of T. The threading will correspond to the inorder traversal of T. There are two types of threading:  One way threading  Two way threading Types of Threading
  • 26.  If the value at node N is greater than every value in the left subtree of node N and is less than every value in the right subtree of node N, then the binary tree is termed binary search tree. Binary Search Trees
  • 27. Example 12: Consider the binary search tree T shown in the Figure 22(a). Suppose ITEM = 20 is given. Example 13: Consider the following six numbers, which are inserted in order, into an empty binary search tree: 40, 60, 50, 33, 55, 11
  • 28.  Case 1: Node N has no children. Then N is deleted from T by simply replacing the pointer of N in the parent node PAR(N) by the NULL value.  Case 2: Node N has exactly one child. Then N is deleted from T by simply replacing the pointer of N in PAR(N) by the location of the only one child of N.  Case 3: Node N has two children. Then N is deleted from T by first deleting SUCC(N) from T (by using Case 1 or Case 2) and then replacing node N in T by the node SUCC(N). Deleting in a Binary Search Tree
  • 31.  Suppose H is a complete binary tree with n elements. Then it will be called as heap tree if it satisfies the following properties.  1. For each node N in H, the value at N is greater than or equal to the value at each of the children of N.  2. N has the value which is greater than or equal to the value of every successor of N.  Such a heap tree is called as max heap. Any node N has value less than or equal to the value of any of the successors of N is called as min heap. In a max heap the root node contains the largest data value whereas in a min heap it contains the smallest data value. Heap Sort
  • 32. Representation of Heap Tree in Memory Inserting into a Heap Tree
  • 33.  Build a heap H from the following list of numbers: 44, 30, 50, 22, 60, 55, 77, 55  Apply the heapsort algorithm to sort the numbers
  • 34. Deleting the Root of a Heap
  • 35.  An extended binary tree or a 2-tree is a binary tree T in which each node has either 0 or 2 children. The nodes with zero children are called external nodes, and the nodes with two children are called internal nodes. Path Length
  • 36.  A solution to the problem of finding a binary tree with minimum weighted external path length was given by D. Huffman. The tree that can be constructed with the Huffman's algorithm is guaranteed to be a minimum weighted binary tree such trees and also called as Huffman tree. Huffman Algorithm