abstract containers

Download abstract containers

Post on 23-Mar-2016

22 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

sequence/linear (1 to 1). first ith last. hierarchical (1 to many). graph (many to many). set. abstract containers. trees. hierarchical organization each item has 1 predecessor and 0 or more successors one item (root) has 0 predecessors - PowerPoint PPT Presentation

TRANSCRIPT

CS-240 Data Structures

abstract containershierarchical (1 to many)graph (many to many)first ith lastsequence/linear (1 to 1)set#1treeshierarchical organizationeach item has 1 predecessor and 0 or more successorsone item (root) has 0 predecessorsgeneral tree - no limit on number of successorsbinary tree - 2 successors (left and right)binary search tree is one use of a binary tree data structure (will deal with later)#tree terminologyThere is a uniquepath from the rootto each node.

Root is a level 0, childis at level(parent) + 1.

Depth/height of a treeis the length of the longest path.level 0

1

2

3

4#trees are recursiveEach node is the rootof a subtree.

Many tree processingalgorithms are bestwritten recursively.#binary treeEach node has two successorsone called the left childone called the right childleft child and/or right child may be emptyA binary tree is either - empty or - consists of a root and two binary trees, one called the left subtree and one called the right subtree#binary tree density (shape)a binary tree of depth n is complete ifflevels 1 .. n have all possible nodes filled-inlevel 1: 1level 2: 2level 3: 4level n: ?nodes at level n occupy the leftmost positionsmax nodes level at n: 2n-1max nodes in binary tree of depth n: 2n-1depth of binary tree with k nodes: >floor[log2 k]depth of binary tree with k nodes: subtree.root)Insert_aux (subtree.left, item) // try insert on right Else already in treeJust keep moving down the tree#deletion casesitem to be deleted is in a leaf nodepointer to its node (in parent) must be changed to NULLitem to be deleted is in a node with one empty subtreepointer to its node (in parent) must be changed to the non-empty subtreeitem to be deleted is in a node with two non-empty subtrees#31the easy cases362042122439452140Just prune itJust set its parent (42) to skip this node#32the hard case362042122439452140#33

the hard case362042122439452140replace with smallest in right subtree (its inorder successor)replace with largest in left subtree (its inorder predecessor)or#34Using method 1Replace with in-order predecessor362042122439452140#Method 2Replace with in-order successor

362042122439452140#big O of BST operationsmeasured by length of the search pathdepends on the height of the BSTheight determined by order of insertionheight of a BST containing n items is minimum: floor (log2 n)maximum: n - 1average: ?#37faster searching"balanced" search trees guarantee O(log2 n) search path by controlling height of the search treeAVL tree2-3-4 treered-black tree (used by STL associative container classes)hash table allows for O(1) search performancesearch time does not increase as n increases#