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

NON-LINEAR DATA STRUCTURES

CO:4 Make use of linear data structures and nonlinear data structures
solving real-time applications.
Non-Linear Data Structure

• In a non-linear data structure, the elements are not arranged in


sequence.
• The data members are arranged in any manner. The data items are not
processed one After another.
• Trees and graphs are examples of non-linear data structures.
Linear Data Structure
Vs
Non-Linear Data Structure

• In linear data structures, data elements are organized sequentially and therefore
they are easy to implement in the computer’s memory.
• In nonlinear data structures, a data element can be attached to several other data
elements to represent specific relationships that exist among them.
Trees
• A tree is a finite nonempty set of
elements. Computers”R”U
• It is an abstract model of a hierarchical s
structure.
• consists of nodes with a parent-child
relation. Sale Manufacturin R&
• Applications: s g D
– Organization charts
– File systems
– Programming environments U Internationa Laptop Desktop
S l s s

Europ Asi Canad


e a a
Tree Terminology
• Root: node without parent (A) • Subtree: tree consisting of a node
• Siblings: nodes share the same parent and its descendants
• Internal node: node with at least one child (A, B,
C, F)
• External node (leaf ): node without children (E, I,
J, K, G, H, D)
• Ancestors of a node: parent, grandparent, grand- A
grandparent, etc.
• Descendant of a node: child, grandchild, grand-
grandchild, etc. B C D
• Depth of a node: number of ancestors
• Height of a tree: the number of edges from
the node to the deepest leaf. E F G H
• Degree of a node: the number of its children
• Degree of a tree: the maximum number of its
node. I J K
subtree
Tree Properties

A •Number of nodes
•Height
B C •Root Node
•Leaves
•Interior nodes
D E F •Ancestors of H
•Descendants of B
•Siblings of E
•Right subtree of A
G
•Degree of this tree

H I
Binary Tree

• A tree in which every node can have a maximum of two children is called as Binary Tree.
Binary Tree variants

• Full binary tree


• Complete binary tree
• Perfect binary tree
• Balanced binary tree
• Left-skewed and right-skewed binary tree
Full or strictly Binary Tree
• A binary tree in which every node has either two or zero number of children is called Full
Binary Tree

In a Full Binary, the number of leaf nodes is the number of internal nodes plus 1
L=I+1
Where L = Number of leaf nodes, I = Number of internal nodes
Complete Binary Tree
• A Binary Tree is a complete Binary Tree if all levels are completely filled except
possibly the last level and the last level has all keys as left as possible.
• Number of nodes = 2d+1 – 1
• Number of leaf nodes = 2d
• Where, d – Depth of the tree
Perfect Binary Tree
• A Binary tree is Perfect Binary Tree in which all internal nodes have two children
and all leaves are at the same level.
A Perfect Binary Tree of height h (where height is the number of nodes on the path from
the root to leaf) has 2h – 1 node.the
Balanced Binary Tree
• A balanced binary tree is a binary tree of height h such that the height of any
node’s right subtree and left subtree differ no more than 1. So it doesn't say
anything about it having to be completed from left to right.
Left-Skewed and Right-Skewed Trees
• Binary tree has only left sub trees - Left Skewed Trees
• Binary tree has only right sub trees - Right Skewed Trees
Binary Tree Representation

1. Sequential representation using arrays


2. List representation using Linked list
Sequential representation

• To represent a binary tree of depth ‘d' using array representation, we need one
dimensional array with a maximum size of 2d+1 - 1.
Sequential representation

• Advantages:
– Direct access to all nodes (Random access)
• Disadvantages:
– Height of tree should be known
– Memory may be wasted
– Insertion and deletion of a node is difficult
List representation

• Advantages:
– Height of tree need not be known
– No memory wastage
– Insertion and deletion of a node is done without affecting other nodes
• Disadvantages:
– Direct access to node is difficult
– Additional memory required for storing address of left and right node
Linked list representation
Binary tree traversal

• 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. Following
are the generally used ways for traversing trees.
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 (Left, Root, Right) : 4 2 5 1 3

Uses of Inorder
• In 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 s reversed can be used.
Preorder Traversal
• Algorithm Preorder(tree)
1. Visit the root
2.Traverse the left subtree, i.e., call Inorder(left-subtree)
3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Preorder (Root, Left, Right) : 1 2 4 5 3

Uses of preorder
• Preorder traversal is used to create a copy
of the tree.
• Preorder traversal is also used to get prefix
expression on of an expression tree.
Postorder Traversal
• Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Traverse the right subtree, i.e., call Inorder(right-subtree)
3. Visit the root

Postorder (Left, Right, Root) : 4 5 2 3 1


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.
Applications of trees
• One reason to use trees might be because you want to store information that
naturally forms a hierarchy.
• If we organize keys in the form of a tree (with some ordering e.g., BST), we can
search for a given key in moderate time (quicker than Linked List and slower
than arrays).
• We can insert/delete keys in moderate time.
• Heap is a tree data structure that is implemented using arrays and used to
implement priority queues.
• B-Tree and B+ Tree : They are used to implement indexing in databases.
• Syntax Tree: Used in Compilers.
Applications of trees
• K-D Tree: A space partitioning tree used to organize points in K-dimensional
space.
• Trie : Used to implement dictionaries with prefix lookup.
• Suffix Tree : For quick pattern searching in a fixed text.
• Manipulate hierarchical data.
• Make information easy to search (see tree traversal).
• Manipulate sorted lists of data.
• As a workflow for compositing digital images for visual effects.
• Router algorithms

You might also like