CS 225
Data Structures
Fe February 21 – Tr Trees
G G Carl Evans
CS 225 Data Structures Fe February 21 Tr Trees G G Carl Evans - - PowerPoint PPT Presentation
CS 225 Data Structures Fe February 21 Tr Trees G G Carl Evans Binary T Bi Tree De Defin ined ed C A binary tree T is either: S X OR A 2 2 5 Tr Tree Property: height C height(T) : length of the longest path
Data Structures
Fe February 21 – Tr Trees
G G Carl Evans
A binary tree T is either:
X S 2 C 2 5
height(T): length of the longest path from the root to a leaf Given a binary tree T: height(T) =
A X S 2 C 2 5
A tree F is full if and only if: 1. 2.
A X S 2 C 2 5
A perfect tree P is defined in terms of the tree’s height. Let Ph be a perfect tree of height h, and: 1. 2.
A X S 2 C 2 5
Conceptually: A perfect tree for every level except the last, where the last level if “pushed to the left”. Slightly more formal: For all levels k in [0, h-1], k has 2k nodes. For level h, all nodes are “pushed to the left”.
A X S 2 C 2 5 Y Z
A complete tree C of height h, Ch:
TL is __________ and TR is _________ OR TL is __________ and TR is _________
A X S 2 C 2 5 Y Z
Is every full tree complete? If every complete tree full?
A X S 3 C 2 5 Y Z
CS 225 has over 50 hours of open office hours each week, lots of time to get help!
CS 225 has over 50 hours of open office hours each week, lots of time to get help!
CS 225 has over 50 hours of open office hours each week, lots of time to get help!
case, or one exam question.
CS 225 has over 50 hours of open office hours each week, lots of time to get help!
yourself up for failure.
CS 225 has over 50 hours of open office hours each week, lots of time to get help!
insert, inserts an element to the tree. remove, removes an element from the tree. traverse,
#pragma once template <class T> class BinaryTree { public: /* ... */ private: };
BinaryTree.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
C S X A 2 2 5 Y
Ø Ø Ø Ø Ø Ø Ø Ø Ø
A X S 2 C 2 5 Y
C S X A 2 2 5 Y
Ø Ø Ø Ø Ø Ø Ø Ø Ø
Theorem: If there are n data items in our representation of a binary tree, then there are ___________ NULL pointers.
Base Cases: n = 0: n = 1: n = 2:
Induction Hypothesis:
Consider an arbitrary tree T containing n data elements:
*
+ / c d e a
*
+ / c d e
template<class T> void BinaryTree<T>::__Order(TreeNode * root) { if (root != NULL) { ______________________; ___Order(root->left); ______________________; ___Order(root->right); ______________________; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
a
*
+ / c d e
template<class T> void BinaryTree<T>::__Order(TreeNode * root) { if (root != NULL) { ______________________; ___Order(root->left); ______________________; ___Order(root->right); ______________________; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
a
*
+ / c d e
template<class T> void BinaryTree<T>::__Order(TreeNode * root) { if (root != NULL) { ______________________; ___Order(root->left); ______________________; ___Order(root->right); ______________________; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
a