51658218 non linear data structures
TRANSCRIPT
-
8/2/2019 51658218 Non Linear Data Structures
1/50
Non-linear data structures
TREES
-
8/2/2019 51658218 Non Linear Data Structures
2/50
TREES
Non-linear data structure
Use to represent hierarchicalrelationship among existing data
items.
-
8/2/2019 51658218 Non Linear Data Structures
3/50
-
8/2/2019 51658218 Non Linear Data Structures
4/50
-
8/2/2019 51658218 Non Linear Data Structures
5/50
Trees TerminologyNode Each data item in a tree.
Root no parent. The topmostnode (A)
child nodes Each node in a tree
has zero or more nodes B,C,D
A
B C
E F G H
D
Root node
Leaf nodes
Interior nodes Height
edge
-
8/2/2019 51658218 Non Linear Data Structures
6/50
Terminology parent node A node that has a child is
called the child's parent node
An internal node or inner node
is any node of a tree that has childnodes.
Depth of tree distance from root toleaf node. In given tree it is 3.
The depth ofa node n is the length
of the path from the root to the node
-
8/2/2019 51658218 Non Linear Data Structures
7/50
Degree of node :
It is the number of subtrees of nodein given tree. Degree of node B is
one, A & C is 3.Degree of tree
It is the maximum degree of nodes in
given tree.
In given tree degree of node b is oneand A&C is 3 so max degree is 3
-
8/2/2019 51658218 Non Linear Data Structures
8/50
Non terminal nodes Any node whosedegree is not zero
Leaf or Terminal node no child
Siblings
The children nodes of given parent node.They are also called brothers
-
8/2/2019 51658218 Non Linear Data Structures
9/50
Level
The entire tree is leveled in suchway that root node is at level 0, its
immediate children are at level 1and their immediate children atlevel two and so on.
i.e. if node is at level n, then itschildren will be at level n+1.
-
8/2/2019 51658218 Non Linear Data Structures
10/50
Edge
Line drawn from one node toanother node
Path
It is the sequence of consecutiveedges from source to thedestination node.
The path between A to F is (A,C)
-
8/2/2019 51658218 Non Linear Data Structures
11/50
-
8/2/2019 51658218 Non Linear Data Structures
12/50
Strictly binary trees
A binary tree is Strictly binary treeif and only if :-
Each node has exactly two child
nodes or no nodes
-
8/2/2019 51658218 Non Linear Data Structures
13/50
full binary
A binary tree is afull binary tree ifand only if
Each non leafnode has exactlytwo child nodes
All leaf nodes areat the same level
-
8/2/2019 51658218 Non Linear Data Structures
14/50
Complete binary treeA binary tree is a complete binary tree (of
height h , we take root node as 0 ) if and only if:-
Level 0 to h-1 represent a full binary tree ofheight h-1
One or more nodes in level h-1 may have 0,or 1 child nodes If j ,k are nodes in level h-1, then j has morechild nodes than k if and only if j is to the left ofk
-
8/2/2019 51658218 Non Linear Data Structures
15/50
Extended binary tree
A binary tree T is said to be 2-tree orextended binary tree if each nodeN has either 0 or 2 children.
In such case, node with 2 children are
called internal nodes and the nodeswith 0 children are called externalnodes.
Binary tree
extendedbinarytree
-
8/2/2019 51658218 Non Linear Data Structures
16/50
Binary tree representation
Root node index starts with 0
A
B C
D E F G
0
21
3 4 5 6
0 A
1 B
2 C3 D
4 E
5 F
6 G
-
8/2/2019 51658218 Non Linear Data Structures
17/50
LINK Representation
Each node consist ofData
Left child
Right child
Lchild Data Rchild
-
8/2/2019 51658218 Non Linear Data Structures
18/50
LINK Representation
a
cb
d
f
e
g
hleftChildelement
rightChild
root
-
8/2/2019 51658218 Non Linear Data Structures
19/50
Maximum Number OfNodes
Maximum number ofnodes
= 1 + 2 + 4 + 8 + +2h-1
A full binary tree of a given height hhas 2h 1 nodes.
-
8/2/2019 51658218 Non Linear Data Structures
20/50
It is the way in which each node inthe tree is visited exactly once insystematic manner.
Used to represent arithmeticexpressions.
Preorder
In order
Post order
Binary Tree TraversalMethods
-
8/2/2019 51658218 Non Linear Data Structures
21/50
Tree traversals
The order in which the nodes are visited duringa tree traversal,
preorderpreorder inorderinorder postorderpostorder
In preorder, the root is visited first
In inorder, the root is visited in the middle
In postorder, the root is visited last
-
8/2/2019 51658218 Non Linear Data Structures
22/50
Pre-order traversalStart at the root node
Traverse the left subtree
Traverse the right subtree
The nodes of this treewould be visited in the
order :
D B A C F E G
-
8/2/2019 51658218 Non Linear Data Structures
23/50
-
8/2/2019 51658218 Non Linear Data Structures
24/50
-
8/2/2019 51658218 Non Linear Data Structures
25/50
-
8/2/2019 51658218 Non Linear Data Structures
26/50
Post-order traversalTraverse the left subtree
Traverse the right subtree
Visit the root node
A C B E G F D
-
8/2/2019 51658218 Non Linear Data Structures
27/50
Post-order
-
8/2/2019 51658218 Non Linear Data Structures
28/50
Conversion of expressioninto binary treeA+B
-
8/2/2019 51658218 Non Linear Data Structures
29/50
1.A+B*C2. A/B-C
-
8/2/2019 51658218 Non Linear Data Structures
30/50
1.(A-B)+C2. A-(B+C)
-
8/2/2019 51658218 Non Linear Data Structures
31/50
(A+B)*(C-D)
raw ree:
-
8/2/2019 51658218 Non Linear Data Structures
32/50
A/B+C/D
(A+B+C)*(D+E+F)
raw ree:-Write preorder, postorder,
inorder
-
8/2/2019 51658218 Non Linear Data Structures
33/50
Inorder: EACKFHDBGpreorder: FAEKCDHGB
FA D
E K H GC B
-
8/2/2019 51658218 Non Linear Data Structures
34/50
Binary Search Tree
The value of the key in the leftchild or left subtree is less thanvalue of the root
The value of the key in rightsubtree is more than or equal tothe value of the root.
All subtree of the left and rightchildren observe these two rules.
-
8/2/2019 51658218 Non Linear Data Structures
35/50
binary search tree (BST) is anode based binary tree data
structure
sometimes also be called orderedor sorted binary tree
Searching
-
8/2/2019 51658218 Non Linear Data Structures
36/50
SearchingCan be recursive or iterative process.
begin by examining the root node. If the tree is null, thevalue we are searching for does not exist in the tree.
Otherwise, if the value equals the root, the search issuccessful.
If the value is less than the root, search the left subtree.
Similarly, if it is greater than the root, search the rightsubtree.
This process is repeated until the value is found or theindicated subtree is null.
If the searched value is not found before a null subtreeis reached, then the item must not be present in thetree.
-
8/2/2019 51658218 Non Linear Data Structures
37/50
insertion
Check the root and recursivelyinsert the new node to the leftsubtree if the new value is less
than the root, or the right subtree ifthe new value is greater than theroot.
-
8/2/2019 51658218 Non Linear Data Structures
38/50
Inserts the node pointed to by"newNode" into the subtree rooted
at "treeNode" */void InsertNode(Node* &treeNode,Node *newNode)
{
if (treeNode == NULL)treeNode = newNode;else if (newNode->key < treeNode->key) InsertNode(treeNode->left,
newNode);else InsertNode(treeNode->right,newNode);}
-
8/2/2019 51658218 Non Linear Data Structures
39/50
Deletion
Deleting a leaf (node with nochildren): Deleting a leaf is easy,as we can simply remove it fromthe tree.
Deleting a node with one child:Remove the node and replace itwith its child.
Deleting a node with twochildren: Call the node to bedeleted N. Do not delete N.
Instead, choose either its in-order
-
8/2/2019 51658218 Non Linear Data Structures
40/50
-
8/2/2019 51658218 Non Linear Data Structures
41/50
B-trees
B-trees are balanced sort trees thatare optimized for situations whenpart or all of the tree must bemaintained in secondary storage
such as a magnetic disk.
The B-tree algorithm minimizes thenumber of times a medium must
be accessed to locate a desiredrecord, thereby speeding up theprocess.
-
8/2/2019 51658218 Non Linear Data Structures
42/50
B-tree
A binary-tree, each node of a b-treemay have a variable number ofkeys and children.
The keys are stored in non-decreasing order
-
8/2/2019 51658218 Non Linear Data Structures
43/50
Each key has an associated child thatis the root of a subtree containing allnodes with keys less than or equal tothe key but greater than thepreceeding key.
A node also has an additionalrightmost child that is the root for a
subtree containing all keys greaterthan any keys in the node.
-
8/2/2019 51658218 Non Linear Data Structures
44/50
Searching techniques
-
8/2/2019 51658218 Non Linear Data Structures
45/50
Searching techniques
Process of finding elements within listof elements.
Two categories:-
Linear search No prerequisite
Binary search
List must be sorted.
-
8/2/2019 51658218 Non Linear Data Structures
46/50
Linear search
Access an element of an array oneby one sequentially.
Search unsuccessful if element is
not found.
Average case, we may have toscan half of the list.
-
8/2/2019 51658218 Non Linear Data Structures
47/50
Binary search
Searches item in min comparisons,This tech,
First find middle element (mid=first
+ last)/2)Compare mid element with an item
There are three cases,
If mid element=search element thensearch is successful.
If it is less than desired item then search
only first half array, beg=mid-1
-
8/2/2019 51658218 Non Linear Data Structures
48/50
9,12,24,30,36,45,70
Item=45
Beg=0 and last=6
Mid=int(0+6)/2=3
A[3]=30
45>30 then
Beg=mid+1=3+1
=4
-
8/2/2019 51658218 Non Linear Data Structures
49/50
New mid is,
Int(4+6)=5
A[5]=45
Search is successful
-
8/2/2019 51658218 Non Linear Data Structures
50/50
12,14,25,32,35,40,45,48,50Item search=25