Non-linear data structures

50
Non-linear data structures TREES

Transcript of Non-linear data structures

Page 1: Non-linear data structures

Non-linear data structuresTREES

Page 2: Non-linear data structures

TREESNon-linear data structureUse to represent hierarchical

relationship among existing data items.

Page 3: Non-linear data structures

Tree : definitionIt is finite set of one or more data

items such that◦There is special data item called as

root of the tree◦And remaining data items are

partitioned into number of mutually exclusive subsets each of which is itself a tree. They are called subtrees.

Page 4: Non-linear data structures

Trees Data StructuresTree

◦Nodes◦Each node can have 0 or more

children◦A node can have at most one parent

Binary tree◦Tree with 0–2 children per node

Tree Binary Tree

Page 5: Non-linear data structures

Trees Terminology◦Node Each data item in a tree.

◦Root no parent. The topmost node (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

Page 6: Non-linear data structures

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 child nodes.

Depth of tree distance from root to leaf node. In given tree it is 3.

The depth of a node n is the length of the path from the root to the node

height of a tree is the length of the path from the root to the deepest node in the tree. A tree with only one node (the root) has a height of zero

Page 7: Non-linear data structures

Degree of node :◦It is the number of subtrees of node

in 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 one

and A&C is 3 so max degree is 3

Page 8: Non-linear data structures

• Non terminal nodes Any node whose degree is not zero

• Leaf or Terminal node no child

• Siblings• The children nodes of given parent node.

They are also called brothers

Page 9: Non-linear data structures

Level◦The entire tree is leveled in such way

that root node is at level 0, its immediate children are at level 1 and their immediate children at level two and so on.

◦i.e. if node is at level n, then its children will be at level n+1.

Page 10: Non-linear data structures

Edge◦Line drawn from one node to another node

Path◦ It is the sequence of consecutive edges

from source to the destination node. ◦The path between A to F is (A,C) and (C,F)

Forest◦ It is the set of disjoint trees. If you remove

root node then it become forest of trees.

Page 11: Non-linear data structures

Binary treeSet of data items with

Root node Left subtreeRight subtree

Tree with 0–2 children per node

Page 12: Non-linear data structures

Strictly binary treesA binary tree is Strictly binary tree

if and only if :- – Each node has exactly two child

nodes or no nodes

Page 13: Non-linear data structures

full binaryA binary tree is

a full binary tree if and only if◦Each non leaf

node has exactly two child nodes

◦All leaf nodes are at the same level

Page 14: Non-linear data structures

Complete binary tree A 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 of height

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 more child nodes than k if and only if j is to the left of k

Page 15: Non-linear data structures

Extended binary treeA binary tree T is said to be 2-tree or

extended binary tree if each node N has either 0 or 2 children.

In such case, node with 2 children are called internal nodes and the nodes with 0 children are called external nodes.

Binary tree

extended binary tree

Page 16: Non-linear data structures

Binary tree representationRoot node index starts with 0

A

B C

D E F G

0

21

3 4 5 6

0 A

1 B

2 C

3 D

4 E

5 F

6 G

Page 17: Non-linear data structures

LINK RepresentationEach node consist of

◦Data◦Left child ◦Right child

Lchild Data Rchild

Page 18: Non-linear data structures

LINK Representation

a

cb

d

f

e

g

hleftChildelementrightChild

root

Page 19: Non-linear data structures

Maximum Number Of Nodes

Maximum number of nodes

= 1 + 2 + 4 + 8 + … + 2h-1

= 2h - 1

A full binary tree of a given height h has 2h – 1 nodes.

Page 20: Non-linear data structures

It is the way in which each node in the tree is visited exactly once in systematic manner.

Used to represent arithmetic expressions.

Preorder In orderPost order

Binary Tree Traversal Methods

Page 21: Non-linear data structures

Tree traversalsThe order in which the nodes are visited

during a tree traversal,

preorderpreorder inorderinorder postorder

postorder

In preorder, the root is visited first

In inorder, the root is visited in the middle

In postorder, the root is visited last

Page 22: Non-linear data structures

Pre-order traversal

Start at the root node Traverse the left subtree Traverse the right subtree

The nodes of this tree would be visited in the order :

D B A C F E G

Page 23: Non-linear data structures
Page 24: Non-linear data structures

In-order traversal

Traverse the left subtree Visit the root node Traverse the right subtree

A B C D E F G

Page 25: Non-linear data structures
Page 26: Non-linear data structures

Post-order traversal

Traverse the left subtree Traverse the right subtree Visit the root node

A C B E G F D

Page 27: Non-linear data structures

Post-order

Page 28: Non-linear data structures

Conversion of expression into binary treeA+B

Page 29: Non-linear data structures

1.A+B*C2. A/B-C

Page 30: Non-linear data structures

1.(A-B)+C2. A-(B+C)

Page 31: Non-linear data structures

(A+B)*(C-D)

Page 32: Non-linear data structures

A/B+C/D

(A+B+C)*(D+E+F)

Draw tree:-Write preorder, postorder, inorder

Page 33: Non-linear data structures

Inorder: EACKFHDBGpreorder: FAEKCDHGB

F A DE K H G C B

Page 34: Non-linear data structures

Binary Search TreeThe value of the key in the left

child or left subtree is less than value of the root

The value of the key in right subtree is more than or equal to the value of the root.

All subtree of the left and right children observe these two rules.

Page 35: Non-linear data structures

binary search tree (BST) is a node based binary tree data structure

sometimes also be called ordered or sorted binary tree

Page 36: Non-linear data structures

SearchingCan be recursive or iterative process. begin by examining the root node. If the tree

is null, the value we are searching for does not exist in the tree.

Otherwise, if the value equals the root, the search is successful.

If the value is less than the root, search the left subtree.

Similarly, if it is greater than the root, search the right subtree.

This process is repeated until the value is found or the indicated subtree is null.

If the searched value is not found before a null subtree is reached, then the item must not be present in the tree.

Page 37: Non-linear data structures

insertionCheck the root and recursively

insert the new node to the left subtree if the new value is less than the root, or the right subtree if the new value is greater than the root.

Page 38: Non-linear data structures

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); }

