data structures 3

57
CS1301 Data Structures

Upload: parthipan-parthi

Post on 08-May-2015

301 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: Data structures 3

CS1301 Data Structures

Page 2: Data structures 3

Unit III Non LINEAR DATA STRUCTURE

Preliminaries Binary Tree The Search Tree ADT Binary Search Tree AVL Tree Tree Traversals Hashing - General Idea Hash Function Separate Chaining OpenAddressing Linear Probing Priority Queue (Heaps) - Model - Simple Implementations Binary Heap

Page 3: Data structures 3

Trees• Finite set of one or more nodes that there is a

specially designated node called root and zero or more non empty sub trees T1, T2,.. each of whose roots are connected by a directed edge from Root R

• A tree T is a set of nodes storing elements such that the nodes have a parent-child relationship that satisfies the following– if T is not empty, T has a special tree called the root

that has no parent– each node v of T different than the root has a unique

parent node w; each node with parent w is a child of w

Page 4: Data structures 3

Trees

Page 5: Data structures 3

Trees• Root : node which doesn’t have a parent• Node : Item of information• Leaf/ terminal : node which doesn’t have children • Siblings : children of same parents • Path : sequence of nodes n1,n2,n3,….,nk such

that ni is parent of ni+1 for 1<= i<k. there is exactly only one path from each node to root; path from A to L is A,C,F,L

• Length : no. of edges on the path ; length of A to L is 3

• Degree : number of subtrees of a node; A -> 4, C->2 degree of tree is maximum no.of any node in the tree ; degree of tree is 4

Page 6: Data structures 3

Trees• Level : initially letting the root be at level one,

if a node is at level L then its children are at level L + 1– Level of A is 1; Level of B, C, D is 2; Level of

F,G,H,I,J is 3, Level of K,L,M is 4

• Depth : For any node n, the depth of n is the unique path from root to n– Depth of root is 0; Depth of L is 3

• Height : For any node n, the height of the node is the length of the longest path from n to the leaf– Height of leaf is 0, height of node F is 1; height of L

is 0

Page 7: Data structures 3

CHAPTER 5 7

Level and Depth

K L

E F

B

G

C

M

H I J

D

A

Level

1

2

3

4

node (13)degree of a nodeleaf (terminal)nonterminalparentchildrensiblingdegree of a tree (3)ancestorlevel of a node

3

2 1 3

2 0 0 1 0 0

0 0 0

1

2 2 2

3 3 3 3 3 3

4 4 4

Page 8: Data structures 3

Trees

• Binary Tree–Tree in which no node can have more than two children

Page 9: Data structures 3

Trees

• Binary Tree Node Declaration

Struct Treenode{ int element;

Struct Treenode *Left;Struct TreeNode *Right;

};

Page 10: Data structures 3

Trees

• Binary Tree–Comparison between general tree & Binary Tree

General Tree Binary Tree

Has any no. of children Has not more than two children

Page 11: Data structures 3

Trees

• Full Binary Tree– Full binary tree of h has 2^h+1 -1 nodes

Page 12: Data structures 3

Trees• Complete Binary Tree– A complete binary tree is a binary tree,

which is completely filled, with the possible exception of the bottom level, which is filled from left to right

– A full binary tree can be a complete binary tree, but all complete binary tree is not a full binary tree

Page 13: Data structures 3

Trees• Representation of Binary Tree–Two ways• Linear Representation • Linked Representation

Page 14: Data structures 3

Trees• Linear Representation of

Binary Tree– Elements are represented using arrays– For any element in position i, the left

child is in position 2i, right child is in position (2i+1) and parent in position (i/2)

Page 15: Data structures 3

Trees• Linked Representation of Binary Tree– Elements are represented using pointers– Each node in linked representation has two fields

• Pointer to left subtree• Data field• Pointer to right subtree

– In leaf node, both pointer fields are assigned as NULL

Page 16: Data structures 3

Trees• Expression Tree– Is a binary tree– Leaf nodes are operands and interior

nodes are operators– Expression tree be traversed by

inorder , preorder, postorder traversal

Page 17: Data structures 3

Trees• Constructing an Expression Tree

1. Read one symbol at a time from postfix expression

2. Check whether symbol is an operand or operator

1. If operand, create a one –node tree and push a pointer on to the stack

2. If operator, pop two pointers from the stack namely T1 and T2 and form a new tree with root as the operator and T2 as a left child and T1 as a right child. A pointer to this new tree is pushed onto the stack

