0% found this document useful (0 votes)
10 views

Binary Trees

Uploaded by

maonangel2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Binary Trees

Uploaded by

maonangel2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

BINARY TREES

GROUP 5

Abegail Acaylar Elias Dondon


Charles Andrei Arucan Jessica Domingo
Karl Marion Acosta Charles Matubis
Dianabel Braceros Joyce Ann Oliva
Leonie Duque Gian Drale Ramos
Marilyn Vegiga
1. Trees, Binary Trees, and Binary Search
Trees
TREES
A tree is a non-linear data structure that
represents a hierarchical relationship between
elements, called nodes. It consists of a root
node, which is the topmost node, and branches
that extend from the root to form child nodes.
Each child node can have its own set of children,
creating a tree-like structure.
COMPONENTS OF
TREES
 Root Node: The topmost node of the
tree.
 Parent Node: A node that has one or
more child nodes.
 Child Node: A node that is directly below a
parent node.
 Leaf Node: A node that has no children.
 Sibling Nodes: Nodes that share the same
parent node.
 Level: The depth of a node from the root.
 Height: The maximum depth of any node in the
BINARY TREES
tree.
A binary tree is a hierarchical data structure
where each node can have at most two
children: a left child and a right child. This
structure is widely used in various algorithms
and data structure due to its efficient search,
insertion, and deletion operations.
TERMS
 Node: Represents a data element.
 Root: The topmost node of the tree.
 Parent Node: A node that has child nodes.
 Child Node: A node that has a parent node.
 Leaf Node: A node with no children.
 Subtree: A portion of the tree rooted at a
particular node.

TREE TRAVERSAL:
There are three common ways to traversal a
binary tree:
 Inorder Traversal: Left subtree, root, right
subtree.
 Preorder Travelrsal: Root, left subtree, right
subtree.
 Postorder Traversal: left subtree,right
BINARY
subtree, SEARCH
root.
TREES:
A Binary Search Trees (BSTs) is a fundamental
data structure in computer science that
organizes data in a hierarchical manner. It has
the following properties:
PROPERTIES:
1. Node Structure: Each node in a
BTS contains three parts:
 Data: the value stored in the node.
 Left Child: A pointer to the left child node.
 Right Child: A pointer to the right child
node .
2. Ordering Property : For any node in the
BTS:
All nodes in its left subtree have values less
than the node’s value. All nodes in its right
subtree have values greater than the node’s
value.
Output:
2.IMPLEMENTNG BINARY TREES
Implementing a binary tree involves creating a
data structure to represent the nodes and their
connections.
BASIC CREATING A
STRUCTION OF BINARY TREE
A NODE:
COMMON OPERATIONS ON BINARY
TREES:
1. Traversal:
 Inorder Traversal: Left, Root, Right
 Preorder Traversal: Root, Left, Right
 Postorder Traversal: Left, Right, Root
2. INSERTION
 Find the appropriate position to insert the
new node based on a specific order (e.g.,
sorted order for binary search trees).
 Create a new node and link it to the parent
node.

3.DELETION
 Find the node to be deleted.
 Consider three cases:
 Node with no children: Simply remove the
node.
 Node with one child: Replace the node
with its child.
4. SEARCHNG
•Traverse the tree, comparing the data at
each node with the target value.
•If a match is found, return the node;
otherwise, continue the search.

Example of Binary Search Tree (BST)

A BST is a binary tree where the left child of a


node contains values less than the node, and the
right child contains values greater than the node.
3. SEARCHING A BINARY SEARCH TREE
A binary search tree is a binary tree where the left
child of a node is always less than the parent
node, and the right child is always greater than
the parent node. This allows for efficient
searching.
Output:
4. BALANCING A TREE

A balanced binary tree is defined as a binary


tree in which at every node, its left sub-tree and
right sub-tree have an equal height or their
height differ by just 1.

In other words, if we consider any node of the


tree as the root of a tree, then the heights of its
left sub-tree and right sub-tree should never
differ by more than 1.
How to check if a Binary Tree is Balanced or not?
As per the definition, the height of the left
subtree and right subtree should not be greater
than one at any node.

So if we consider a tree to be balanced at any


node, we will have to find the height of its left
sub-tree and right sub-tree.
Then we will check the difference in the heights.
If the difference comes out to be greater than 1
at any node, we will declare that the tree is not
balanced. The following is the algorithm for this
procedure:
Algorithm
Check Balanced Binary Tree:
Input: Root Node of the binary tree.
Output:True if binary tree is balanced and False
otherwise.
Start. 0.If tree is empty, return True.

1.Check the height of left sub-tree.


2.Check the height of right sub-tree.
3.If difference in height is greater than 1 return
False.
4.Check if left sub-tree is balanced.
5.Check if right sub-tree is balanced.
6.If left sub-tree is balanced and right sub-tree is
We have figured out the algorithm for checking
if the binary tree is balanced but we don’t
know how to calculate the height of tree and
sub trees. So We will first implement a program
to find the height of tree if the root node is
given and then we will implement the above
algorithm.

In other words, if we consider any node of the


tree as the root of a tree, then the heights of its
left sub-tree and right sub-tree should never
differ by more than 1.
How to check if a Binary Tree is Balanced or not?
As per the definition, the height of the left subtree
and right subtree should not be greater than one
at any node.

So if we consider a tree to be balanced at any


node, we will have to find the height of its left
sub-tree and right sub-tree.

Then we will check the difference in the heights. If


the
difference comes out to be greater than 1 at any
node, we will
How to find the height of a balanced binary tree?
To find the height of a binary tree, we can just
keep in mind the following points.

If the root is empty, then the height of the tree


will be 0. If the root is not empty, then the height
of the tree will be equal to the maximum height
of the left sub-tree of the root and right sub-tree
of the root added 1. Keeping in mind the above
points, The algorithm for finding the height of
the tree is:
Algorithm height(tree):
Input: Root of the tree
Output:Height of the tree Start.
1.If the root is None, Return 0.
2.Find the height of the left
subtree.//height(root.leftChild) 3.Find the height of
the right subtree .//height(root.rightChild)
4.Find the maximum value in 2 and 3 and add 1 to
it. End
Now we will implement the above algorithm and
execute it for the following binary tree.
Askpython31 2
Program to find the height of a binary tree
Following is the code for finding height of a
binary tree.
Output
:
Now, we know how to find the height of a
binary tree. So we will now implement the
algorithm to check if a binary tree is
balanced or not for above given binary tree.

Program to check if a Binary Tree is Balanced


or not Following program has been
implemented to check if binary tree is
balanced or not.
Output:
As the binary tree in our example is
balanced, the program has printed True. You
can check it for unbalanced binary trees by
modifying the program to insert new values.
5. HEAPS
A heap is a specialized tree-based data
structure satisfying the heap property:
 Max-Heap: Each parent node is greater
than or equal to its child nodes.
 Min-Heap: Each parent node is less than or
equal to its child nodes.

Example Code for Min-Heap and Max-Heap


Using Priority Queue:
Output:
Organizing Arrays as Heaps

To organize an array as a heap, we can use


the heapify function, which converts a list
into a heap in time.

Output:

You might also like