non-linear data structures

Post on 25-Nov-2014

685 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Non-linear data structuresTREES

TREESNon-linear data structureUse to represent hierarchical

relationship among existing data items.

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.

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

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

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

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

• 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

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.

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.

Binary treeSet of data items with

Root node Left subtreeRight subtree

Tree with 0–2 children per node

Strictly binary treesA binary tree is Strictly binary tree

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

nodes or no nodes

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

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

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

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

LINK RepresentationEach node consist of

◦Data◦Left child ◦Right child

Lchild Data Rchild

LINK Representation

a

cb

d

f

e

g

hleftChildelementrightChild

root

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.

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

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

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

In-order traversal

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

A B C D E F G

Post-order traversal

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

A C B E G F D

Post-order

Conversion of expression into binary treeA+B

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

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

(A+B)*(C-D)

A/B+C/D

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

Draw tree:-Write preorder, postorder, inorder

Inorder: EACKFHDBGpreorder: FAEKCDHGB

F A DE K H G C B

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.

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

sometimes also be called ordered or sorted binary tree

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.

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.

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

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.

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

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.

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

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.

Searching techniques

Searching techniquesProcess of finding elements

within list of elements.Two categories:-

◦Linear search No prerequisite

◦Binary search List must be sorted.

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.

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

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

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

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

top related