Non Linear Data Structure
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
• 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.
• Leaf − Node which does not have any child node is called leaf 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.
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.
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.
• 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
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:
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
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