## Chapter 6. Trees I

### 6.1 The Tree Abstract Data Type

A tree is an ADT that stores elements hierarchically. With the exception of the top (root) element, each element in a tree has a parent element and zero or more children.
• In computer science, a tree is an abstract model of a hierarchical structure
• A tree consists of nodes with a parent-child relation
• Applications:
• Organization charts
• File systems
• Programming environments
• [Picture]
6.1.1 Terminology and Basic Properties
• A tree T is a set of nodes storing elements in a parent-child relationship with the following properties:
• T has a special node r, called the root of T, with no parent node.
• Each node v of T different from r has a unique parent node u.
• If node u is the parent of node v, we say that v is a child of u.
• Two nodes that are children of the same parent are siblings.
• A node is external if has no children, and it is internal if it has one or more children. External nodes are also known as leaves.
• The subtree of T rooted at a node v is the tree consisting of all the descendents of v in T (including v itself).
• An ancestor of a node is either the parent node itself or an ancestor of the parent of the node.
• A node v is a descendent of a node u if u is an ancestor of v.
• Example:
 Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node (K): parent (F), grandparent (B), grand-grandparent (A), etc. Depth of a node (F): number of ancestors (2) Height of a tree: maximum depth (3) of any node (J) Descendant of a node (A): child (B), grandchild (F), grand-grandchild (I), etc. Subtree, rooted at a node (C) consists of the node (C) and all its descendents (G, H)

Ordered Trees

• A tree is ordered if there is a linear ordering defined for children of each node (siblings ordering).
• A binary tree is an ordered tree in which every node has at most two children.
• A binary tree is proper if each node has either zero or two children.
• For each internal node in a binary tree, we label each child as either being a left child or a right child.
• The subtree rooted at a left or right child of an internal node v is called a left subtree or right subtree of v, respectively.
• We use positions to abstract nodes:
• Object element(Position)
• Generic methods:
• integer size()
• boolean isEmpty()
• ObjectIterator elements()
• PositionIterator positions()
• Accessor methods:
• Position root()
• Position parent(Position)
• PositionIterator children(Position)
• Query methods:
• boolean isInternal(Position)
• boolean isExternal(Position)
• boolean isRoot(Position)
• Update methods:
• void swapElements(Position, Position)
• void replaceElement(Position, Object)
• Additional update methods may be defined by data structures implementing the Tree ADT
6.1.3 A Tree Interface
html-6.1a (InspectablePositionalContainer)
html-6.1b (PositionalContainer)
html-6.1c (InspectableTree)
html-6.1d (Tree)

### 6.2 Basic Algorithms on Trees

6.2.1 Running-Time Assumptions

 Input Output root() None Position O(1) parent(v) Position Position O(1) isInternal(v) Position bool O(1) isExternal(v) Position bool O(1) isRoot(v) Position bool O(1) children(v) Position Iterator of positions O(cv) swapElements(v,w) Two positions Nine O(1) replaceElement(v,e) A position and an object None O(1) elements() None Iterator of objects O(n) positions() None Iterator of positions O(n)

6.2.2 Depth and Height

The depth of
a node v is the number of ancestors of v, excluding v itself.
html-6.3 (depth)

The height of a node v in a tree T is also defined recursively.
• If v is an external node, then the height of v is 0.
• Otherwise, the height of v is one plus the maximum height of a child of v.
The height of a tree T is the height of the root of T.
html-6.5 (height1)
html-6.6 (height2)

6.2.3 Preorder Traversal
• 1. node 2. descendents
• [Picture]
 Algorithm preOrder(v)       visit(v)      for each child w of v           preOrder(w)

Tree Printing: An Application of Preorder Traversal
html-6.9 (preorderPrint)

Using Preorder Traversal for Representing a Tree
html-6.10 (parentPrint)

6.2.4 Postorder Traversal
• 1. descendents 2. node
• [Picture]
 Algorithm postOrder(v)      for each child w of v           postOrder(w)      visit(v)

An Illustrative Use of the Postorder Traversal
html-6.12 (postorderPrint)

A Postorder Algorithm for Computing Disk Usage
html-6.13 (diskSpace)

Other Kinds of Traversals
• For example, we could traverse a tree so that we visit all the nodes at depth d before we visit the nodes at depth d+1.
• [Picture]