Students can learn Trees concept in data structures. various types of data structures like binary trees, expression trees, binary search trees and AVL trees are covered in this PPT.
Content of slide
Tree
Binary tree Implementation
Binary Search Tree
BST Operations
Traversal
Insertion
Deletion
Types of BST
Complexity in BST
Applications of BST
Student will be able to learn linear data structures concepts in detailed manner. This PPT comprises of the following topics: LIST ADT, Singly Linked List, Doubly Linked List, Circular Linked List, Applications of Linked List, Applications of Stack and Queue, Tower of Hanoi, Balancing Parenthesis
1) Introduction to Trees.
2) Basic terminologies
3) Binary tree
4) Binary tree types
5) Binary tree representation
6) Binary search tree
7) Creation of a binary tree
8) Operations on binary search tree Trees
Content of slide
Tree
Binary tree Implementation
Binary Search Tree
BST Operations
Traversal
Insertion
Deletion
Types of BST
Complexity in BST
Applications of BST
Student will be able to learn linear data structures concepts in detailed manner. This PPT comprises of the following topics: LIST ADT, Singly Linked List, Doubly Linked List, Circular Linked List, Applications of Linked List, Applications of Stack and Queue, Tower of Hanoi, Balancing Parenthesis
1) Introduction to Trees.
2) Basic terminologies
3) Binary tree
4) Binary tree types
5) Binary tree representation
6) Binary search tree
7) Creation of a binary tree
8) Operations on binary search tree Trees
Students will be able to learn the various kinds of searching, sorting and hashing techniques to handle the data structures efficiently. This PPT contains the following topics: linear search, binary search, insertion sort, selection sort, bubble sort, shell sort, quick sort merge sort, bucket sort, m-way merge sort, polyphase merge sort, hashing techniques like separate chaining, closed chaining or open addressing, linear probing, quadratic probing, double hashing, rehashing and extendible hashing.
A tree is a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear data structures. A tree can be empty with no nodes or a tree is a structure consisting of one node called the root and zero or one or more sub-trees.
↓↓↓↓ Read More:
@ Kindly Follow my Instagram Page to discuss about your mental health problems-
-----> https://instagram.com/mentality_streak?utm_medium=copy_link
@ Appreciate my work:
-----> behance.net/burhanahmed1
Thank-you !
Linked Lists: Introduction Linked lists
Representation of linked list
operations on linked list
Comparison of Linked Lists with Arrays and Dynamic Arrays
Types of Linked Lists and operations-Circular Single Linked List, Double Linked List, Circular Double Linked List
In computer science, tree traversal (also known as tree search) is a form of graph traversal and refers to the process of visiting (checking and/or updating) each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well.
Frequent Pattern growth algorithm provides better performance than Apriori algorithm. This approach used to detect frequent itemsets in database. It has two phase. In first phase, it constructs a suffix tree and in next, it starts mining recursively.
The recursion process is shown in details in presentation with figure.
Students will be able to learn the concepts of advanced trees like Splay Tree, B Tree, Red Black Tree, Priority Queue or Heap. In Heap Data Structure the following methods are covered: Binary Heap, d-heap, Leftist Heap and Skew Heap.
Students will be able to learn the various kinds of searching, sorting and hashing techniques to handle the data structures efficiently. This PPT contains the following topics: linear search, binary search, insertion sort, selection sort, bubble sort, shell sort, quick sort merge sort, bucket sort, m-way merge sort, polyphase merge sort, hashing techniques like separate chaining, closed chaining or open addressing, linear probing, quadratic probing, double hashing, rehashing and extendible hashing.
A tree is a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear data structures. A tree can be empty with no nodes or a tree is a structure consisting of one node called the root and zero or one or more sub-trees.
↓↓↓↓ Read More:
@ Kindly Follow my Instagram Page to discuss about your mental health problems-
-----> https://instagram.com/mentality_streak?utm_medium=copy_link
@ Appreciate my work:
-----> behance.net/burhanahmed1
Thank-you !
Linked Lists: Introduction Linked lists
Representation of linked list
operations on linked list
Comparison of Linked Lists with Arrays and Dynamic Arrays
Types of Linked Lists and operations-Circular Single Linked List, Double Linked List, Circular Double Linked List
In computer science, tree traversal (also known as tree search) is a form of graph traversal and refers to the process of visiting (checking and/or updating) each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well.
Frequent Pattern growth algorithm provides better performance than Apriori algorithm. This approach used to detect frequent itemsets in database. It has two phase. In first phase, it constructs a suffix tree and in next, it starts mining recursively.
The recursion process is shown in details in presentation with figure.
Students will be able to learn the concepts of advanced trees like Splay Tree, B Tree, Red Black Tree, Priority Queue or Heap. In Heap Data Structure the following methods are covered: Binary Heap, d-heap, Leftist Heap and Skew Heap.
B tree
B plus
graph
M way Search Tree
Types of binary trees
Extended binary tree
Perfect binary tree
Ex of B tree
Definitions
Graph Representation
Adjacency list representation
Graph traversal Techniques
Ex of BFS
Depth First Search
Breadth First Search
Time complexity
FIFO
queue
BFS
DFS
This PPT is very much useful for practitioners who are all making products and services to society. Mangers think innovatively and come up with innovative ideas. It is a 5 stage processing also called a design thinking process. The stages are empathize, define, ideate, prototype and test.
Student information management system project report ii.pdfKamal Acharya
Our project explains about the student management. This project mainly explains the various actions related to student details. This project shows some ease in adding, editing and deleting the student details. It also provides a less time consuming process for viewing, adding, editing and deleting the marks of the students.
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSEDuvanRamosGarzon1
AIRCRAFT GENERAL
The Single Aisle is the most advanced family aircraft in service today, with fly-by-wire flight controls.
The A318, A319, A320 and A321 are twin-engine subsonic medium range aircraft.
The family offers a choice of engines
Overview of the fundamental roles in Hydropower generation and the components involved in wider Electrical Engineering.
This paper presents the design and construction of hydroelectric dams from the hydrologist’s survey of the valley before construction, all aspects and involved disciplines, fluid dynamics, structural engineering, generation and mains frequency regulation to the very transmission of power through the network in the United Kingdom.
Author: Robbie Edward Sayers
Collaborators and co editors: Charlie Sims and Connor Healey.
(C) 2024 Robbie E. Sayers
Explore the innovative world of trenchless pipe repair with our comprehensive guide, "The Benefits and Techniques of Trenchless Pipe Repair." This document delves into the modern methods of repairing underground pipes without the need for extensive excavation, highlighting the numerous advantages and the latest techniques used in the industry.
Learn about the cost savings, reduced environmental impact, and minimal disruption associated with trenchless technology. Discover detailed explanations of popular techniques such as pipe bursting, cured-in-place pipe (CIPP) lining, and directional drilling. Understand how these methods can be applied to various types of infrastructure, from residential plumbing to large-scale municipal systems.
Ideal for homeowners, contractors, engineers, and anyone interested in modern plumbing solutions, this guide provides valuable insights into why trenchless pipe repair is becoming the preferred choice for pipe rehabilitation. Stay informed about the latest advancements and best practices in the field.
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
CFD analysis is incredibly effective at solving mysteries and improving the performance of complex systems!
Here's a great example: At a large natural gas-fired power plant, where they use waste heat to generate steam and energy, they were puzzled that their boiler wasn't producing as much steam as expected.
R&R and Tetra Engineering Group Inc. were asked to solve the issue with reduced steam production.
An inspection had shown that a significant amount of hot flue gas was bypassing the boiler tubes, where the heat was supposed to be transferred.
R&R Consult conducted a CFD analysis, which revealed that 6.3% of the flue gas was bypassing the boiler tubes without transferring heat. The analysis also showed that the flue gas was instead being directed along the sides of the boiler and between the modules that were supposed to capture the heat. This was the cause of the reduced performance.
Based on our results, Tetra Engineering installed covering plates to reduce the bypass flow. This improved the boiler's performance and increased electricity production.
It is always satisfying when we can help solve complex challenges like this. Do your systems also need a check-up or optimization? Give us a call!
Work done in cooperation with James Malloy and David Moelling from Tetra Engineering.
More examples of our work https://www.r-r-consult.dk/en/cases-en/
Forklift Classes Overview by Intella PartsIntella Parts
Discover the different forklift classes and their specific applications. Learn how to choose the right forklift for your needs to ensure safety, efficiency, and compliance in your operations.
For more technical information, visit our website https://intellaparts.com
Courier management system project report.pdfKamal Acharya
It is now-a-days very important for the people to send or receive articles like imported furniture, electronic items, gifts, business goods and the like. People depend vastly on different transport systems which mostly use the manual way of receiving and delivering the articles. There is no way to track the articles till they are received and there is no way to let the customer know what happened in transit, once he booked some articles. In such a situation, we need a system which completely computerizes the cargo activities including time to time tracking of the articles sent. This need is fulfilled by Courier Management System software which is online software for the cargo management people that enables them to receive the goods from a source and send them to a required destination and track their status from time to time.
Event Management System Vb Net Project Report.pdfKamal Acharya
In present era, the scopes of information technology growing with a very fast .We do not see any are untouched from this industry. The scope of information technology has become wider includes: Business and industry. Household Business, Communication, Education, Entertainment, Science, Medicine, Engineering, Distance Learning, Weather Forecasting. Carrier Searching and so on.
My project named “Event Management System” is software that store and maintained all events coordinated in college. It also helpful to print related reports. My project will help to record the events coordinated by faculties with their Name, Event subject, date & details in an efficient & effective ways.
In my system we have to make a system by which a user can record all events coordinated by a particular faculty. In our proposed system some more featured are added which differs it from the existing system such as security.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
Maintaining high-quality standards in the production of TMT bars is crucial for ensuring structural integrity in construction. Addressing common defects through careful monitoring, standardized processes, and advanced technology can significantly improve the quality of TMT bars. Continuous training and adherence to quality control measures will also play a pivotal role in minimizing these defects.
1. UNIT II : TREES
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
4. Text Book and Reference Book
4/6/2022 2.1 _ Trees Preliminaries 4
5. Unit II : Contents
1. Implementation of trees
2. Tree Traversals with an Application
3. Binary trees: Implementation
4. Expression trees
5. The Search Tree ADT: Binary Search Trees
– Construction
– Searching
– Insertion
– Deletion
– Find Min
– Find Max
6. AVL trees
– Rotation
– Insertion
– Deletion.
4/6/2022 5
2.1 _ Trees Preliminaries
6. Introduction
• For large amounts of input, the linear access time
of linked lists is prohibitive.
• In this unit we look at a simple data structure for
which the running time of most operations is
O(log n) on average.
• We also sketch a conceptually simple
modification to this data structure that
guarantees the above time bound in the worst
case and discuss a second modification that
essentially gives an O(log n) running time per
operation for a long sequence of instructions.
4/6/2022 2.1 _ Trees Preliminaries 6
7. Tree Representation
• Tree represents the nodes connected by edges. We will
discuss binary tree or binary search tree specifically.
• Binary Tree is a special data structure used for data
storage purposes.
• A binary tree has a special condition that each node
can have a maximum of two children.
• A binary tree has the benefits of both an ordered array
and a linked list as search is as quick as in a sorted
array and insertion or deletion operation are as fast as
in linked list.
4/6/2022 2.1 _ Trees Preliminaries 7
8. Trees
• The data structure that we are referring to is
known as a binary search tree.
• Trees in general are very useful abstractions in
computer science.
• Advantages:
– trees are used to implement the file system of several
popular operating systems.
– trees can be used to evaluate arithmetic expressions.
– trees to support searching operations in O(log n)
average time.
4/6/2022 2.1 _ Trees Preliminaries 8
9. Trees
• A tree can be defined in several ways.
• One natural way to define a tree is recursively.
• A tree is a collection of nodes.
• The collection can be empty, which is sometimes denoted as A.
• Otherwise, a tree consists of a distinguished node r, called the root, and
zero or more (sub)trees T1, T2, . . . , Tk, each of whose roots are
connected by a directed edge to r.
• The root of each subtree is said to be a child of r, and r is the parent of
each subtree root.
• From the recursive definition, we find that a tree is a collection of n nodes,
• one of which is the root, and n - 1 edges
4/6/2022 2.1 _ Trees Preliminaries 9
10. Tree Terms
• In the tree of Figure below, the root is A.
• Node F has A as a parent and K, L, and M as children.
• Each node may have an arbitrary number of children, possibly zero.
• Nodes with no children are known as leaves; the leaves in the tree below are B, C,
H, I, P, Q, K, L, M, and N.
• Nodes with the same parent are siblings; thus K, L, and M are all siblings.
• Grandparent and grandchild relations can be defined in a similar manner.
• A path from node n1 to nk is defined as a sequence of nodes n1, n2, . . . , nk.
• The length of this path is the number of edges on the path, namely k -1. There is a
path of length zero from every node to itself.
• Notice that in a tree there is exactly one path from the root to each node.
4/6/2022 2.1 _ Trees Preliminaries 10
11. Tree Terms
• For any node ni, the depth of ni is the length of the unique
path from the root to ni. Thus, the root is at depth 0.
• The height of ni is the longest path from ni to a leaf. Thus
all leaves are at height 0.
• The height of a tree is equal to the height of the root.
• For the tree in previous Figure,
– E is at depth 1 and height 2;
– F is at depth 1 and height 1;
– the height of the tree is 3.
• The depth of a tree is equal to the depth of the deepest
leaf; this is always equal to the height of the tree.
• If there is a path from n1 to n2, then n1 is an ancestor of
n2 and n2 is a descendant of n1. If n1=n2, then n1 is a
proper ancestor of n2 and n2 is a proper descendant of n1.
4/6/2022 2.1 _ Trees Preliminaries 11
13. Tree Terms
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root
per tree and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node
called parent.
• Child − The node below a given node connected by its edge downward is
called its child node.
• Leaf − The node which does not have any child node is called the leaf
node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on
the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root
node is at level 0, then its next child node is at level 1, its grandchild is at
level 2, and so on.
• keys − Key represents a value of a node based on which a search operation
is to be carried out for a node.
4/6/2022 2.1 _ Trees Preliminaries 13
15. UNIT II : TREES
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
16. Unit II : Contents
1. Implementation of trees
2. Tree Traversals with an Application
3. Binary trees: Implementation
4. Expression trees
5. The Search Tree ADT: Binary Search Trees
– Construction
– Searching
– Insertion
– Deletion
– Find Min
– Find Max
6. AVL trees
– Rotation
– Insertion
– Deletion.
4/6/2022 16
2.2 _ Tree Traversals
17. Tree Traversal
• Traversal is a process to visit all the nodes of a tree
and may print their values too.
• Because, all nodes are connected via edges (links) we
always start from the root (head) node.
• That is, we cannot randomly access a node in a tree.
• There are three ways which we use to traverse a tree −
– In-order Traversal
– Pre-order Traversal
– Post-order Traversal
• Generally, we traverse a tree to search or locate a
given item or key in the tree or to print all the values it
contains.
4/6/2022 2.2 _ Tree Traversals 17
18. In-order Traversal
• In this traversal method,
– the left subtree is visited first,
– then the root and
– later the right sub-tree.
• We should always remember that every node
may represent a subtree itself.
• If a binary tree is traversed in-order, the
output will produce sorted key values in an
ascending order.
4/6/2022 2.2 _ Tree Traversals 18
19. In-order Traversal
4/6/2022 2.2 _ Tree Traversals 19
• We start from A, and
following in-order traversal, we
move to its left subtree B.
• B is also traversed in-order.
• The process goes on until all
the nodes are visited.
•The output of inorder
traversal of this tree will be −
D → B → E → A → F → C → G
20. In-order Traversal – Algorithm & Code
• Until all nodes are traversed
• Step 1 − Recursively traverse left subtree.
• Step 2 − Visit root node.
• Step 3 − Recursively traverse right subtree.
4/6/2022 2.2 _ Tree Traversals 20
void inorder_traversal(struct node* root)
{
if(root != NULL)
{
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
}
}
21. Pre-order Traversal
• In this traversal method,
– the root node is visited first,
– then the left subtree and
– finally the right subtree.
4/6/2022 2.2 _ Tree Traversals 21
22. Pre-order Traversal
4/6/2022 2.2 _ Tree Traversals 22
• We start from A, and following
pre-order traversal, we first visit
A itself and then move to its left
subtree B.
• B is also traversed pre-order.
• The process goes on until all
the nodes are visited.
•The output of pre-order
traversal of this tree will be −
A → B → D → E → C → F → G
23. Pre-order Traversal – Algorithm & Code
• Until all nodes are traversed
• Step 1 − Visit root node.
• Step 2 − Recursively traverse left subtree.
• Step 3 − Recursively traverse right subtree.
4/6/2022 2.2 _ Tree Traversals 23
void preorder_traversal(struct node* root)
{
if(root != NULL)
{
printf("%d ",root->data);
preorder_traversal(root->leftChild);
preorder_traversal(root->rightChild);
}
}
24. Post-order Traversal
• In this traversal method, the root node is
visited last, hence the name.
– First we traverse the left subtree,
– then the right subtree and
– finally the root node.
4/6/2022 2.2 _ Tree Traversals 24
25. Post-order Traversal
4/6/2022 2.2 _ Tree Traversals 25
•We start from A, and following
Post-order traversal, we first
visit the left subtree B.
•B is also traversed post-order.
•The process goes on until all
the nodes are visited.
•The output of post-order
traversal of this tree will be −
D → E → B → F → G → C → A
26. Post-order Traversal – Algorithm & Code
• Until all nodes are traversed
• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree.
• Step 3 − Visit root node.
4/6/2022 2.2 _ Tree Traversals 26
void postorder_traversal(struct node* root)
{
if(root != NULL)
{
postorder_traversal(root->leftChild);
postorder_traversal(root->rightChild);
printf("%d ",root->data);
}
}
36. UNIT II : TREES
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
37. Unit II : Contents
1. Implementation of trees
2. Tree Traversals with an Application
3. Binary trees: Implementation
4. Expression trees
5. The Search Tree ADT: Binary Search Trees
– Construction
– Searching
– Insertion
– Deletion
– Find Min
– Find Max
6. AVL trees
– Rotation
– Insertion
– Deletion.
4/6/2022 37
2.3 _ Binary Tree
39. Binary Tree
• 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.
4/6/2022 2.3 _ Binary Tree 39
40. Binary Tree - Examples
4/6/2022 2.3 _ Binary Tree 40
41. Binary Search Tree
• Binary Search Tree is a node-based
binary tree data structure which
has the following properties:
– The left subtree of a node contains
only nodes with keys lesser than the
node’s key.
– The right subtree of a node
contains only nodes with keys
greater than the node’s key.
– The left and right subtree each
must also be a binary search tree.
– There must be no duplicate nodes.
4/6/2022 2.3 _ Binary Tree 41
42. Binary Tree Vs Binary Search Tree
4/6/2022 2.3 _ Binary Tree 42
43. Types of Binary Tree
• Full/Proper/Strict Binary Tree
• Complete Binary Tree
• Perfect Binary Tree
• Degenerate / Pathological Binary Tree
– Left Skewed
– Right Skewed
– Pathological
• Balanced Binary Tree
4/6/2022 2.3 _ Binary Tree 43
44. 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.
• OR
• The full binary tree can also be defined as the
tree in which each node must contain 2
children except the leaf nodes.
4/6/2022 2.3 _ Binary Tree 44
47. Complete Binary Tree
• The complete binary tree is a tree in which all
the nodes 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 should be
added from the left.
• Let's create a complete binary tree.
4/6/2022 2.3 _ Binary Tree 47
50. 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.
4/6/2022 2.3 _ Binary Tree 50
53. Degenerate Binary Tree
• The degenerate binary tree is a tree in which
all the internal nodes have only one children.
4/6/2022 2.3 _ Binary Tree 53
left-skewed tree
right-skewed tree
55. Balanced binary Tree
• The balanced binary tree is a tree in which both
the left and right trees differ by atmost 1.
• For example, AVL and Red-Black trees are
balanced binary tree.
4/6/2022 2.3 _ Binary Tree 55
This tree is a balanced binary tree because the
difference between the left subtree and right
subtree is zero.
56. Balanced Binary Tree - Example
4/6/2022 2.3 _ Binary Tree 56
The above tree is not a balanced binary tree because the difference
between the left subtree and the right subtree is greater than 1.
57. To Find Height and Min/Max Nodes in Binary Tree
• Depth of Node x: Length of path from root to Node x
• Height of Node x: Number of edges in the longest path from node x
to leaf node
• Height of tree: Height of the tree is the height of the root
• Level of node: Set of all nodes at a given depth is called the level of
the tree
• Trees with N nodes will have N-1 edges
• Depth of root node is Zero
• Root node is at level Zero
• Height of leaf node is Zero
• Height of tree with one node is Zero
• An empty tree is also a valid binary tree
• Every node except root is connected by a directed edge from
exactly one node to other node and direction is: parent -> children
4/6/2022 2.3 _ Binary Tree 57
59. Maximum and Minimum Number of Nodes of
binary tree
• Maximum number of nodes of binary tree of height
“h” is 2h+1 - 1.
4/6/2022 2.3 _ Binary Tree 59
60. • Minimum number of nodes of binary tree of
height “h” is “h+1”
4/6/2022 2.3 _ Binary Tree 60
61. Height of the binary tree with maximum and
minimum nodes of binary tree
4/6/2022 2.3 _ Binary Tree 61
• Minimum height of the binary tree of with maximum number of
nodes as “n”
• For any binary tree of height “h”, maximum number of nodes =
2h+1 – 1
Here, Max nodes = n
So, n = 2h+1 – 1
• => n + 1 = 2h+1
• Applying log on both sides,
• =>log(n+1) = log2h+1
• => log(n+1) = (h+1) log2
• =>log(n+1)=(h+1)
• =>Height of the binary with maximum “n” nodes =>
• h = ⌈ log(n+1) ⌉ - 1
62. • Minimum height of the binary tree of with
maximum number of nodes as “n”
• For any binary tree of height “h”, minimum
number of nodes = h + 1
• =>n=h+1
• =>Height of the binary with minimum “n”
nodes => h = n-1
4/6/2022 2.3 _ Binary Tree 62
68. UNIT II : TREES
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
69. Unit II : Contents
1. Implementation of trees
2. Tree Traversals with an Application
3. Binary trees: Implementation
4. Expression trees
5. The Search Tree ADT: Binary Search Trees
– Construction
– Searching
– Insertion
– Deletion
– Find Min
– Find Max
6. AVL trees
– Rotation
– Insertion
– Deletion.
4/6/2022 69
2.4 _ Expression Tree
70. Expression Tree
• The expression tree is a binary tree in which
each
– internal node corresponds to the operator and
– each leaf node corresponds to the operand.
4/6/2022 2.4 _ Expression Tree 70
71. Expression Tree - Example
• expression tree for 3 + ((5+9)*2) would be:
4/6/2022 2.4 _ Expression Tree 71
72. Constructing an Expression Tree
• First, convert infix to postfix.
• Next, We read our expression one symbol at a
time.
– If the symbol is an operand, we create a one node
tree and push a pointer to it onto a stack.
– If the symbol is an operator, we pop pointers to two
trees T1 and T2 from the stack (T1 is popped first) and
form a new tree whose root is the operator and
whose left and right children point to T2 and T1
respectively. A pointer to this new tree is then pushed
onto the stack.
4/6/2022 2.4 _ Expression Tree 72
74. Example (cntd.,)
• The first two symbols are operands, so we
create one-node trees and push pointers to
them onto a stack.*
4/6/2022 2.4 _ Expression Tree 74
a b + c d e + * *
75. Example (cntd.,)
• Next, a '+' is read, so two pointers to trees are
popped, a new tree is formed, and a pointer
to it is pushed onto the stack.*
4/6/2022 2.4 _ Expression Tree 75
a b + c d e + * *
76. Example (cntd.,)
• Next, c, d, and e are read, and for each a one-
node tree is created and a pointer to the
corresponding tree is pushed onto the stack.
4/6/2022 2.4 _ Expression Tree 76
a b + c d e + * *
77. Example (cntd.,)
• Now a '+' is read, so two trees are merged.
4/6/2022 2.4 _ Expression Tree 77
a b + c d e + * *
78. Example (cntd.,)
• Continuing, a '*' is read, so we pop two tree
pointers and form a new tree with a '*' as
root.
4/6/2022 2.4 _ Expression Tree 78
a b + c d e + * *
79. Example (cntd.,)
• Finally, the last symbol is read, two trees are
merged, and a pointer to the final tree is left
on the stack.
4/6/2022 2.4 _ Expression Tree 79
a b + c d e + * *
81. UNIT II : TREES
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
82. Unit II : Contents
1. Implementation of trees
2. Tree Traversals with an Application
3. Binary trees: Implementation
4. Expression trees
5. The Search Tree ADT :Binary Search Trees
– Construction
– Searching
– Find Min
– Find Max
– Insertion
– Deletion
6. AVL trees
– Rotation
– Insertion
– Deletion.
4/6/2022 82
2.5 _ Binary Search Tree
83. Search Tree ADT
• Search Tree ADT contains the following
operations:
– Construction
– Searching
– Insertion
– Deletion
– Find Min
– Find Max
4/6/2022 2.5 _ Binary Search Tree 83
84. Binary Search Tree
• Binary Search tree exhibits a special behavior.
• A node's
– left child must have a value less than its parent's
value and
– right child must have a value greater than its
parent value
– The left and right subtree each must also be a
binary search tree.
– There must be no duplicate nodes.
4/6/2022 2.5 _ Binary Search Tree 84
86. Tree Node
• The code to write a tree node would be
similar to what is given below.
• It has a data part and references to its left and
right child nodes.
4/6/2022 2.5 _ Binary Search Tree 86
87. BST Basic Operations
• The basic operations that can be performed on a
binary search tree data structure, are the
following −
– Insert − Inserts an element in a tree/create a tree.
– Search − Searches an element in a tree.
– Preorder Traversal − Traverses a tree in a pre-order
manner.
– Inorder Traversal − Traverses a tree in an in-order
manner.
– Postorder Traversal − Traverses a tree in a post-order
manner.
4/6/2022 2.5 _ Binary Search Tree 87
88. Insertion Operation - BST
• The very first insertion creates the tree.
• Afterwards, whenever an element is to be
inserted, first locate its proper location.
• Start searching from the root node,
– then if the data is less than the key value,
– search for the empty location in the left subtree and
– insert the data.
• Otherwise,
– search for the empty location in the right subtree and
– insert the data.
4/6/2022 2.5 _ Binary Search Tree 88
89. Insertion on BST - Algorithm
4/6/2022 2.5 _ Binary Search Tree 89
93. Search Operation - BST
• Whenever an element is to be searched,
– start searching from the root node,
– then if the data is less than the key value,
– search for the element in the left subtree.
• Otherwise,
– search for the element in the right subtree.
• Follow the same algorithm for each node.
4/6/2022 2.5 _ Binary Search Tree 93
96. Find an element in BST
• This operation requires returning a pointer to the node
in tree T that has key X or NULL if there is no such
node.
• The find operation as follows.
– Check whether the root is NULL if so then return NULL.
– Otherwise, check the value X with the root node value (ie.
T->Element)
– If X is equal to T->Element, return T
– If X is less than T->Element, traverse the left of T
recursively.
– If X is greater than T->Element, traverse the right of T
recursively.
4/6/2022 2.5 _ Binary Search Tree 96
98. Example
• To find an element 4 (X = 4) in the below tree
– In the first fig, the element 4 is checked with root 6. 4 is
less than 6. So goto the left subtree.
– In the second fig, element 4 is checked with node 2. 4 is
greater than 2. So goto the right subtree.
– In the third fig, the element 4 is checked with node 4. The
element is equal. So return 4.
4/6/2022 2.5 _ Binary Search Tree 98
99. Find Min
• This operation returns the position of the
smallest element in the tree.
• To find the minimum element, start at the
root and go left as long as there is a left child.
• The stopping point is the smallest element.
4/6/2022 2.5 _ Binary Search Tree 99
101. Find Max
• This operation returns the position of the
largest element in the tree.
• To find the maximum element, start at the
root and go right as long as there is a right
child.
• The stopping point is the largest element.
4/6/2022 2.5 _ Binary Search Tree 101
103. Insert
• To insert the element X in to the tree, check with
the root node T.
• If it is less than the root, traverse the left sub
tree recursively until it reaches the T->Left equals
to NULL. Then X is placed in T->Left.
• If it is greater than the root, traverse the right
sub tree recursively until it reaches the T->Right
equals to NULL. Then X is placed in T->Right.
• If X is found in the tree, do nothing.
4/6/2022 2.5 _ Binary Search Tree 103
104. Insert
• SearchTree Insert( ElementType X, SearchTree T )
• {
• if( T = = NULL )
• { /* Create and return a one-node tree */
• T = malloc ( sizeof (struct TreeNode) );
• if( T != NULL )
• {
• T-Element = X;
• T->Left = T->Right = NULL;
• }
• }
• else
• {
• if( X < T->Element )
• T->Left = Insert( X, T->Left ); else
• if( X > T->Element )
• T->Right = Insert( X, T->Right );
• /* else X is in the tree already. We'll do nothing */
• return T;
• }
• }
4/6/2022 2.5 _ Binary Search Tree 104
105. Example - To insert 8, 4, 1, 6, 5, 7, 10
• First insert element 8 which is considered as
root.
• Next insert element 4, 4 is less than 8, traverse
towards left.
4/6/2022 2.5 _ Binary Search Tree 105
106. Example - To insert 8, 4, 1, 6, 5, 7, 10
• Next insert element 1, 1<8 traverse towards left. 1<4 traverse
towards left.
• To insert element 6, 6<8 traverse towards left. 6>4, place it as right
child of 4.
• To insert element 5, 5<8 traverse towards left. 5>4, traverse
towards right. 5<6, place it as left child of 6.
4/6/2022 2.5 _ Binary Search Tree 106
107. Example - To insert 8, 4, 1, 6, 5, 7, 10
• To insert element 7, 7<8 traverse towards left. 7>4,
traverse towards right. 7>6, place it as right child of 6.
• To insert element 10, 10>8, place it as a right child of 8.
4/6/2022 2.5 _ Binary Search Tree 107
108. Delete
• While deleting a node from a tree, the
memory is to be released.
• Deletion operation is the complex operation
in the binary search tree.
• To delete a node from the tree consider the
following three possibilities.
– Case 1: Node to be deleted is a leaf node
– Case 2: Node with one child.
– Case 3: Node with two children.
4/6/2022 2.5 _ Binary Search Tree 108
109. Case 1: Deleting the leaf node
1. Search the parent of the leaf node.
2. Make the parent link of the leaf node as NULL.
3. Release Memory.
4/6/2022 2.5 _ Binary Search Tree 109
Example: Deletion of node 6
110. Case 2: Deleting the node with only one child.
4/6/2022 2.5 _ Binary Search Tree 110
1. Search the parent of the node to be deleted.
2. Assign the parent link to the child node of the node to be deleted.
3. Release the memory of the deleted node.
4. If a node has one child, it can be deleted by adjusting its parent
pointer to point to its child node.
Eg. Deletion of a node (4) with one child, before and after
111. Case 3: Deleting a node with two children.
4/6/2022 2.5 _ Binary Search Tree 111
• Inorder to delete a node with two children, it can be replaced by a
node whose key is larger than any node in P‟s right sub tree to
preserve the Binary Search Tree property.
• The possible nodes that could replace node P are
– Rule 1: The node with the largest value in P‟s left sub tree.
– Rule 2: The node with the smallest value in P‟s right sub tree.
Eg: Deletion of a node 4 with two children, before and after
112. Delete
4/6/2022 2.5 _ Binary Search Tree 112
• SearchTree Delete( ElementType X, SearchTree T )
• {
• Position TmpCell;
• if( T = = NULL )
• Error("Element not found"); else
• if( X < T->Element ) /* Go left */
• T->Left = Delete( X, T->Left );
• else
• if( X > T->Element ) /* Go right */
• T->Right = Delete( X, T->Right );
• else /* Found element to be deleted */
• if( T->Left && T->Right ) /* Two children */
• { /* Replace with smallest in right subtree */
• TmpCell = FindMin( T->Right );
• T->Element = TmpCell->Element;
• T->Right = Delete( T->Element, T->Right );
• }
• else /* One or zero child */
• {
• TmpCell = T;
• if( T->Left = = NULL ) /* Only a right child */
• T = T->Right;
• if( T->Right = = NULL ) /* Only a left child */
• T = T->Left;
• free(TmpCell );
• }
• return T;
• }
113. BST
• How many distinct binary search trees can be
created out of 3 distinct keys?
4/6/2022 2.5 _ Binary Search Tree 113
115. UNIT II : TREES
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Data Structures and Algorithm Analysis in C by Mark Allen Weiss & Sumitabha Das, “Computer Fundamentals and C
Programming”, 1st Edition, McGraw Hill, 2018.
20CST32 – Data Structures
116. Unit II : Contents
1. Implementation of trees
2. Tree Traversals with an Application
3. Binary trees: Implementation
4. Expression trees
5. The Search Tree ADT :Binary Search Trees
– Construction
– Searching
– Find Min
– Find Max
– Insertion
– Deletion
6. AVL trees
– Rotation
– Insertion
– Deletion
4/6/2022 116
2.6 _ AVL Trees
117. AVL Tree
• AVL Tree is invented by GM Adelson - Velsky and EM Landis
in 1962. The tree is named AVL in honour of its inventors.
• An AVL (Adelson-Velskii and Landis) tree is a binary search
tree with a balance condition.
• AVL Tree can be defined as height balanced binary search
tree in which each node is associated with a balance factor
which is calculated by subtracting the height of its right
sub-tree from that of its left sub-tree.
• The balance condition must be easy to maintain, and it
ensures that the depth of the tree is O(log n).
• The simplest idea is to require that the left and right
subtrees have the same height.
4/6/2022 2.6 _ AVL Trees 117
118. Balancing Factor
• Balancing factor is -1,0,1
• If balance factor of any node is 1, it means that the left
sub-tree is one level higher than the right sub-tree.
• If balance factor of any node is 0, it means that the left
sub-tree and right sub-tree contain equal height.
• If balance factor of any node is -1, it means that the left
sub-tree is one level lower than the right sub-tree.
4/6/2022 2.6 _ AVL Trees 118
120. Why AVL Tree?
• AVL tree controls the height of the binary
search tree by not letting it to be skewed.
• The time taken for all operations in a binary
search tree of height h is O(h).
• However, it can be extended to O(n) if the BST
becomes skewed (i.e. worst case).
• By limiting this height to log n, AVL tree
imposes an upper bound on each operation to
be O(log n) where n is the number of nodes.
4/6/2022 2.6 _ AVL Trees 120
122. Operations on AVL tree
• Due to the fact that, AVL tree is also a binary
search tree therefore, all the operations are
performed in the same way as they are
performed in a binary search tree.
– Searching and traversing do not lead to the
violation in property of AVL tree.
– However, insertion and deletion are the
operations which can violate this property and
therefore, they need to be revisited.
4/6/2022 2.6 _ AVL Trees 122
123. AVL Tree - Rotations
4/6/2022 2.6 _ AVL Trees 123
124. AVL Tree - Rotations
• If the balance factor of any node in an AVL tree becomes less than -1 or greater
than 1, the tree has to be balanced by making a simple modifications in the tree
called rotation.
• An AVL tree causes imbalance, when any one of the following conditions occur. α –
is the node must be rebalanced.
– Case 1: An insertion from the left sub tree of the left child of node α. [LL
Rotation]
– Case 2: An insertion into the right sub tree of the left child of α. [LR Rotation]
– Case 3: An insertion into the left sub tree of the right child of α. [RL Rotation]
– Case 4: An insertion into the right sub tree of the right child of α. [RR
Rotation]
• These imbalances can be overcome by
– Single Rotation
– Double Rotation
• Case 1 and Case 4 imbalance (left – left or right – right) is fixed by a single rotation
of the tree.
• Case 2 and Case 3 imbalance (left – right or right – left) is fixed by double rotation.
4/6/2022 2.6 _ AVL Trees 124
125. LL Rotation
• When BST becomes unbalanced, due to a node is
inserted into the left subtree of the left subtree of C,
then we perform LL rotation.
• LL rotation is clockwise rotation, which is applied on
the edge below a node having balance factor 2.
4/6/2022 2.6 _ AVL Trees 125
126. RR Rotation
• When BST becomes unbalanced, due to a node is inserted
into the right subtree of the right subtree of A, then we
perform RR rotation.
• RR rotation is an anticlockwise rotation, which is applied on
the edge below a node having balance factor -2.
4/6/2022 2.6 _ AVL Trees 126
127. LR Rotation
• Double rotations are bit tougher than single
rotation which has already explained above.
• LR rotation = RR rotation + LL rotation
• i.e., first RR rotation is performed on subtree
and then LL rotation is performed on full tree,
• Here the full tree we mean the first node from
the path of inserted node whose balance
factor is other than -1, 0, or 1.
4/6/2022 2.6 _ AVL Trees 127
129. RL Rotation
4/6/2022 2.6 _ AVL Trees 129
• As already discussed, that double rotations are
bit tougher than single rotation which has already
explained above.
• R L rotation = LL rotation + RR rotation
• i.e., first LL rotation is performed on subtree and
then RR rotation is performed on full tree,
• Here the full tree we mean the first node from
the path of inserted node whose balance factor is
other than -1, 0, or 1.
131. AVL Tree - Insertion
• In an AVL tree, the insertion operation is performed
with O(log n) time complexity.
• In AVL Tree, a new node is always inserted as a leaf
node.
• The insertion operation is performed as follows...
– Step 1 - Insert the new element into the tree using Binary
Search Tree insertion logic.
– Step 2 - After insertion, check the Balance Factor of every
node.
– Step 3 - If the Balance Factor of every node is 0 or 1 or -
1 then go for next operation.
– Step 4 - If the Balance Factor of any node is other than 0
or 1 or -1 then that tree is said to be imbalanced. In this
case, perform suitable Rotation to make it balanced and
go for next operation.
4/6/2022 2.6 _ AVL Trees 131
132. AVL Tree Example – 51,26,11,6,8,4,31
4/6/2022 2.6 _ AVL Trees 132
133. AVL Tree - Deletion
• The deletion operation in AVL Tree is similar
to deletion operation in BST.
• But after every deletion operation, we need
to check with the Balance Factor condition.
• If the tree is balanced after deletion go for
next operation otherwise perform suitable
rotation to make the tree Balanced.
4/6/2022 2.6 _ AVL Trees 133