Joe Meehean 1 A B D I C E X Terminology • each circle isAa node • pointers are edges • topmost node is the root • bottom B nodesI are leavesC no outgoing edges Every non-empty tree has • one root D E • one or more leaves X Node A is the parent of node B Node B is the child of node A The root has no parent All other nodes have exactly 1 parent A B Not a tree D has 2 parents A B C D Path is a sequence of connected nodes Length of a path is the number of edges in the path • A to D is 2 • C to F is 1 Tree 1 Tree 2 A C B F Tree 3 • X is 0 D X Height of a tree: length of its longest path from root to leaf Ex: Height of 2 A B E C D Depth of a node: length of path from root Example A B • A: 0 • B: 1 C • E: 2 • D: 2 • C: 1 E D Subtrees of a node are the nodes rooted at a nodes children As subtrees A B • rooted at B • rooted at C E C D Special Tree A No node has more than 2 children • can have 0,1, or 2 Each child is either “right” or “left” B E C D A B E X C D Y Z S T template <typename D> class BinaryTreenode<D>{ private: D data_; BinaryTreenode<D>* left_; BinaryTreenode<D>* right_; public: BinaryTreenode(D d = D(), BinaryTreenode<D>* left = NULL, BinaryTreenode<D>* right = NULL); }; Iterate through all nodes • each node visited once, to… • print all values • see if a node has some property • modify the node, etc… 4 common orders for visiting nodes • preorder • postorder • in order (binary trees only) • level order Depth-first traversal Visit the root first Recursive definition 1 • visit the root 2 • do a preorder A B C 5 traversal of each subtree, left to right Ex: A BEDCF E 3 F D 4 6 Depth-first traversal Visit the root last Recursive definition 6 • do a postorder 3 traversal of each subtree, left to right • visit the root Ex: E DBFCA B E 1 A F D 2 C 5 4 Depth-first traversal For binary trees only Visit root in between subtrees Recursive definition 4 2 • in-order traversal of left subtree • visit the root • in-order traversal of right subtree Ex: E BDAFC B E 1 A F D 3 C 6 5 Visit all nodes at level 1 (depth 1) • then level 2, level 3, etc… • always left to right (or some order) Use a queue • instead of recursion (implicitly uses a stack) q.push(root); while( !q.empty() ){ //dequeue node n //visit n //enqueue all of n’s children(L to R) } Special kind of binary tree Each node stores a key • sometimes an associated value For each node n • all keys in n’s left subtree are < key at n • all keys in n’s right subtree are > key at n • if duplicate keys allowed keys that equal n can go left XOR right (not both) 18 Insert a key (and associated data) Lookup a key (and associated data) Remove a key (…) Print all keys in sorted • using an inorder traversal 19 4 2 1 6 5 4 2 6 9 5 4 2 9 7 3 Yes In order traversal produces: 24569 No: 7 is not < 6 20 6 lookup(2) 4 3 9 5 7 15 2 21 6 lookup(2) 4 3 2 9 5 7 15 Eliminates half the nodes at every compare 22 6 lookup(2) 4 3 2 9 5 7 15 Eliminates half the nodes at every compare 23 6 lookup(2) 4 3 2 9 5 7 15 Eliminates half the nodes at every compare 24 Worst-case • O(height of tree) Worst of worst • height is N • lookup is O(N) Best worst-case • height is log2N • lookup is O(logN) O(LogN) is waaaay better than O(N) 25 New values inserted as leaves Must choose position to respect BST ordering • and to ensure we can find it with a lookup Duplicate keys are not allowed 26 Traverse the tree • like a lookup If we find a duplicate • return an error If we end up at a null (child of a leaf) • make a new node with the key • make it the child of the leaf Note the above two were our base cases for lookup too 27 Similar to lookup • worst-case follow path from root to leaf • O(logN) for a balanced tree • O(N) for a completely unbalanced tree 28 Find the node n w/ key to be deleted Different actions depending on n’s # of kids • Case 1: n has 0 kids (it’s a leaf) set parent’s n-pointer (left or right) to null • Case 2: n has 1 kid set parent’s n-pointer to point to n’s only kid • Case 3: n has 2 kids replace n’s key with a key further down in the tree delete that node 29 8 delete(17) 15 … … 20 … 18 … 16 17 30 8 delete(17) 15 … … 20 … 18 … 16 17 31 8 delete(16) 15 … … 20 … 18 … 16 17 32 8 delete(16) 15 … … 20 … 18 … 16 17 33 delete(15) 8 15 … … 20 … 18 … 16 Smallest value in right subtree 17 34 delete(15) 8 16 … … 20 … 18 … 16 Case 2: 1 kid Replace 16 with it’s only child 17 35 8 delete(15) 16 … … 20 … 18 … 16 Case 2: 1 kid Replace 16 with it’s only child 17 36 Worst-case (delete root) Root to leaf H = Height of Tree • O(H) Balanced tree • O(logN) where N is keys in tree O(N) for a completely unbalanced tree 37 DSW algorithm • transform BST into a linked list • transform linked list into balanced BST • uses tree rotations Tree rotation • rotates a part of the tree • makes a child the parent • makes a parent the child • preserves BST property 38 Left-child tree rotation: (4 & 6) 6 4 2 9 5 39 Left-child tree rotation: (4 & 6) 6 4 4 9 2 6 Make 4 parent of 6 2 5 5 9 40 Left-child tree rotation: (4 & 6) 6 4 2 4 9 5 Assign 4’s abandoned child to 6. 2 5 6 9 41 General left-child tree rotation A k2 k1 B X Z Y 42 General left-child tree rotation A k2 k2 k1 k1 B X B Z Y X A Y Z 43 General left-child tree rotation A k2 B k2 k1 k1 B X Z Y A X Y Z 44 General left-child tree rotation A k2 B k2 k1 k1 B X Z Y A X Y Z 45 46