You are on page 1of 6

Tree

Trees
• We have the following dilemma with respect to representing ordered
lists of items
– The array
• offers efficient search – O(log n)
• but requires shifting elements when inserting and deleting
resulting in O(n)
• limits storage space available without an expensive array
doubling operation, or wastes storage space as we might request
an array larger in size than needed
– The linked list
• offers easy insert/delete – O(1)
• and utilizes the exact amount of storage space needed
• but we must use sequential search to find an item for
search/insert/delete, so ultimately has an O(n)
search/insert/delete
– The tree data structure offers the best of both – efficient
insert/delete, utilizes the exact amount of memory needed, and may
provide log n search
– The drawback?
• tree implementations can be very complicated especially if we
want to guarantee log n search
• here we introduce the tree and specifically a binary tree
Binary Trees
8.1 Trees, Binary Trees, and Binary Search Trees

How to transform
a list into a tree?
Trees

• A tree is a data structure used to represent different kinds


of data and help solve a number of algorithmic problems
Why Trees?
• Trees are very important data structures in computing.
• They are suitable for:
– Hierarchical structure representation, e.g.,
• File directory.
• Organizational structure of an institution.
• Class inheritance tree.
– Problem representation, e.g.,
• Expression tree.
• Decision tree.
– Efficient algorithmic solutions, e.g.,
• Search trees.
• Efficient priority queues via heaps.
Linear Lists And Trees
• Linear lists are useful for serially ordered data.
– (e0, e1, e2, …, en-1)
– Days of week.
– Months in a year.
– Students in this class.
• Trees are useful for hierarchically ordered data.
– Employees of a corporation.
• President, vice presidents, managers, and so on.
– Java’s classes.
• Object is at the top of the hierarchy.
• Subclasses of Object are next, and so on.

You might also like