Download presentation
Presentation is loading. Please wait.
Published byDorthy McCoy Modified over 8 years ago
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
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)
3
Terminology Root Parent Child Descendant Ancestor Leaf Interior Node
Subtree Path Level Depth Height Terminology
4
Level, Depth, and Path Length
8
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
9
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.
10
Complete Trees d as fn. of N? 2d <= N < 2(d + 1)
d = floor(lg(N))
11
Complete Trees (Cont’d)
12
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
13
Full Trees
14
Sample Trees Degenerate
15
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?
16
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); }
17
Calculate Depth int depth (Node* t) { if (t == NULL) return -1; return max (depth (t->left), depth (t->right)) + 1; }
18
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); }
19
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?
20
Binary Search Trees
22
Using Binary Search Trees Removing Duplicates
Insert into set, then copy back to vector
23
BST Find i = t.find (37); Current Node Action
Root = Compare item = 37 and 50 Go left Node = Compare item = 37 and 30 Go right Node = Compare item = 37 and 35 Node = Compare item = 37 and 37. Found. BST Find
24
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); // … };
25
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?
26
Insert Operation t.insert (32);
27
Insert (Cont’d)
28
Insert (Cont’d) Node* newNode = new Node (item, NULL, NULL, parent);
parent->left = newNode; **Always insert as leaf**
29
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);
30
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
31
Erase (Leaf)
32
Erase (Only Left)
33
Erase (Only Right)
34
Erase (Both) Erase node with two children
35
Erase (Both)
36
Erase (Both)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.