Top Banner
Introduction to Trees
17

Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Dec 31, 2015

Download

Documents

Mervin Greene
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Introduction to Trees

Page 2: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

When at a node in a singly linked list, there is no choice as to which node can be visited next:

Motivation

when here we canonly go here

In a doubly linked list we have a choice of 2 nodes:

if herethen hereor here

If decisions or multiple possibilities are inherent in an application, then more than one or two nodes should be able to be reached from any given node. Data structures allowing this include trees and graphs:

Trees can be regarded as falling between linear lists and graphs in the hierarchy of structural complexity.

Tree Graph

Trees

Page 3: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

Tree Definitions

There are many ways of defining a tree. We will look at 4 definitions, and as we do, we will define many other aspects of trees, which MUST be understood and remembered for successful mastery:

1) Definition in terms of Graphs: A tree is a connected, undirected graph that contains no circuit.

In other words, there is a unique path between any 2 nodes.

Not a tree, because there is a circuit:

We use the adjective connected since one graph can be considered as consisting of two disconnected parts:

considered as one graph

The adjective undirected allows the definition to be more general:

(restricted, since you can’t go against an arrow)

(general, since you can go both ways between nodes)

directed graph or digraph:

undirected graph:

Page 4: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

Because computer applications of trees require that we start at one designated node in the tree in order to reach the other nodes, we drop the undirected requirement from the definition:

Using directed edges allows us to designate the node where we should start as the root node.

root

Definition: The root node has indegree of 0.

Indegree ≡ The number of directed edges pointing at a node.

Outdegree ≡ The number of directed edges pointing away from a node.

For Example:

This root has:indegree = 0 andoutdegree = 4

This internal node has:indegree = 1 andoutdegree = 3

Leaf nodes have outdegree of 0.

Page 5: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

To impose some standardization so that we can visually pick out the root more easily, computer scientists require that trees be rooted:

When we root a directed tree, it is no longer necessary to draw the arrows on the directed edges. They are understood to be there:

bottom rooted top rooted siderooted

(European) (American) (compromise)

(From now on, we will usually draw trees as top-rooted, with all edges understood to be directed downwards.)

Page 6: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

2) Definition in terms of Number of Edges and Nodes:

A tree is a finite connected graph such that:

For Example:

3 nodes2 edges

5 nodes4 edges

8 nodes7 edges

no. of Nodes = no. of Edges + 1

3) Definition in terms of Connectivity:

A graph is a tree iff the removal of any edge would disconnect the graph:

Removing this, or any other edge, disconnects the graph.

Definition: A collection of disconnected trees is called a forest:A forest of 5 trees.

For Example:

For Example:

Page 7: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

4) Recursive Definition (Defining trees in terms of trees.)

(This is the definition that computer scientists prefer.)

A tree is a finite set of nodes which is either empty (root=NULL) or there is

one node called the root node and the remaining nodes can be partitioned

into disjoint sets each of which is also a tree.

The Recursion

This recursive definition for trees gives us some easily defined search algorithms, which we will study later.

These 3 disjoint partitions are also trees – and with respect to the larger structure, they are called subtrees.

1 2

3

Page 8: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

TreesDefining Parts of a Tree:

Many scientific and medical terms seem obscure to us because these disciplines originated millennia ago within ancient languages. Many medical terms derive from ancient Greek.

Even though the terminology came from common, everyday words, it is now foreign to us because of the language difference.

More recent scientific disciplines kept Latin as their common language of discourse.

Fortunately for us, computer science did not make the mistake of obscuring its definitions in ancient Latin or Greek.

It uses current, common everyday English words like mouse, bugs, chips, etc. (More obscure terms mainly come from abbreviations and acronyms.)

However, there is a drawback. For some concepts within our discipline, there may be more than one common word, and the one which will predominate from long historical use remains to be seen.

stetho·scope

Greek: lungs to watch

ophthalmo·scope

eye

osteo·myel·itis

bone marrow inflamation

Botanical Generic

Familial Relations

brothers or siblings

ancestor – parent – father →descendent – child – son →

Generic

predecessor

successor

← root ------------------ start node← branch node ---- internal node← branch ------------- edge or arc

← leaf node ---- external node↑vertex

is another name for node

Page 9: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

Kinds of Trees – in terms of branching factor (outdegree).

