0% found this document useful (0 votes)
50 views82 pages

Binary, Trees

The document discusses tree data structures including their properties, terminology, representations, types of binary trees, and tree traversal methods. Key aspects covered include root, child, parent and sibling nodes, internal and leaf nodes, height, depth, and preorder, inorder, and postorder traversals.

Uploaded by

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

Binary, Trees

The document discusses tree data structures including their properties, terminology, representations, types of binary trees, and tree traversal methods. Key aspects covered include root, child, parent and sibling nodes, internal and leaf nodes, height, depth, and preorder, inorder, and postorder traversals.

Uploaded by

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

TREES

Tree is a non-linear data structure which organizes data in a hierarchical


structure.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between every pair of vertices,
then graph is called as a tree.
PROPERTIES

The important properties of tree data structure are-


•There is one and only one path between every pair of vertices in a tree.
•A tree with n vertices has exactly (n-1) edges.
•A graph is a tree if and only if it is minimally connected.
•Any connected graph with n vertices and (n-1) edges is a tree.
TREE TERMINOLOGY
1. Root-
The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
2. Edge-

•The connecting link between any two nodes is called as an edge.


•In a tree with n number of nodes, there are exactly (n-1) number of edges.
Parent-

The node which has a branch from it to any other node is called as a parent
node.
In other words, the node which has one or more children is called as a parent
node.
In a tree, a parent node can have any number of child nodes.

Node A is the parent of nodes B and C


Node B is the parent of nodes D, E
and F
Node C is the parent of nodes G and H
Node E is the parent of nodes I and J
Node G is the parent of node K
Child-

The node which is a descendant of some node is called as a child node.


All the nodes except root node are child nodes.

•Nodes B and C are the children of


node A
•Nodes D, E and F are the children
of node B
•Nodes G and H are the children of
node C
•Nodes I and J are the children of
node E
•Node K is the child of node G
Siblings-
Nodes which belong to the same parent are called as siblings.
In other words, nodes with the same parent are sibling nodes.
Degree-
Degree of a node is the total number of children of that node.
Degree of a tree is the highest degree of a node among all the nodes in the
tree.
•Degree of node A=2
•Degree of node B=3
•Degree of node C=2
•Degree of node D=0
•Degree of node E=2
•Degree of node F=0
•Degree of node G=1
•Degree of node H=0
•Degree of node I=0
•Degree of node J=0
•Degree of node K=0
Internal Node-
The node which has at least one child is called as an internal node.
Internal nodes are also called as non-terminal nodes.
Every non-leaf node is an internal node.
Leaf Node-
The node which does not have any child is called as a leaf node.
Leaf nodes are also called as external nodes or terminal nodes.
Level-
In a tree, each step from top to bottom is called as level of a tree.
The level count starts with 0 and increments by 1 at each level or step.
Height-
Total number of edges that lies on the longest path from any leaf node to a
particular node is called as height of that node.
Height of a tree is the height of root node.
Height of all leaf nodes = 0
Height of node A=3
Height of node B=2
Height of node C=2
Height of node D=0
Height of node E=1
Height of node F=0
Height of node G=1
Height of node H=0
Height of node I=0
Height of node J=0
Height of node K=0
Depth-

Total number of edges from root node to a particular node is called as depth
of that node.
Depth of a tree is the total number of edges from root node to a leaf node in
the longest path.
Depth of the root node = 0
The terms “level” and “depth” are used interchangeably.
Depth of node A = 0
Depth of node B = 1
Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Subtree-
In a tree, each child from a node forms a subtree recursively.
Every child node forms a subtree on its parent node.
Tree Representations

A tree data structure can be represented in two methods.

❖ List Representation
❖ Left Child - Right Sibling Representation
1. List Representation

In this representation, two types of nodes one for representing the node with
data called 'data node' and another for representing only references called
'reference node’.
Start with a 'data node' from the root node in the tree.
Then it is linked to an internal node through a 'reference node' which is
further linked to any other node directly.
This process repeats for all the nodes in the tree.
Left Child - Right Sibling Representation

In this representation, use a list with one type of node which consists of three
fields namely Data field, Left child reference field and Right sibling reference
field.
Data field stores the actual value of a node, left reference field stores the
address of the left child and right reference field stores the address of the
right sibling node.
BINARY TREE

Binary tree is a special tree data structure in which each node can have at
most 2 children.
Thus, in a binary tree,Each node has either 0 child or 1 child or 2 children.
Unlabelled Binary Tree
Labelled Binary Tree
Types of Binary Trees
1. Rooted Binary Tree-

A rooted binary tree is a binary tree that satisfies the following 2 properties-

❖ It has a root node.


❖ Each node has at most 2 children.
Full / Strictly Binary Tree

A binary tree in which every node has either 0 or 2 children is called as a Full
binary tree.
Full binary tree is also called as Strictly binary tree.
Almost Complete Binary Tree-

An almost complete binary tree is a binary tree that satisfies the following 2
properties-

❖ All the levels are completely filled except possibly the last level.
❖ The last level must be strictly filled from left to right.
Skewed Binary Tree-
A skewed binary tree is a binary tree that satisfies the following 2 properties-

❖ All the nodes except one node has one and only one child.
❖ The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Tree Traversal-
Tree Traversal refers to the process of visiting each node in a tree data
structure exactly once.
1. Preorder Traversal-
Algorithm-

1. Visit the root Root → Left → Right


