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

Non Linear Data Structure

The document discusses non-linear data structures and binary search trees. It provides definitions and examples of key concepts related to trees, including that trees are hierarchical structures with parent-child relationships, and define terms like root, child, leaf, and subtree. It also summarizes operations on binary search trees like search, insertion, deletion and finding the minimum and maximum values. Binary search trees have the property that all values in the left subtree are less than the root and all values in the right subtree are greater than the root.

Uploaded by

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

Non Linear Data Structure

The document discusses non-linear data structures and binary search trees. It provides definitions and examples of key concepts related to trees, including that trees are hierarchical structures with parent-child relationships, and define terms like root, child, leaf, and subtree. It also summarizes operations on binary search trees like search, insertion, deletion and finding the minimum and maximum values. Binary search trees have the property that all values in the left subtree are less than the root and all values in the right subtree are greater than the root.

Uploaded by

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

Non Linear Data Structure

Tree
So far we discussed Linear data structures
like

stack
Introduction to
trees
•linear data structures are – arrays, lists, stacks and queues

• Now we will discuss a non-linear data structure called tree.

• Trees are mainly used to represent data containing a hierarchical


relationship between elements, for example, records, family trees
and table of contents.

• Consider a parent-child relationship where we have show a tree like


DS
Tre
e
• A tree is an abstract model of a hierarchical structure that consists of
nodes with a parent-child relationship.
• Tree is a sequence of nodes

• There is a starting node known as a root node

• Every node other than the root has a parent node.

• Nodes may have any number of children


Some Key
•Terms:
Root − Node at the top of the tree is called root.

• Parent − Any node except root node has one edge upward to a node called parent.

• Child − Node below a given node connected by its edge downward is called its child node.

• Sibling – Child of same node are called siblings

• Leaf − Node which does not have any child node is called leaf node.

• Sub tree − Sub tree represents descendants of a node.

• Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child
node is at level 1, its grandchild is at level 2 and so on.

• keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
Some Key Terms:
• Degree of a node:
• The degree of a node is the number of children of that node
• Degree of a Tree:
• The degree of a tree is the maximum degree of nodes in a given tree
• Path:
• It is the sequence of consecutive edges from source node to
destination node.
• Height of a node:
• The height of a node is the max path length form that node to a leaf
node.
• Height of a tree:
• The height of a tree is the height of the root
Characteristics of
trees
• Non-linear data structure
• Combines advantages of an ordered array
• Searching as fast as in ordered array
• Insertion and deletion as fast as in linked
list
• Simple and fast
Application
• Directory structure of a file store
• Structure of an arithmetic expressions
• Used in almost every 3D video game to determine what objects need to
be rendered.
• Used in almost every high-bandwidth router for storing router-tables.
• used in compression algorithms, such as those used by the .jpeg and .mp3
file- formats.
Introduction To Binary Trees
• A binary tree, is a tree in which no node can have more than
two children i.e every node have at most two children
• Each node is labeled as being either a left child or a right
child.
• Consider a binary tree T, here ‘A’ is the root node of the binary
tree T.
Binary
•Trees
A binary tree, T, is either empty or such that
I. T has a special node called the root node
II. T has two sets of nodes LT and RT, called the left subtree and right
subtree of T, respectively
III.
III LT and RT are binary trees.
.
The following figure shows a binary tree with 9 nodes where A is the
root
Complete binary
tree
• A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are
as far left as possible.
• A complete binary tree has 2r nodes at every level r and 2d -1
non leaf nodes
Extended Binary Tree
• A binary tree T is said to be a 2-tree or an extended binary
tree if each node N has either 0 or 2 children.

• In such a case, the nodes, with 2 children are called internal


nodes, and the node with 0 children are called external node.

Binary Tree Extended 2-tree


Binary Trees: Array representation
Sequential Representation of binary Trees uses only a single linear array TREE together
with a pointer variable END as follows:
(a) The root R of T is stored in TREE[1].
(b) If a node occupies TREE[k], then its left child is stored in TREE[2 * K] and its right child is
stored in TREE[2*k+1]
(c) END contains the location of the last node of T.
Binary Trees: Array Representation

