trees. introduction to trees trees are very common in computer science they come in different forms...

29
Trees

Upload: dana-mosley

Post on 29-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Trees

Page 2: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Introduction to Trees

Trees are very common in computer science They come in different forms They are used as data representation in many applications They appear frequently in several algorithmic solutions

There are several different types of trees Binary Trees Binary Search Trees AVL Trees 2-3 Trees Red-Black Trees

Page 3: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Trees as tool for abstraction

We often use trees in our everyday lives To keep track of our ancestors: a family tree

Most of the terminology used in trees in computer science here To organize sport events: a tennis draw When describing the organization of a company: a company

chart Organization of files in a computer: a directory structure

In here we want to concentrate on trees as tools for the implementation of computer programs: trees as abstract data structures

Page 4: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Understanding the terminology One thing about studying trees (an graphs for that matter)

is that there are several concepts that must be learned. Here is just a few definitions

A tree is a non-empty collection of vertices (or nodes) and edges (or arcs) which carry some properties

A vertex is an object that has a name or has some information associated with it

An edge is a connection between two vertices which can also have some information associated with it

A path is a sequence of adjacent of vertices connected by edges

Page 5: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

A tree

Page 6: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Vertices

Page 7: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Edges

Page 8: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

A path

Page 9: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Trees, Paths and Forests

The main property of a tree is based on pathsIn a tree there is exactly one path between any two nodes

If there exist more than one path between any pair or nodes or if there is no path between any of them we do not have a tree

A disjoint set of trees is called a forest

Page 10: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Rooted Trees

A rooted tree is a tree where one node is "special" and is called the root of the tree

A tree where there is no root is called a free tree Rooted trees are the most common in computer applications,

so common that we'll use the term tree as a synonym for rooted tree

A rooted tree a free tree

Page 11: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

A subtreeA subtree

root of subtreeroot of subtree

Trees and subtrees

In a rooted tree, any node is a root of a subtree consisting of itself and the nodes "below" it

By definition there is only one path between the root and each of the other nodes. Because a root is also a node the main property applies

The convention used in computer science is that roots are drawn on the top. It may seem strange at the beginning but you'll get used to it

RootRoot

Page 12: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

More conventions

Due to the fact that we organize trees with root at the top (as shown in previous slide) we will often hear Node A is below node B Node C is above node A etc.

In fact, the most common way to refer to nodes based on another node is use the idea of a family tree Parent node Children Siblings etc

Page 13: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Another Important Characteristic Another characteristic of a tree which is that every node

(except the root) has only one node immediately above it (one parent) but can have several immediately below (children)

nodenode

parentparent

childrenchildren

Page 14: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Leaves

Nodes that have no children are called leaves (or terminal) nodes

Node with at least one child are called non-terminal

LeavesLeaves

Page 15: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Tress with specific number of children

It is possible that the order in which children are defined is important. We call these trees ordered trees

Sometimes a node must have a maximum number of children. If a for the whole tree the maximum number of children nodes can

have is M and this tree is ordered we have a M-ary tree. A binary tree is a special case of a M-ary tree where all

nodes (except the leaves) have at most 2 children. Because the tree is ordered the children have a order and they are

normally referred to as left and right child

Page 16: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

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

Page 17: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Hierarchical Data And Trees

The element at the top of the hierarchy is the root Elements next in the hierarchy are the children of

the root Elements next in the hierarchy are the

grandchildren of the root, and so on Elements at the lowest level of the hierarchy are the

leaves

Page 18: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

Tree applications Expression evaluations (note how different

traversals result in different notation) Parsing (as part of the compilation process) Storing and retrieving information by a key Representing structured objects (e.g. the universe in

an adventure game) Useful when needing to make a decision on how to

proceed (tic-tac-toe, chess)

Page 19: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

19

Jake’s Pizza Shop

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

Page 20: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

20

A Tree Has a Root Node

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

ROOT NODE

Page 21: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

21

Leaf nodes have no children

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

LEAF NODES

Page 22: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

22

Sibling nodes have same parent

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

SIBLINGS

Page 23: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

23

Tree Terminology

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

SIBLINGS

Page 24: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

24

Tree Terminology

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

SIBLINGS

Page 25: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

25

A Tree Has Levels

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

LEVEL #1

Page 26: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

26

Level Two

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook

Cook Joe Tim Max Ted Len Dale

LEVEL #2

Page 27: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

27

Level Three

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

LEVEL #3

Page 28: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

28

A Subtree

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

A SUBTREE OF ROOT NODE

Page 29: Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications

29

Another Subtree

Owner

Manager Chef

Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale

ANOTHER SUBTREE OF ROOT NODE