2. Traverse the left sub tree
3. Traverse the right sub tree Preorder Traversal Shortcut
Traverse the entire tree starting from the root
node keeping yourself to the left.
Applications-

Preorder traversal is used to get prefix expression of an expression tree.


Preorder traversal is used to create a copy of the tree.
2. Inorder Traversal-
Algorithm-
Left → Root → Right
1. Traverse the left sub tree
2. Visit the root
3. Traverse the right sub tree
Inorder Traversal Shortcut
Keep a plane mirror horizontally at the bottom of
the tree and take the projection of all the nodes.

Application-
Inorder traversal is used to get infix expression
3. Postorder Traversal-
Algorithm-
Left → Right → Root
1. Traverse the left sub tree
2. Traverse the right sub tree
3. Visit the root Postorder Traversal Shortcut
Pluck all the leftmost leaf nodes one by one.
Applications-

❖ Postorder traversal is used to get postfix expression of an expression tree.


❖ Postorder traversal is used to delete the tree.
❖ This is because it deletes the children first and then it deletes the parent.
Breadth First Traversal-

Breadth First Traversal of a tree prints all the nodes of a tree level by level.
Breadth First Traversal is also called as Level Order Traversal.

Application-
Level order traversal is used to print the data in the same order as stored in the array
representation of a complete binary tree.
Binary Search Tree
Binary Search Tree is a special kind of binary tree in which nodes are
arranged in a specific order
In a binary search tree (BST), each node contains-
❖ Only smaller values in its left sub tree
❖ Only larger values in its right sub tree
If three distinct keys are A, B and C, then 5 distinct binary search trees are-

Binary Search Tree Construction-


Binary Search Tree Operations-
Commonly performed operations on binary search tree are

1. Search Operation-
Search Operation is performed to search a particular element in the Binary Search Tree.
Rules-

❖ Compare the key with the value of root node.


❖ If the key is present at the root node, then return the root node.
❖ If the key is greater than the root node value, then recur for the root node’s
right subtree.
❖ If the key is smaller than the root node value, then recur for the root node’s
Example-
Consider key = 45 has to be searched in the given BST-

We start our search from the root node 25.


As 45 > 25, so we search in 25’s right subtree.
As 45 < 50, so we search in 50’s left subtree.
As 45 > 35, so we search in 35’s right subtree.
As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.
2. Insertion Operation-
To insert an element in the Binary Search Tree.
Rules-

❖ The insertion of a new key always takes place as the child of some leaf node.
❖ For finding out the suitable leaf node,
❖ Search the key to be inserted from the root node till some leaf node is
reached.
❖ Once a leaf node is reached, insert the key as child of that leaf node.
key = 40 is inserted in the given BST-
Deletion Operation-
To delete a particular element from the Binary Search Tree.

Case-01: Deletion Of A Node Having No Child (Leaf Node)-


Just remove / disconnect the leaf node that is to deleted from the tree.
Case-02: Deletion Of A Node Having Only One Child-

Just make the child of the deleting node, the child of its grandparent.
Deletion Of A Node Having Two Children-

A node with two children may be deleted from the BST in two ways-
Method-01:
❖ Visit to the right subtree of the deleting node.
❖ Pluck the least value element called as inorder successor.
❖ Replace the deleting element with its inorder successor.
❖ An inorder successor of a node in the BST is the next node
in the inorder sequence
Method-02:
❖ Visit to the left subtree of the deleting node.
❖ Pluck the greatest value element called as inorder predecessor.
❖ Replace the deleting element with its inorder predecessor.
M Way Search Tree
A multiway tree is defined as a tree that can have more than two children.
If a multiway tree can have maximum m children, then this tree is called as
multiway tree of order m (or an m-way tree).

As with the other trees that have been studied, the nodes in an m-way tree
will be made up of m-1 key fields and pointers to children.

multiway tree of order 5


To make the processing of m-way trees easier some type of
constraint or order will be imposed on the keys within each node,
resulting in a multiway search tree of order m (or an m-way search
tree).
By definition an m-way search tree is a m-way tree in which
following condition should be satisfied −

❖ Each node is associated with m children and m-1 key fields


❖ The keys in each node are arranged in ascending order.
❖ The keys in the first j children are less than the j-th key.
❖ The keys in the last m-j children are higher than the j-th key.
4-way tree, where each node can have at most 3(4-1) key
fields and at most 4 children. It is also a 4-way search tree.
B Trees
A B tree is an extension of an M-way search tree.
Besides having all the properties of an M-way search tree, it has
some properties of its own, these mainly are:
❖ All the leaf nodes in a B tree are at the same level.
❖ All internal nodes must have M/2 children.
❖ If the root node is a non leaf node, then it must have at least
two children.
❖ All nodes except the root node, must have at least [M/2]-1 keys
and at most M-1 keys.
B Trees
B Trees
B-Tree of Order m has the following properties...

#1 - All leaf nodes must be at same level.


#2 - All nodes except root must have at least [m/2]-1 keys and
maximum of m-1 keys.
#3 - All non leaf nodes except root (i.e. all internal nodes) must
have at least m/2 children.
#4 - If the root node is a non leaf node, then it must have atleast 2
children.
#5 - A non leaf node with n-1 keys must have n number of
children.
#6 - All the key values in a node must be in Ascending Order.
Thank you
Thank You
B Trees
B Trees
B Trees

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