Lecture 9 Binary Trees Trees General Definition Terminology Complete and Full Binary Tree Definition Sample Trees Binary Tree Nodes Binary Search Trees Using Binary Search Trees - Removing Duplicates Find, Insert, Erase
Used to implement STL set, multiset, map, multimap (OACs) Trees k-ary trees Type of graph: undirected, acyclic, connected |E| = |V| - 1 Each node has max of k children Most popular: k = 2 Used to implement STL set, multiset, map, multimap (OACs)
Terminology Root Parent Child Descendant Ancestor Leaf Interior Node Subtree Path Level Depth Height Terminology
Level, Depth, and Path Length
Binary Tree Definition A binary tree T is a finite set of nodes such that (a) T is empty; (b) T consists of a root, R, and exactly two distinct binary trees left subtree TL right subtree TR
Expression Tree a * b + (c – d) / e a b * c d – e / + – + * / e a b c Use a stack to build a binary tree out of posfix notation.
Complete Trees d as fn. of N? 2d <= N < 2(d + 1) d = floor(lg(N))
Complete Trees (Cont’d)
Complete Trees (Cont’d) A E D C B G F K I H Non- Complete Tree (Depth 3) Nodes at level 3 do not occupy leftmost positions
Full Trees
Sample Trees Degenerate
Node Composition struct Node { Node (const T& v = T (), Node* l = NULL, Node* r = NULL) : data (v), left (l), right (r) { } T data; Node* left; Node* right; // maybe *parent also }; // familiar?
Recursive Tree Visits Systematically explore every node 3 methods In-order – left, node, right Pre-order – node, left, right Post-order – left, right, node void inOrder (Node* t) { if (t != NULL) { inOrder (t->left); process (t->data); inOrder (t->right); }
Calculate Depth int depth (Node* t) { if (t == NULL) return -1; return max (depth (t->left), depth (t->right)) + 1; }
Count Leaves int countLeaves (Node* t) { if (t == NULL) return 0; if (t->left == NULL && t->right == NULL) return 1; return countLeaves (t->left) + countLeaves (t->right); }
Binary Search Trees (BSTs) For each node N Keys in N’s left subtree are < key (N) Keys in N’s right subtree are > key (N) Duplicates?
Binary Search Trees
Using Binary Search Trees Removing Duplicates Insert into set, then copy back to vector
BST Find i = t.find (37); Current Node Action Root = 50 Compare item = 37 and 50 Go left Node = 30 Compare item = 37 and 30 Go right Node = 35 Compare item = 37 and 35 Node = 37 Compare item = 37 and 37. Found. BST Find
Set Class Implementation template<typename T> class Set { // Insert Node struct decl Node* h; size_t sz; public: Set () : h (new Node ()), sz (0) { h->left = h->right = h; } iterator find (const T& v); pair <iterator, bool> insert (const T& v); size_t erase (const T& v); // … };
Find iterator find (const T& seek) { Node* pn = h->right; h->data = seek; T data; while ((data = pn->data) != seek) pn = (seek < data) ? pn->left : pn->right; return iterator (pn); } // Recursive impl?
Insert Operation t.insert (32);
Insert (Cont’d)
Insert (Cont’d) Node* newNode = new Node (item, NULL, NULL, parent); parent->left = newNode; **Always insert as leaf**
Insert (Cont’d) pair <iterator, bool> insert (const T& add) { Node *p = h, *n = h->right; while (n != h) { p = n; if (add < n->data) n = n->left; else if (add > n->data) n = n->right; else return make_pair (iterator (n), false); } if (add < p->data) p = p->left = new Node (add, h, h); p = p->right = new Node (add, h, h); return make_pair (iterator (p), true);
Erase 4 cases Leaf node (no replacement needed) Has only left child Replace with in-order predecessor (IOP) Has only right child Replace with in-order successor (IOS) Has both children Find IOS Splice out IOS Splice in IOS in place of node to erase
Erase (Leaf)
Erase (Only Left)
Erase (Only Right)
Erase (Both) Erase node with two children
Erase (Both)
Erase (Both)