Loading...

PPT – Trees PowerPoint presentation | free to view - id: 20a96-MTE0O

The Adobe Flash plugin is needed to view this content

Topic 12

- Trees

Chapter Objectives

- define trees as data structures
- define the terms associated with trees
- discuss the possible implementations of trees
- analyze tree implementations of collections
- discuss methods for traversing trees
- examine a binary tree example

Trees

- a tree is a nonlinear data structure used to

represent things that are in some hierarchical

relationship, for example - family tree
- class inheritance hierarchy in Java
- computer file system (directories and

subdirectories) - decision trees
- top-down design

Tree Definition

- tree a set of elements of the same type that is

either empty or has a distinguished element

called the root, from which descend zero or more

subtrees - each subtree is itself a tree

Tree Terminology

- nodes the elements in the tree
- a node has a data portion and some reference

portions - nodes are connected by edges
- root the origin of the tree
- leaf node a node without a reference to another

node - interior node a node that is not a leaf node

Tree Terminology

Tree Terminology (contd)

- a tree has no cycles (i.e. a node cant refer

back to a node that is higher in the hierarchy) - the simplest tree is the empty tree
- what does that mean? the root reference is null

Tree Terminology (contd)

- parent or predecessor the node directly above in

the hierarchy - child or successor a node directly below in the

hierarchy - siblings nodes that have the same parent
- example diagram

Discussion

- Does a leaf node have any children?
- Does the root node have a parent?
- How many parents does every node other than the

root node have?

Tree Terminology (contd)

- Continuing the family tree analogy
- ancestors of a node s are the parent of s, the

parent of the parent of s, etc. - descendants of a node t are the children of t,

the children of the children of t, etc.

Level of a Node

- level of a node number of edges between root

and node - it can be defined recursively
- the root node is at level 1
- a node that is not the root node has a level that

is 1 greater than the level of its parent - example

Path length and level

Height of a Tree

- height of a tree is the number of nodes on the

longest path from the root to a leaf - alternate definition
- is a tree is empty, its height is 0
- is a tree is not empty, its height is the maximum

of the levels of its nodes - example

Subtrees

- subtree a set of nodes which is itself a tree
- a subtree consists of a node and all its

descendants - example
- c is the root node of a subtree

Balanced and Complete Trees

- a tree is balanced if all the leaves are on the

same level or at least within one level of each

other - a tree is complete if it is balanced and all the

leaves at the bottom level are on the left side

of the tree

Balanced and unbalanced trees

Some complete trees

Degree or Arity

- the degree or arity of a node is the number of

its children - example
- the degree or arity of a tree is the maximum of

the degrees of its nodes

Binary Trees

- general tree a node may have any number of

children - n-ary tree a node may have no more than n

children - binary tree a node may have no more than 2

children - example

Binary Trees (contd)

- each node of a binary tree will have
- a data portion
- a reference to a left child
- a reference to a right child
- recursive definition of a binary tree
- a binary tree is the empty tree, or a binary tree

whose left and right successors are binary trees - a binary tree is a positional tree, i.e. it

matters whether the subtree is left or right

Tree Traversals

- a traversal of a tree should visit each node of

the tree once - example a typical reason to traverse a tree is

to display the data stored at each node of the

tree - traversal orderings
- preorder
- inorder
- postorder
- level-order

Preorder Traversal

- start at the root
- visit each node, followed by its children
- recursive algorithm
- if tree is not empty,
- visit root node of tree
- perform preorder traversal of its left subtree
- perform preorder traversal of its right subtree

Inorder Traversal

- start at the root
- visit the left child of each node, then the node,

then any remaining nodes - recursive algorithm
- if tree is not empty,
- perform inorder traversal of left subtree of root
- visit root node of tree
- perform preorder traversal of its right subtree

Postorder Traversal

- start at the root
- visit the children of each node, then the node
- recursive algorithm
- if tree is not empty,
- perform postorder traversal of left subtree of

root - visit root node of tree
- perform postorder traversal of right subtree of

root

Traversal Analysis

- each node is processed exactly once
- so, a traversal is O(n)

Iterative Traversals

- (do we want to do this??)
- recursive tree traversals use the Java runtime

stack to keep track of where we are in the tree - in iterative tree traversals, it is up to the

programmer! - etc. etc. etc.

Operations on a Binary Tree

- what might we want to do with a binary tree?
- add an element (but where?)
- remove an element (but from where?)
- is the tree empty?
- size of the tree (how many elements)
- other operations?

Discussion

- difficult to have a general add operation, until

we know the purpose of the tree (we will discuss

binary search trees later) - could add randomly go either right or left,

and add at the first available spot

Discussion (contd)

- similarly, where would a general remove operation

remove from? - could arbitrarily choose to remove, say, the

leftmost leaf - if random choice, what would happen to the

children and descendants of the element that was

removed? what does the parent of the removed

element now point to? - what if the removed element is the root?

One Set of Operations on a Binary Tree

UML description of the BinaryTreeADT interface

Binary Tree Implementation using Links

- to represent the binary tree, we need
- a linked list of nodes
- root to keep track of the node that is the root

of the tree - count to keep track of the number of nodes in

the tree - first, how will we represent a node?

BinaryTreeNode class

- this represents a node in a binary tree and

contains - the data element stored in the node
- pointer to left child of the node
- pointer to right child of the node
- see BinaryTreeNode.java

BinaryTree class

- it implements the Binary Tree ADT interface (see

BinaryTreeADT.java) - constructors
- removeLeftSubtree method
- find method
- iteratorInOrder method
- see others in BinaryTree.java

Using Binary Trees Expression Trees

- expression tree represents an arithmetic

expression - root and internal nodes contain operations
- leaf nodes contain operands
- we can use an expression tree to evaluate an

expression evaluate from the bottom up

An example expression tree

Building Expression Trees

- how do we build an expression tree?
- recall stack algorithm to evaluate postfix

expressions - exercise develop the algorithm

Building an expression tree from a postfix

expression

Building an expression tree from a postfix

expression (contd)

UML description of the Postfix example