Page 18: Data structures 3

Trees• Example

Page 19: Data structures 3

Trees• Example

Page 20: Data structures 3

Trees• Binary Search Tree– Is a binary tree –For every node x in the tree, value of all the

keys in its left subtree are smaller than the value X and value of all the keys in the right subtree are larger than the value X

Page 21: Data structures 3

Trees• Binary Search Tree–Every binary search tree is a binary tree–All binary tree need not be bnary search tree

Page 22: Data structures 3

Trees• Declaration Routine for Binary

Search Tree

Page 23: Data structures 3

Trees• Routine for Make an empty tree

Page 24: Data structures 3

Trees• Insertion

Page 25: Data structures 3

Trees• Insertion - Example

Page 26: Data structures 3

Trees• Insertion - Example

Page 27: Data structures 3

Trees• Routine for Insertion

Page 28: Data structures 3

Trees• Find

Page 29: Data structures 3

Trees• Find – Example to find 10

Page 30: Data structures 3

Trees• Find – Example to find 10

Page 31: Data structures 3

Trees• Routine for Find

Page 32: Data structures 3

Trees• Find Minimum–Returns position of the smallest element

in a tree–Start at the root and go left as long as

there is a left child, the stopping point will be the smallest element

Page 33: Data structures 3

Trees• Find Minimum – example

Page 34: Data structures 3

Trees• Find Minimum – Recursive

routine

Page 35: Data structures 3

Trees• Find Minimum – Non

Recursive routine

Page 36: Data structures 3

Trees• Find Maximum–Returns the largest element in the tree–To perform, start at the root and go

right as long as there is a right child–Stopping point is the largest element

Page 37: Data structures 3

Trees• Find Maximum - Example

Page 38: Data structures 3

Trees• Find Maximum – Recursive

Routine

Page 39: Data structures 3

Trees• Find Maximum – Non

Recursive Routine

Page 40: Data structures 3

Trees• Deletion–Complex in BST–To delete an element, consider the following 3 possibilities•Node to be deleted is a leaf node •Node with one child•Node with two children

Page 41: Data structures 3

Trees• Deletion– Node to be deleted is a leaf node

Page 42: Data structures 3

Trees• Deletion

•Node with one child : deleted by adjusting its parent pointer that points to its child node

Page 43: Data structures 3

Trees• Deletion

•Node with two children : to replace the data of node to be deleted with its smallest data of right subtree and recursively delete that node

Page 44: Data structures 3

Trees• Deletion

•Node with two children

Page 45: Data structures 3

Trees• Deletion

•Node with two children

Page 46: Data structures 3

Trees• Deletion

•Node with two children

Page 47: Data structures 3

Trees• Deletion

•Node with two children

Page 48: Data structures 3

Trees• Deletion

•Node with two children

Page 49: Data structures 3

Trees• Deletion

•Node with two children

Page 50: Data structures 3

Trees• Deletion Routine

Page 51: Data structures 3

Trees• Deletion Routine

Page 52: Data structures 3

Trees• AVL Tree (Adelson - Velskill and Landis)–Binary search tree except that for every

node in the tree, the height of left and right subtrees can differ by atmost 1–Balance factor is the height of left

subtree minus height of right subtree; -1, 0, or +1– If Balance factor is less than or greater

than 1, the tree has to be balanced by making single or double rotations

Page 53: Data structures 3

Trees• AVL Tree (Adelson Velskill and

Landis)

Page 54: Data structures 3

Trees• AVL Tree (Adelson Velskill and

Landis)

Page 55: Data structures 3

Trees• AVL Tree (Adelson Velskill and Landis)– AVL tree causes imbalance, when anyone of the

following conditions occur• An insertion into the left subtree of the left child

of node• An insertion into the right subtree of the left

child of node• An insertion into the left subtree of the right

child of node• An insertion into the right subtree of the right

child of nodeImbalances can overcome by 1. Single rotation 2. Double rotation

Page 56: Data structures 3

Trees• AVL Tree (Adelson Velskill and

Landis)• Single rotation– Performed to fix case1 and case 4Case 1: An insertion into the left subtree of the left child of node

Page 57: Data structures 3

Trees• AVL Tree (Adelson Velskill and

Landis)• Single rotation– Performed to fix case1 and case 4Case 1: An insertion into the left subtree of the left child of node