0% found this document useful (0 votes)
58 views

17 Trees Slides

This document discusses trees as non-linear data structures and their terminology. It defines trees recursively as a set of nodes where one node is the root and other nodes are partitioned into subtrees with the subtree roots as children of the root. Key tree terminology introduced includes nodes, edges, roots, parents, children, siblings, ancestors, descendants, paths, depth, height, internal nodes, leaves and implementations using an adjacency list and a "first child, next sibling" approach.

Uploaded by

manish_sat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

17 Trees Slides

This document discusses trees as non-linear data structures and their terminology. It defines trees recursively as a set of nodes where one node is the root and other nodes are partitioned into subtrees with the subtree roots as children of the root. Key tree terminology introduced includes nodes, edges, roots, parents, children, siblings, ancestors, descendants, paths, depth, height, internal nodes, leaves and implementations using an adjacency list and a "first child, next sibling" approach.

Uploaded by

manish_sat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

COMP 250

Lecture 17

(rooted) trees
Oct. 19, 2016

Linear Data Structures


linked list
array

Non-Linear Data Structures


tree

graph

Tree e.g. UNIX file system

e.g. Organization Hierarchy (McGill)

Family Tree (descendents)


person

Child 1

Child 2

Child 3

Here I ignore spouses (partner).

Family Tree (ancestors)


person

dad

mom

moms
mom

moms
dad

dads
mom

dads
dad

This is an example of a binary tree (next week)

Tree Terminology
root

node, vertex
(directed) edge

A directed edge is ordered pair: (from, to)

root

parent

child

Every node except the root is a child, and has exactly one parent.
8

Q: If a tree has N nodes, how


many edges does it have ?

Q: If a tree has N nodes, how


many edges does it have ?
A:

N-1

Since every every edge is of the form


(parent, child), and each node except
the root is a child and each child has
exactly one parent.
10

parent

siblings

Two nodes are siblings if they have the same parent.


11

Recursive definition of rooted tree

12

Recursive definition of rooted tree


A tree T is a finite (possibly empty) set of nodes such that:
if the set is non-empty then one of the nodes is the root r
the non-root nodes are partitioned into subsets
T1, T2, , Tk, each of which is a tree (called a subtree)
the roots of the subtrees are the children of root r
13

internal nodes

(e.g. file directories)

external nodes
(leaf)
(e.g. files)
14

A path in a tree is a
sequence of nodes
( , ,, ) such
that ( ,
) is an
edge.

The length of a
path is the
number of edges
(number of
nodes 1)

15

A path with just


one node ( )
has length = 0.

16

ancestor

descendent

Node v is an ancestor of node w if there is a path from v to w.


Node w is a descendent of node v.

17

The depth or level of a node is the length of


the path from the root to the node.
depth (level)
root

0
1
2
3
4

18

How to compute depth(v) ?

depth (level)
0
1
2
3
4

19

depth( v ){
if ( v.parent == null) //root
return 0
else
return 1 + depth( v.parent )
}

20

The height of a node is the maximum length


of a path from that node to a leaf.

?
2

21

The height of a node is the maximum length


of a path from that node to a leaf.

?
1

22

The height of a node is the maximum length


of a path from that node to a leaf.

2
1

23

How to compute height(v) ?


4
2

24

4
2

height(v){
if (v is a leaf)
return 0
else{
h=0
for each child w of v
h = max(h, height(w))
return 1 + h
}
}
25

How to implement a tree ?


class TreeNode<T>{
T element;

}
26

How to implement a tree ?


class TreeNode<T>{
T element;
ArrayList< TreeNode<T> >
}

TreeNode<T>

children;
parent;

27

Another common implementation:


first child, next sibling

28

More common implementation:


first child, next sibling
class TreeNode<T>{
T element;
TreeNode<T> firstChild;
TreeNode<T> nextSibling;
:
:
}
class Tree<T>{
TreeNode<T> root;
:
:
}

29

More common implementation:


first child, next sibling
class TreeNode<T>{
T element;
TreeNode<T> firstChild;
TreeNode<T> nextSibling;
TreeNode<T> parent
:
}
class Tree<T>{
TreeNode<T> root;
:
:
}

30

A tree of what? Each node also


has an element (not shown)
class TreeNode<T>{

T element;

TreeNode<T> firstChild;
TreeNode<T> nextSibling;
:
:

class Tree<T>{
TreeNode<T> root;
:
:
}

31

You might also like