Page 39: Non-linear data structures

DeletionDeleting a leaf (node with no

children): Deleting a leaf is easy, as we can simply remove it from the tree.

Deleting a node with one child: Remove the node and replace it with its child.

Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Replace the value of N with the value of R, then delete R.

Page 40: Non-linear data structures

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.

Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays

Page 41: Non-linear data structures

B-trees B-trees are balanced sort trees that are

optimized for situations when part or all of the tree must be maintained in secondary storage such as a magnetic disk.

The B-tree algorithm minimizes the number of times a medium must be accessed to locate a desired record, thereby speeding up the process.

Page 42: Non-linear data structures

B-treeA binary-tree, each node of a b-

tree may have a variable number of keys and children.

The keys are stored in non-decreasing order

Page 43: Non-linear data structures

Each key has an associated child that is the root of a subtree containing all nodes with keys less than or equal to the key but greater than the preceeding key.

A node also has an additional rightmost child that is the root for a subtree containing all keys greater than any keys in the node.

Page 44: Non-linear data structures

Searching techniques

Page 45: Non-linear data structures

Searching techniquesProcess of finding elements

within list of elements.Two categories:-

◦Linear search No prerequisite

◦Binary search List must be sorted.

Page 46: Non-linear data structures

Linear searchAccess an element of an array

one by one sequentially.Search unsuccessful if element is

not found.Average case, we may have to

scan half of the list.

Page 47: Non-linear data structures

Binary searchSearches 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 then search is successful.

If it is less than desired item then search only first half array, beg=mid-1

If it is greater than desired item then search only in the second half array. beg=mid+1

Page 48: Non-linear data structures

9,12,24,30,36,45,70

Item=45Beg=0 and last=6Mid=int(0+6)/2=3A[3]=3045>30 then Beg=mid+1=3+1=4

Page 49: Non-linear data structures

New mid is,Int(4+6)=5A[5]=45Search is successful

Page 50: Non-linear data structures

12,14,25,32,35,40,45,48,50Item search=25