1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear...
-
Upload
edward-clemson -
Category
Documents
-
view
232 -
download
0
Transcript of 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear...
![Page 1: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/1.jpg)
1
abstract containers
hierarchical (1 to many)
graph (many to many)first ith last
sequence/linear (1 to 1)
set
![Page 2: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/2.jpg)
2
trees
hierarchical organization each item has 1 predecessor and 0 or
more successors one item (root) has 0 predecessors
general tree - no limit on number of successors
binary tree - 2 successors (left and right)
binary search tree is one use of a binary tree data structure (will deal with later)
![Page 3: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/3.jpg)
3
tree terminology
There 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
![Page 4: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/4.jpg)
4
trees are recursive
Each node is the rootof a subtree.
Many tree processingalgorithms are bestwritten recursively.
![Page 5: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/5.jpg)
5
binary tree Each node has two successors
one called the left child one called the right child left child and/or right child may be empty
A 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
![Page 6: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/6.jpg)
6
binary tree density (shape)
a binary tree of depth n is complete iff levels 1 .. n have all possible nodes filled-in
level 1: 1 level 2: 2 level 3: 4 level n: ?
nodes at level n occupy the leftmost positions max nodes level at n: 2n-1
max nodes in binary tree of depth n: 2n-1 depth of binary tree with k nodes: >floor[log2
k] depth of binary tree with k nodes: <ceil[log2
k] longest path is O(log2 k)
![Page 7: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/7.jpg)
7
representing a binary tree
have to store items and relationships (predecessor and successors information)
array representation each item has a position number (0 .. n-1) use the position number as an array index
O(1) access to parent and children of item at position i wastes space unless binary tree is complete
linked representation each item stored in a node which contains:
the item a pointer to the left subtree a pointer to the right subtree
![Page 8: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/8.jpg)
8
array representation
each item has a position number root is at position 0 root's left child is at position 1 root's right child is at position 2
in general: left child of i is at 2i + 1 right child of i is at 2i + 2 parent of i is at (i-1)/2
0
1 2
3 4 5 6
0 1 2 3 4 5 6 - - - - - - -
0
1 2
6
13
works well if n is known in advance and there are no "missing" nodes
![Page 9: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/9.jpg)
9
linked representation
class binTreeNode {public: T item; binTreeNode * left; binTreeNode * right; binTreeNode (const T & value) { item = value; left = NULL; right = NULL; }};binTreeNode * root;
root
left child of *p?right child of *p?parent of *p?
p
![Page 10: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/10.jpg)
10
why is a binary tree so useful?
for storing a collection of values that has a binary hierarchical organization arithmetic expressions boolean logic expressions Morse or Huffman code trees
data structure for a binary search tree
general tree can be stored using a binary tree data structure
![Page 11: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/11.jpg)
11
an expression tree an arithmetic
expression consists of an operator and two operands (either of which may be an arithmetic expression)
some simplifications only binary operators
(+, *, /, -) only single digit, non-
negative integer operands
*
- +
5 2 6 1
operands are stored in leaf nodesoperators are stored in branch nodes
![Page 12: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/12.jpg)
12
traversing a binary tree
what does it mean to traverse a container? "visit" each item once in some order
many operations on a collection of items involve traversing the container in which they are stored displaying all items making a copy of a container
linear collections (usually) traversed from first to last last to first is an alternative traversal order
![Page 13: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/13.jpg)
13
binary tree traversals
order in which to "visit" the items?some common orders - visit an
item before its children (preorder) after its children (postorder) between its children (inorder) level by level (level order)
by convention go left before right
![Page 14: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/14.jpg)
14
traversing an expression tree
4
*
+ *
1 3 - 2
1
preorder (1st touch) * + 1 3 * - 4 1 2
inorder (2nd touch) 1 + 3 * 4 - 1 * 2
postorder (last touch) 1 3 + 4 1 - 2 * *
![Page 15: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/15.jpg)
15
expression tree traversal
traverse (ExprTree) { if (root of ExprTree holds a digit) //is a leaf node visit (root); else // root holds an operand { visit (root) traverse (ExprTree's left subtree) traverse (ExprTree's right subtree) }}
![Page 16: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/16.jpg)
16
expression tree operations
use "recursive partners" allow recursive calls to deal with a subtree
(identifed by a pointer to its root node) build uses a preorder traversal copy uses a preorder traversal postfix uses a postorder traversal clear uses a postorder traversal what kind of traversal does infix need? what kind of traversal does evaluate
need?
![Page 17: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/17.jpg)
17
Associative Containers
fast searching
![Page 18: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/18.jpg)
18
STL Containers
Sequence containers vector list deque
Adapters stack queue priority_queue
Associative containers set map multiset multimap
unique keys non unique keys
keys only set multiset
key, value pairs map multimap
![Page 19: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/19.jpg)
19
map containers
allow storing a collection of items each of which has a key which uniquely identifies it student records (social security number) books in a library (ISBN) identifiers appearing in a function (name)
has operations to insert an item (key-value pair) delete the item with a given key retrieve the item with a given key
operations based on value, not position searching for a key is the critical operation
![Page 20: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/20.jpg)
20
some possible data structures
array (or STL vector) unordered ordered by keys
linked list (or STL list) unordered ordered by keys
binary search tree balanced search trees hash table
![Page 21: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/21.jpg)
21
comparative performance
data structure Retrieve Insert Delete
unordered array
ordered array
unordered linked list
ordered linked list
O(n) O(1) O(n)
O(log2n) O(n) O(n)
O(n) O(1) O(n)
O(n) O(n) O(n)
search trees and hash table both have better performance
![Page 22: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/22.jpg)
22
binary search tree
each item is stored in a node of a binary tree
each node is the root of a binary tree with the BST property all items stored in its left subtree have
smaller key values all items stored in its right subtree have
larger key values May be lop-sided. Insertion order
matters.
![Page 23: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/23.jpg)
23
the BSTree<TE, KF> class
BSTreeNode has same structure as binary tree nodes
elements stored in a BSTree are a key-value pair must be a class (or a struct) which has
a data member for the value a data member for the key a method with the signature: KT key( )
const; where KT is the type of the key
![Page 24: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/24.jpg)
24
an example
struct treeItem{ int id; // key string data; // value int key( ) const { return id; }};BSTree<treeItem, int> myBSTree;
![Page 25: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/25.jpg)
25
a binary search tree
36
20 42
12 24 39 45
21 40
root
This is NOT a Heap!!
No node
![Page 26: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/26.jpg)
26
traversing a binary search tree
can use any of the binary tree traversal orders – preorder, inorder, postorder base case is reaching an empty tree
inorder traversal visits the elements in order of their key values
how would you visit the elements in descending order of key values?
![Page 27: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/27.jpg)
27
Recursive BST Search Algorithm
void search (bstree, searchKey){ if (bstree is empty)
//base case: item not found. Take needed action else if (key in bstree's root == search Key) // base case: item found. Process it.
else if (searchKey < key in bstree's root ) search (leftSubtree, searchKey); else search (rightSubtree, searchKey);}
![Page 28: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/28.jpg)
28
searching for 40
36
20 42
12 24 39 45
21 40
root
![Page 29: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/29.jpg)
29
searching for 30
36
20 42
12 24 39 45
21 40
root
![Page 30: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/30.jpg)
30
Recursive BST Insertion
Similar to BST search:Insert_aux (nodeptr & subtree, &item)
If (subtree.root.empty( ) ) subtree.root=itemElse if (item < subtree.root)
Insert_aux (subtree.left, item) // try insert on left
Else if (item > subtree.root)Insert_aux (subtree.left, item) // try insert on
right Else already in tree
Just keep moving down the tree
![Page 31: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/31.jpg)
31
deletion cases item to be deleted is in a leaf node
pointer to its node (in parent) must be changed to NULL
item to be deleted is in a node with one empty subtree pointer to its node (in parent) must be
changed to the non-empty subtree item to be deleted is in a node with
two non-empty subtrees
![Page 32: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/32.jpg)
32
the easy cases
36
20 42
12 24 39 45
21 40
Just prune it
Just set its parent (42) to skip this node
![Page 33: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/33.jpg)
33
the “hard” case
36
20 42
12 24 39 45
21 40
![Page 34: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/34.jpg)
34
the “hard” case
36
20 42
12 24 39 45
21 40
replace with smallest in right subtree (its inorder successor)
replace with largest in left subtree (its inorder predecessor)
or
![Page 35: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/35.jpg)
35
Using method 1
Replace with in-order predecessor
36
20 42
12 24 39 45
21 40
![Page 36: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/36.jpg)
36
Method 2
Replace with in-order successor
36
20 42
12 24 39 45
21 40
![Page 37: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/37.jpg)
37
big O of BST operations
measured by length of the search path depends on the height of the BST height determined by order of
insertionheight of a BST containing n items
is minimum: floor (log2 n) maximum: n - 1 average: ?
![Page 38: 1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.](https://reader030.fdocuments.in/reader030/viewer/2022032517/56649cb95503460f949812e6/html5/thumbnails/38.jpg)
38
faster searching
"balanced" search trees guarantee O(log2 n) search path by controlling height of the search tree AVL tree 2-3-4 tree red-black tree (used by STL associative
container classes)hash table allows for O(1) search
performance search time does not increase as n
increases