1
14
2 3
10 16
4 5 6 7
8 12 15 18
8 9 10 11
7 9 11 13

1 2 3 4 5 6 7 8 9 10 11
Array A: 14 10 16 8 12 15 18 7 9 11 13
Linked Representation of Binary Tree
The most popular way to present a binary tree
Each element is represented by a node that has two link fields ( leftChild and rightChild )
plus an Info field
The space required by an n node binary tree is
n * sizeof(binaryTreeNode)

20
Linked Representation of Binary trees
1. INFO[K] contains the data at the node N
2. LEFT[K] contains the location of the left child of node N
3. RIGHT[K] contains the location of the right child of node N.

Fig : 7-6
Tree
•traversal
Traversal is a process to visit all the nodes of a tree and may print
their values too.

• All nodes are connected via edges (links) we always start from the
root (head) node.

• There are three ways which we use to traverse a tree


• In-order Traversal
• Pre-order Traversal
• Post-order Traversal

• Generally we traverse a tree to search or locate given item or key in the


tree or to print all the values it contains.
Pre-order
•Traversal
The preorder traversal of a nonempty binary tree is defined as
follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder
Pre-order
Pseudocode
struct
Node{ char
data; Node
*left; Node
*right;
}
void
Preorder(Nod
e *root)
{
if (root==NULL) return;
printf (“%c”, root-
>data);
Preorder(root->left);
In-order
traversal
•The in-order traversal of a nonempty binary tree is defined as
follows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder

• The in-order traversal output of


the given tree is
HDIBEAFCG
In-order
Pseudocode
struct
Node{ char
data; Node
*left; Node
*right;
}
void
Inorder(Node
*root)
{
if (root==NULL) return;
Inorder(root->left);
printf (“%c”, root->data);
Inorder(root->right);
Post-order
traversal
•The in-order traversal of a nonempty binary tree is defined as
follows:
• Traverse the left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node

• The in-order traversal


output of the given tree is
HIDEBFGCA
Post-order
Pseudocode
struct
Node{ char
data; Node
*left; Node
*right;
}
void
Postorder(Nod
e *root)
{
if (root==NULL) return;
Postorder(root->left);
Postorder(root->right);
printf (“%c”, root-
Traversing of Binary Tree

 Write the preorder, inorder and post order traversing of the


above binary tree.
Binary Search Tree (BST)

• A binary search tree (BST) is a binary tree that is either empty or in


which every node contains a key (value) and satisfies the following
conditions:
• All keys in the left sub-tree of the root are smaller than the key in the
root
node
• All keys in the right sub-tree of the root are greater than the key in the root
node
• The left and right sub-trees of the root are again binary search trees
Binary Search Trees

32
Binary Search Tree (BST)
Binary Search Tree (BST)
Following figure shows a binary search tree. Notice that this tree is obtained by
inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order,
starting from an empty tree.
Operations on Binary Search Tree
(BST)
•Following operations can be done in BST:

• Search(k, T): Search for key k in the tree T. If k is found in some node of
tree then return true otherwise return false.

• Insert(k, T): Insert a new node with value k in the info field in the tree T
such
that the property of BST is maintained.

• Delete(k, T):Delete a node with value k in the info field from the tree T
such that the property of BST is maintained.

• FindMin(T), FindMax(T): Find minimum and maximum element from


the given nonempty BST.
Searching Through The
BST
•Compare the target value with the element in the root
node
 If the target value is equal, the search is successful.
If target value is less, search the left subtree.
If target value is greater, search the right subtree.
If the subtree is empty, the search is unsuccessful.
(Con..)
Insertion of a node in
BST
•To insert a new item in a tree, we must first verify that its key is
different from those of existing elements.

• If a new value is less, than the current node's value, go to the left
subtree, else go to the right subtree.

• Following this simple rule, the algorithm reaches a node, which has no
left or right subtree.

• By the moment a place for insertion is found, we can say for sure,
that a new value has no duplicate in the tree.
• If any duplicate is found than insert the new item on the right empty
subtree.
Building a BST

Build a BST from a sequence of nodes read one a time

Example: Inserting C A B L M (in this order!)

1) Insert C 2) Insert A
C
C
A
Building a BST

3) Insert B C
5) Insert M
A
C
B
A L

