360 likes | 397 Views
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. Trees. k-ary trees
E N D
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
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)
Root Parent Child Descendant Ancestor Leaf Interior Node Subtree Path Level Depth Height Terminology
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
+ * / – e a b c d a * b + (c – d) / e a b * c d – e / + Expression Tree
Complete Trees d as fn. of N? 2d <= N < 2(d + 1) d = floor(lg(N))
A B C D E G F H K I Non- Complete Tree (Depth 3) Nodes at level 3 do not occupy leftmost positions Complete Trees (Cont’d)
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); }
BST’s • 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?
Using Binary Search Trees Removing Duplicates Insert into set, then copy back to vector
i = t.find (37); Current NodeAction 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 Go right 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) 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); else 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 (Both) Erase node with two children