Download presentation
Presentation is loading. Please wait.
Published byDana Mosley Modified over 9 years ago
1
Trees
2
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
3
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
4
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
5
A tree
6
Vertices
7
Edges
8
A path
9
Trees, Paths and Forests The main property of a tree is based on paths In 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
10
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 treea free tree
11
A subtree root 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 Root
12
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
13
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) node parent children
14
Leaves Nodes that have no children are called leaves (or terminal) nodes Node with at least one child are called non-terminal Leaves
15
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
16
Linear Lists And Trees Linear lists are useful for serially ordered data. (e 0, e 1, e 2, …, e n-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
17
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
18
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)
19
19 Jake’s Pizza Shop Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale
20
20 A Tree Has a Root Node Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale ROOT NODE
21
21 Leaf nodes have no children Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale LEAF NODES
22
22 Sibling nodes have same parent Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale SIBLINGS
23
23 Tree Terminology Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale SIBLINGS
24
24 Tree Terminology Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale SIBLINGS
25
25 A Tree Has Levels Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale LEVEL #1
26
26 Level Two Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale LEVEL #2
27
27 Level Three Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale LEVEL #3
28
28 A Subtree Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale A SUBTREE OF ROOT NODE
29
29 Another Subtree Owner Manager Chef Delivery Delivery Waiter Waiter Cook Cook Joe Tim Max Ted Len Dale ANOTHER SUBTREE OF ROOT NODE
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.