T2620 ntroduction to ata Structures Lecture 4a inary Trees Review of Linked Lists Linked-Lists dynamic length arbitrary memory locations access by following links an only traverse link in forward direction ow to go both ways? Two links/pointers oubly Linked Lists public class oublelink { public int key; public oublelink left; public oublelink right; } left key right oubly Linked Lists list 5 10 15 ach link has two pointers an traverse linked list in forward and backward directions 1
oubly Linked Lists oubly Linked Lists V list 5 10 15 list 5 10 15 prev toelete toelete Previous delete needed to pointers toelete.left.right = toelete.right; toelete.right.left = toelete.left; oubly Linked Lists V list 5 10 15 inary Tree efinitions binary tree is a structure that is either empty or which consists of one node connected to two disjoint (binary) subtrees disjoint no common nodes toelete 2
inary Tree efinitions inary Tree efinitions ach node of a binary tree has a value, a pointer to a left child node, and a pointer to a right child node (pointers may be NULL) node is the parent of its child nodes inary Tree efinitions V inary Tree efinitions V Node is the root Nodes,,, are leaf nodes They have no children 3
inary Tree efinitions V inary Tree efinitions V Nodes,,,, are internal nodes They have children Nodes,,, form the left subtree of node inary Tree efinitions V inary Tree efinitions X,,, is the path (of length 3) from node to node Paths connect ancestors to descendants 4
inary Tree efinitions X inary Tree efinitions X Nodes, are siblings Nodes, are have depth 1, they are at level 1 in the tree inary Tree efinitions X inary Tree efinitions X This tree has a height of 4 Node has depth 0 minimum-level binary tree has all levels full except (maybe) the last level,,,,,..., 5
inary Tree efinitions XV inary Tree efinitions XV full binary tree has all levels full including the last level,, complete binary tree is a mimumlevel binary tree with nodes filled in from the left on the last level,,,,..., Properties Level i of a full binary tree has 2 i nodes full tree of height h has 2 h-1 leaf nodes 2 h-1 1 internal nodes 2 h 1 total nodes ~50% of nodes are leaves in a full tree the height of a full tree is O(logn) mplementation public class inarynode { public inarynode left; public char key; public inarynode right; } 6
mplementation mplementation inary Search Trees ST property: or each node (with a key value of K) in the binary tree, ll nodes in the left sub-tree will have key values less than K, ll nodes in the right sub-tree will have key values greater than K inary Search Trees 6 3 8 1 5 7 9 2 4 7
Searching STs f node has same key value Return it f node has larger key value Search the left sub-tree f node has smaller key value Search the right sub-tree Searching STs f ST is balanced, we get binary search full binary search tree has ideal balancing 50% (remaining) values on each side of each node ode for Searching a ST public static inarynode find (inarynode root, int searchkey) { inarynode current = root; } while (current!= null && current.key!= searchkey) { if (current.key > searchkey) current = current.left; else current = current.right; } return current; nalysis for Searching a ST What is the complexity of the find() method? What is the first question we ask? s there a best, worst, and average case? 8
nalysis for Searching a ST nalysis for Searching a ST est case Root node O(1) Worst case O(n) Vine, end node nalysis for Searching a ST V verage case epends on the shape of the tree! On average (random/reasonably balanced trees) O(logn) nsertions o find on ST When null, insert new node Newly inserted nodes are always leaf nodes No changes made to existing tree structure 9
nsertions nsertions 5 5 3 8 3 8 1 6 9 1 6 9 2 nsert 2 Put it where you would look for it! nsertions V 5 3 8 1 4 6 9 enefits of STs alanced STs have O(logn) worst and average case find like binary search on an array STs have O(1) update like linked lists 0 7 10
Readings and ssignments Suggested Readings from Shaffer (third edition) 4.1.5, 5.1, 5.3.1, 5.4 11