0% found this document useful (0 votes)
21 views26 pages

Bnrytree

A binary tree is a hierarchical data structure where each node has at most two children. Binary trees have many applications including data storage and retrieval, expression evaluation, and game AI. Common tree traversal methods include inorder, preorder, and postorder traversals, which visit nodes in different orders.

Uploaded by

holybright609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views26 pages

Bnrytree

A binary tree is a hierarchical data structure where each node has at most two children. Binary trees have many applications including data storage and retrieval, expression evaluation, and game AI. Common tree traversal methods include inorder, preorder, and postorder traversals, which visit nodes in different orders.

Uploaded by

holybright609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

BINARY TREE

John Jake T. Quinton

Kristel Joyce Zausa


INTRODUCTION TO BINARY TREE

\\A binary tree is a tree data structure in which each node can have at most two children, which
are referred to as the left child and the right child.
\\The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.
A binary tree can be visualized as a hierarchical structure with the root at the top and the leaves
at the bottom.
Binary trees have many applications in computer science, including data storage and retrieval,
expression evaluation, network routing, and game AI. They can also be used to implement various
algorithms such as searching, sorting, and graph algorithms.

Representation of Binary Tree:


Each node in the tree contains the following:

-Data

-Pointer to the left child

-Pointer to the right child


INTRODUCTION TO BINARY TREE

In C, we can represent a tree node using structures. In other languages, we can use classes as
part of their OOP feature. Below is an example of a tree node with integer data .

Basic Operations On Binary Tree:


-Inserting an element.
-Removing an element.
-Searching for an element.
-Deletion for an element.
-Traversing an element. There are four (mainly three) types of traversals in a binary tree -
which will be discussed ahead.
TYPES OF BINARY TREE

Following are the types of Binary Tree based on the


number of children:

-Full Binary Tree


-Degenerate Binary Tree
-Skewed Binary Trees
TYPES OF BINARY TREE

1. Full Binary Tree


-A Binary Tree is a full binary tree if every node has 0 or 2 children. The
following are examples of a full binary tree. We can also say a full binary tree
is a binary tree in which all nodes except leaf nodes have two children.
-A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children. It is also known as a proper
binary tree.
TYPES OF BINARY TREE

2. Degenerate (or pathological) tree


A Tree where every internal node has one child. Such trees are performance-
wise same as linked list. A degenerate or pathological tree is a tree having a
single child either left or right.
TYPES OF BINARY TREE

3. Skewed Binary Tree


A skewed binary tree is a pathological/degenerate tree in which the tree is
either dominated by the left nodes or the right nodes. Thus, there are two
types of skewed binary tree: left-skewed binary tree and right-skewed binary
tree.
1. LEFT SKEWED BINARY TREE:

These are those skewed


binary trees in which all
the nodes are having a
left child or no child at
all. It is a left side
dominated tree. All the
right children remain as
null.

Below is an example of a
left-skewed tree:
2. RIGHT SKEWED BINARY TREE:

These are those


skewed binary trees in
which all the nodes
are having a right child
or no child at all. It is
a right side dominated
tree. All the left
children remain as
null.

Below is an example of
a right-skewed tree:
Real-time applications of Binary Trees:
DOM in HTML.
File explorer.
APPLICATIONS, ADVANTAGES AND Used as the basic data structure in
DISADVANTAGES OF BINARY TREE Microsoft Excel and spreadsheets.
Editor tool: Microsoft Excel and
spreadsheets.
Evaluate an expression
Routing Algorithms

Application of Binary Trees:


--Search algorithms: Binary search algorithms use the structure of binary trees to efficiently search for a
specific element. The search can be performed in O(log n) time complexity, where n is the number of nodes
in the tree.

--Sorting algorithms: Binary trees can be used to implement efficient sorting algorithms, such as binary
search tree sort and heap sort.

--Database systems: Binary trees can be used to store data in a database system, with each node
representing a record. This allows for efficient search operations and enables the database system to
handle large amounts of data.

--File systems: Binary trees can be used to implement file systems, where each node represents a directory
or file. This allows for efficient navigation and searching of the file system.

--Compression algorithms: Binary trees can be used to implement Huffman coding, a compression algorithm
that assigns variable-length codes to characters based on their frequency of occurrence in the input data.