Unary Trees – outdegree for every node ≤ 1. (a singly linked list)

Binary Trees – outdegree for every node ≤ 2.

Ternary Trees – outdegree for every node ≤ 3.

Note: The indegree for every node in a directed tree, except the root, is 1.

.

.

.

m - ary Trees – outdegree for every node ≤ m.

m

Page 10: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

TreesMathematical Properties of Trees

Level Binary Tree Ternary Tree m-ary Tree

0

1

2

3

.

.

....

.

.

....

.

.

.

1 1 1

2 3 m

4 9 m2

8 27 m3

In general: h 2h 3h

......

......

......

...

mh

Total max no. of nodes:

Max no. of Nodes per Level:

......

These formulas may be proved by induction:

h

k

k

0

2

h

k

k

0

3

h

k

km

0

12 1 h2

13 1

h

1

11

m

mh

Page 11: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

Definition

The path length of a node in a tree is the no. of edges from the root to that node.

height(or depth)

= 4

The height or depth of a tree is the max over all the path lengths of its nodes.

path length = 2

path length = 3

path length = 4

path length = 1

path length = 0

Page 12: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

TreesDefinition

An m-ary tree is regular if every one of its internal nodes has exactly m sons.

TernaryBinary

For each of the following kinds of trees, state which are regular and which are not regular:

regular

regular

regular

regularnot regular

not regular

Page 13: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

TreesDefinition

Question: For a regular binary tree of height h, what is the maximum no. of leaves it can have?

Answer: 2hQuestion: What is the max no. of leaves for a regular m-ary tree of height h ?

Answer: mh

Such trees are called full trees. For example:

Full Binary Trees

non-Full Binary Trees

Page 14: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

TreesDefinition

The min no. of leaves that a regular m-ary tree of height h can have is:

1 + h(m-1)

An example, for binary trees:

m = 2

h = 4

So for a binary tree of height 4, the min no. of leaves for such a tree is:

1 + 4(2-1) = 1 + 4 = 5

An example, for ternary trees:

m = 3

h = 5

So for a ternary tree of height 5, the min no. of leaves for such a tree is:

1 + 5(3-1) = 1 + 10 = 11

Page 15: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees

Definition

Complete Binary Tree – Such a tree of height h is a full tree up to level h-1 and is filled in from left-to-right at level h.

For example: Complete Binary Trees

Not Complete

...

Page 16: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

TreesMore Mathematical Properties

Question: How is the height h related to the total number of nodes, n, in a:

1) full binary tree?

2) full m-ary tree?

Recall that the no. of nodes for a full binary tree of height h is:

2h+1−1

So, to get h in terms of n just solve the following equation for h:

2h+1−1 = n

2h+1 = n + 1

h + 1 = log2 (n + 1)

h = log2 (n + 1) − 1

Similarly, since mh+1−1 m −1

= n for full m-ary trees, the solution is easily determined to be:

h = logm [n(m−1) + 1] − 1

Questions: Will these log functions always return integer values? Why, or why not?

Page 17: Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Trees Mathematical Properties

Question: How do we determine h in terms of n for complete binary trees?

Hint: Derive the formula by looking at the trees below, and filling in and observing the number pattern in the table:

For complete binary trees: h = ⎿log2(n)⏌For complete m-ary trees: h = ⎿logm(n)⏌

The pair of delimiters ⎿ ⏌ is called the floor function, and is defined as:

The ceiling function ⎾ ⏋ is the complement of the floor function, and is defined as:

⎿ x⏌ ≡ the largest integer ≤ x

⎾ x⏋ ≡ the smallest integer ≥ x

height h

nodes n

Note that the height h increases by 1 at every power of 2, and is simply related as the logarithm of that power of 2: h = log2(n)

But the log2 of the intervening integers results in decimal numbers, not integers. How can we denote the truncation of their decimal parts?

– e.g. ⎿2.135⏌ = 2 ⎿2.975⏌ = 2

– e.g. ⎾2.135⏋ = 3 ⎾2.975⏋ = 3

1 nodeh = 0

8 nodesh = 3

3 nodesh = 1

2 nodesh = 1

4 nodesh = 2

5 nodesh =2

7 nodesh = 2

6 nodesh = 2

0 1

1 2

1 3

2 4

2 5

2 6

2 7

3 8⋮ ⋮