Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Tree and Binary Tree Data Structure

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

TREE DATA STRUCTURE

NON-LINEAR
It is a hierarchical structure that is used to
represent and organize data in a way that is easy to
navigate and search. It is a collection of nodes that are
connected by edges and has a hierarchical relationship
between the nodes.
Tree
Data Structure The topmost node of the tree is called the root,
and the nodes below it are called the child nodes. Each
node can have multiple child nodes, and these child
nodes can also have their own child nodes, forming a
recursive structure.
This data structure is a specialized method to
Tree organize and store data in the computer to be used more
effectively. We can also say that tree data structure has
Data Structure roots, branches, and leaves connected with one another.
Parent Node: The node which is a predecessor of a
node is called the parent node of that node. {B} is the
Basic parent node of {D, E}.
Terminologies in
Tree Data Child Node: The node which is the immediate
Structure successor of a node is called the child node of that node.
Examples: {D, E} are the child nodes of {B}.
Root Node: The topmost node of a tree or the node
which does not have any parent node is called the root
node. {A} is the root node of the tree. A non-empty tree
Basic must contain exactly one root node and exactly one path
Terminologies in from the root to all other nodes of the tree.
Tree Data
Structure Leaf Node or External Node: The nodes which do not
have any child nodes are called leaf nodes. {K, L, M, N,
O, P} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path
of the root to that node are called Ancestors of that node.
{A,B} are the ancestor nodes of the node {E}

Basic
Terminologies in Descendant: Any successor node on the path from the
Tree Data leaf node to that node. {E,I} are the descendants of the
Structure node {B}.

Sibling: Children of the same parent node are called


siblings. {D,E} are called siblings.
Level of a node: The count of edges on the path from
the root node to that node. The root node has level 0.

Basic Internal node: A node with at least one child is called


Terminologies in Internal Node.
Tree Data
Structure
Neighbor of a Node: Parent or child nodes of that node
are called neighbors of that node.

Subtree: Any node of the tree along with its descendant.


A Tree Data Structure can be traversed in following
ways:
Depth First Search or DFS
 Inorder Traversal
 Preorder Traversal
Tree Traversal
 Postorder Traversal
Techniques
Level Order Traversal or Breadth First Search( BFS)
Boundary Traversal
Diagonal Traversal
Inorder(Tree):

 Traverse the left subtree, i.e., call Inorder(left->subtree)


 Visit the root.