--Decision trees: Binary trees can be used to implement decision trees, a type of machine learning
algorithm used for classification and regression analysis.

--Game AI: Binary trees can be used to implement game AI, where each node represents a possible move in
the game. The AI algorithm can search the tree to find the best possible move.
TREE TRAVERSAL TECHNIQUES

Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to
traverse them, trees can be traversed in different ways.
A Tree Data Structure can be traversed in following ways:

\\ Depth First Search or DFS

-Inorder Traversal

-Preorder Traversal

-Postorder Traversal

\\ Level Order Traversal or


Breadth First Search or BFS

-Boundary Traversal

-Diagonal Traversal
TREE TRAVERSAL - INORDER, PREORDER AND POSTORDER

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.

According to this structure, every tree is a combination of


-A node carrying data -Two subtrees
INORDER TRAVERSAL:

Algorithm Inorder(tree)
1.Traverse the left subtree, i.e., call Inorder(left->subtree)
2.Visit the root.
3.Traverse the right subtree, i.e., call Inorder(right->subtree)

Inorder traversal
First, visit all the nodes in the left subtree
Then the root node
Visit all the nodes in the right subtree

Uses of Inorder Traversal:


In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To
get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder traversal is
reversed can be used.
INORDER TRAVERSAL:
PREORDER TRAVERSAL

Algorithm Preorder(tree)

1.Visit the root.


2.Traverse the left subtree, i.e., call Preorder(left->subtree)
3.Traverse the right subtree, i.e., call Preorder(right->subtree)

Preorder traversal
Visit root node
Visit all the nodes in the left subtree
Visit all the nodes in the right subtree

Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to
get prefix expressions on an expression tree.
PREORDER TRAVERSAL
POSTORDER TRAVERSAL

Algorithm Postorder(tree)
1.Traverse the left subtree, i.e., call Postorder(left->subtree)
2.Traverse the right subtree, i.e., call Postorder(right->subtree)
3.Visit the root

Postorder traversal
Visit all the nodes in the left subtree
Visit all the nodes in the right subtree
Visit the root node

Uses of Postorder:
Postorder traversal is used to delete the tree. Postorder traversal is also useful to get the
postfix expression of an expression tree
Below is the implementation of the above traversal methods:
POSTORDER TRAVERSAL
LEVEL ORDER TRAVERSAL (BREADTH FIRST SEARCH OR BFS) OF BINARY
TREE

Level Order Traversal technique is defined as a method to traverse a Tree such that all nodes
present in the same level are traversed completely before traversing the next level.
Example:
Input: Output: 1
23
45

How does Level Order Traversal work?


The main idea of level order traversal is to traverse all the nodes of a lower level before moving to
any of the nodes of a higher level. This can be done in any of the following ways:
the naive one (finding the height of the tree and traversing each level and printing the nodes of that
level)
efficiently using a queue.
Level Order Traversal (Naive approach):

LEVEL ORDER TRAVERSAL (NAIVE Find height of tree. Then for each level, run a recursive function by
maintaining current height. Whenever the level of a node matches, print
APPROACH): that node.

Below is the implementation of the above


approach:
LEVEL ORDER TRAVERSAL USING QUEUE

We need to visit the nodes in a lower level before any node in a higher level, this idea is quite similar to that of a
queue. Push the nodes of a lower level in the queue. When any node is visited, pop that node from the queue and push
the child of that node in the queue.
This ensures that the node of a lower level are visited prior to any node of a higher level.

Below is the Implementation of the above approach:


INSERTION IN A BINARY TREE IN LEVEL
ORDER
INSERTION IN A BINARY TREE IN
LEVEL ORDER
DELETION IN A BINARY TREE

Given a binary tree, delete a node from it by making sure that the tree shrinks from the bottom
(i.e. the deleted node is replaced by the bottom-most and rightmost node). This is different from
BST(binary search tree) deletion. Here we do not have any order among elements, so we replace
them with the last element.
Examples :
DELETION IN A BINARY TREE

Algorithm:
--Starting at the root, find the deepest and rightmost node in the binary tree and the node
which we want to delete.
--Replace the deepest rightmost node’s data with the node to be deleted.
--Then delete the deepest rightmost node.
DELETION IN A BINARY TREE

You might also like