4) Insert L C B M

A L

B
Deleting a node from the
BST
•While deleting a node from BST, there may be three cases:
1. The node to be deleted may be a leaf node:
• In this case simply delete a node and set null pointer to its parents those
side at which this deleted node exist.
Deleting a node from the
BST
2. The node to be deleted has one child
• In this case the child of the node to be deleted is appended to its parent
node.
Suppose node to be deleted is 18
Deleting a node from the
BST
Binary Search
Tree(BST)
Time Complexity
Array Linked List BST
Search O(n) O(n) O(logn)
Insert O(n) O(1) O(logn)
Remove O(n) O(1) O(logn)
Heap
• A heap tree is a complete binary tree in which data values stored in any node
is greater than or equal to the value of its children(if any)
• The value stored in the root node of a heap tree is always the largest value in
the tree. Such a heap tree is called a max-heap.
• If the value stored in the root node is guaranteed to be smallest then such
tree is known as min-heap
Heap

Heap shape:
Max Heap
A Max heap (Ascending heap) is an almost complete binary tree in which the value
at each parent node is greater than or equal to the values in its child nodes.
Obviously, the maximum value is in the root node.
Note, too, that any path from a leaf to the root passes through the data in
Ascending order.
Here is an example of a max heap:

88

72 55

70 44 30 50

50 66 22 33 25
Max Heap with 9 Nodes
Max Heap with 12 Nodes
Min Heap with 9 Nodes
A minimal heap (descending heap) is an almost complete binary tree in which the
value at each parent node is less than or equal to the values in its child nodes.
Obviously, the minimum value is in the root node.
Note, too, that any path from a leaf to the root passes through the data in
descending order.
Here is an example of a minimal heap:

Complete binary tree with 9 nodes


Which are min-heaps?

wrong! 10 wrong! 20wrong!


10
20 80 10 80
20 80
40 60 85 99 40 60 85 99
30 15
50 700 50 700
10

20 80
10 10 wrong!

20 80 20 80
40 60 85 99
40 60 40 60 99
50 700
Which are max-heaps?

wrong!
30 48 80

10 20 21 10 25 30
14 24

50
33 30
30 35
10 17 10 40
22 28 18 9
7 3
wrong! 11
Heap
• Lets have an example
For input 35 33 42 10 14 19 27 44 26 31
• Max Heap

• Min Heap
Max Heap Construction
• Now, We are going to derive an algorithm for max heap by
inserting one element at a time
• At any point of time, heap must maintain its property
• Let's understand Max Heap construction by an animated
illustration. We consider the same input sample that we used
earlier.
Max Heap Construction

Step 1: Create a new node at the end of the heap


Step 2: Assign new value to the node.
Step 3: Compare the value of this child node with its
parents
Step 4: If the value of the parent is less than child ,
then swap them
Step 5: Repeat step 3 & 4 until heap property holds
Building a Heap

EXAMPLE 7.21
Build a heap H from the following list of numbers:
44, 30, 50, 22, 60, 55,77, 55

60
44 44 50 50

50 44
44 30 44
30 30
22 30
22
Building a Heap

60 77 77

50 55 50 60 55 60

22 30 44 22 30 44 55 50 30 44 55

22
General Trees
A general tree (tree) is defined to be a nonempty finite set T of elements, called nodes,
such that:
•T contains a distinguished element R, called the root of T
•The remaining elements of T form an ordered collection of zero or more disjoint trees T1,
T2, …….., Tm.
•Trees T1, T2, …….., Tm are called subtrees of the root R, and the roots of T1, T2, ……..,
Tm are called successors of R.
Thanks All

You might also like