Inorder  Traverse the right subtree, i.e., call Inorder(right-


>subtree)
Traversal
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.
Preorder(Tree)

 Visit the root.


 Traverse the left subtree, i.e., call Preorder(left-
>subtree)
Preorder  Traverse the right subtree, i.e., call Preorder(right-
Traversal >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.
Postorder(Tree):

 Traverse the left subtree, i.e., call Postorder(left-


>subtree)
 Traverse the right subtree, i.e., call Postorder(right-
Postorder
>subtree)
Traversal
 Visit the root

Uses of Postorder:
Postorder traversal is used to delete the tree.
For each node, first, the node is visited and then
it’s child nodes are put in a FIFO queue. Then again the
first node is popped out and then it’s child nodes are put
in a FIFO queue and repeat until queue becomes empty.

Level-Order
Traversal
Output:
1
23
45
Boundary Traversal of a Tree includes:

 Left boundary (nodes on left excluding leaf nodes)


 Leaves (consist of only the leaf nodes)
 Right boundary (nodes on right excluding leaf nodes)
Boundary
Traversal
Output:
root : 20
left- boundary nodes: 8
leaf nodes: 4 10 14 25
right–boundary nodes: 22
In the Diagonal Traversal of a Tree, all the nodes in a
single diagonal will be printed one by one.

Diagonal
Traversal
Output:
8 10 14
3 6 7 13
14
One reason to use trees might be because you
want to store information that naturally forms a
hierarchy. For example, the file system on a computer:

Need for Tree


Data Structure
Trees (with some ordering e.g., BST) provide moderate
access/search (quicker than Linked List and slower than
arrays).

Need for Tree Trees provide moderate insertion/deletion (quicker than


Data Structure Arrays and slower than Linked Lists).

Like Linked Lists and unlike Arrays, Trees don’t have


an upper limit on the number of nodes as nodes are
linked using pointers.
File System: This allows for efficient navigation and
organization of files.

Application of Data Compression: Huffman coding is a popular


Tree Data technique for data compression that involves
Structure constructing a binary tree where the leaves represent
characters and their frequency of occurrence. The
resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
Compiler Design: In compiler design, a syntax tree is
used to represent the structure of a program.
Application of
Tree Data
Database Indexing: B-trees and other tree structures
Structure are used in database indexing to efficiently search for
and retrieve data.
Tree offer Efficient Searching Depending on the type of
tree, with average search times of O(log n) when the tree
is balanced.

Advantages of Trees provide a hierarchical representation of data,


Tree Data making it easy to organize and navigate large amounts
Structure of information.

The recursive nature of trees makes them easy to


traverse and manipulate using recursive algorithms.
Unbalanced Trees, meaning that the height of the tree is
skewed towards one side, which can lead to inefficient
search times.

Disadvantages
Trees demand more memory space requirements than
of some other data structures like arrays and linked lists,
Tree Data especially if the tree is very large.
Structure
The implementation and manipulation of trees can be
complex and require a good understanding of the
algorithms.
BINARY TREE
In a binary tree, each node can have a maximum of two
children linked to it. Some common types of binary trees
Binary Tree include full binary trees, complete binary trees, balanced
binary trees, and degenerate or pathological binary trees.
Full Binary Tree

Types of Degenerate Binary Tree


Binary Tree
Skewed Binary Trees
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.

Full
Binary 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.

Degenerate
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.

Skewed
Binary Tree
Complete Binary Tree
Types of Binary
Tree
On the basis of the Perfect Binary Tree
completion of
levels
Balanced Binary Tree
Complete Binary Tree if all the levels are completely
filled except possibly the last level and the last level has
all keys as left as possible.

A complete binary tree is just like a full binary tree, but


with two major differences:
Complete
Binary Tree
 Every level except the last level must be completely
filled.
 All the leaf elements must lean towards the left.
 The last leaf element might not have a right sibling i.e.
a complete binary tree doesn’t have to be a full binary
tree.
All the internal nodes have two children and all leaf
nodes are at the same level.
Perfect Binary
Tree A perfect binary tree is a type of binary tree in which
every internal node has exactly two child nodes and all
the leaf nodes are at the same level.
A balanced binary tree is a binary tree that follows the 3
conditions:
 The height of the left and right tree for any node does
not differ by more than 1.
Balanced  The left subtree of that node is also balanced.
Binary Tree  The right subtree of that node is also balanced.

A single node is always balanced. It is also referred to as


a height-balanced binary tree.
Binary tree contains the following:

Representation  Data
Binary Tree  Pointer to the left child
 Pointer to the right child
 Inserting an element.
 Removing an element.
Basic
Operation on  Searching for an element.
Binary Tree  Deletion for an element.
 Traversing an element.
 Used to find elements in less time (binary search tree)
 Used to enable fast memory allocation in computers.
 Used to perform encoding and decoding operations.
 Binary trees can be used to implement sorting
Applications of algorithms, such as in heap sort which uses a binary
Binary Tree heap to sort elements efficiently.
 Binary trees can be used to represent the decision-
making process of computer-controlled characters in
games, such as in decision trees.
Tree Traversal algorithms can be classified broadly
into two categories:
Traversals of
Binary Tree  Depth-First Search (DFS) Algorithms
 Breadth-First Search (BFS) Algorithms
Tree is a hierarchical data structure. Main
uses of trees include maintaining hierarchical data,
providing moderate access and insert/delete
operations.
Conclusion
Binary trees are special cases of tree where
every node has at most two children.

You might also like