CS 112
Lecture 19
Trees
- Trees. Examples: genealogical trees; the Unix file system; a
few Unix features: root directory, navigation using .. and
~account, symbolic links (shortcuts).
- Mathematical versus Computer Science point of view on
trees. Trees as connected graphs without cycles. Rooted trees.
- Binary trees.
- Implementation of binary trees. The binary tree node: data,
left and right. Binary tree abstract data
type.
- Applications: Binary arithmetic expression trees and Binary
search trees.
- Binary arithmetic expresion trees. From a fully parenthesized expression
to a binary expression tree to an arithmetic expression in
postfix form.
- Definition of binary search trees.
- Application of binary tree traversal for search trees: Sorting using
binary search trees.
- Tree traversal algorithms, sketch. Left node Right, Left
Right node traversals.
- Start designing the BTree class. Discussion, in class: how to
implement the node; what is in the private and in the public part
of the class. See the next
homework 9.
- Private: a pointer to the root ("root") and a pointer to the
current element ("current").
- What member functions are needed; types of
functions in the BTree class: constructor (destructor, next
time); navigation (GoToRoot, MoveLeft, MoveRight); insertion
(InsertRoot, InsertLeft, InsertRight); deletion (DeleteRoot,
DeleteLeft, DeleteRight); Miscellenia: IsEmpty, IsEmptyLeft,
IsEmptyRight; Printing: Print and PrintCrt.
- To implement Printing you need to traverse the tree, which we
will do next time, using recursion. Right now, I will give you
the code for the Print functions.
- In class, it was observed that to correctly implement the
delete functions, we need to deallocate a whole subtree, and this
needs traversal. Leave this as comments in your code, and we will
discuss it in class next time.
- It was also observed that insert may overwrite a subtree,
which will be then left unused: then it should also be
deallocated. To be discussed next time.
Ileana Streinu