1 / 36

Trees General Definition Terminology Complete and Full Binary Tree Definition Sample Trees

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

harrisjohn
Download Presentation

Trees General Definition Terminology Complete and Full Binary Tree Definition Sample Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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)

  3. Root Parent Child Descendant Ancestor Leaf Interior Node Subtree Path Level Depth Height Terminology

  4. Level, Depth, and Path Length

  5. 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

  6. + * / – e a b c d a * b + (c – d) / e a b * c d – e / + Expression Tree

  7. Complete Trees d as fn. of N? 2d <= N < 2(d + 1)  d = floor(lg(N))

  8. Complete Trees (Cont’d)

  9. 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)

  10. Full Trees

  11. Sample Trees Degenerate

  12. 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?

  13. 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); } }

  14. Calculate Depth int depth (Node* t) { if (t == NULL) return -1; return max (depth (t->left), depth (t->right)) + 1; }

  15. 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); }

  16. 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?

  17. Binary Search Trees

  18. Using Binary Search Trees Removing Duplicates Insert into set, then copy back to vector

  19. 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

  20. 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); // … };

  21. 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?

  22. Insert Operation t.insert (32);

  23. Insert (Cont’d)

  24. Insert (Cont’d) Node* newNode = new Node (item, NULL, NULL, parent); parent->left = newNode; **Always insert as leaf**

  25. 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); }

  26. 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

  27. Erase (Leaf)

  28. Erase (Only Left)

  29. Erase (Only Right)

  30. Erase (Both) Erase node with two children

  31. Erase (Both)

  32. Erase (Both)

More Related