Lecture 31: TreesIn this lecture, we will study trees:
TreesYou have most probably studied rooted trees and binary search trees as a data-structure for organizing lists of numbers and efficiently performing insertions and deletions. However a tree is a useful structure in many other parts of CS:
Tree (definition)Let us start with the simplest kind of trees: unrooted and undirected trees. Definition
A (unrooted) tree
A rooted tree ExamplesHere are some examples of unrooted trees: ![]() ![]() ![]() Non-ExamplesThe following graph is not a tree. It has a cycle: ![]() Here is another example that has more than one maximal connected components and is not a tree: ![]() Properties of TreesLeaves of a TreeA leaf of an unrooted tree is a node that has a degree ![]() Leaves are ![]() Leaves are ![]() Leaves are Claim Every tree has a leaf. Proof
If the tree just has one node, then it is trivially a leaf. If the tree has two nodes connected by an edge, both nodes are leaves. Let us focus on trees with three or more nodes. Let us assume that a tree Start from any node
Note that the walk above can be continued for arbitrarily many steps. However, since number of vertices is finite, the walk repeats a vertex. Let for some Thus, we have concluded that every tree has a leaf. Number of EdgesClaim A tree with Proof
Proof is by weak induction on the number of nodes Base Case: Take any tree with Inductive Hypothesis: Let us assume that all trees with Take some tree Number of LeavesClaim Any tree has at least two leaves. We will prove this in class by the following argument. We already know that any tree has at least one leaf. Proof
Let us assume that there is a tree with
Paths in TreesClaim Let Argument in class Since the tree is connected component itself, there has to be at least one path from We will argue that having two distinct paths will necessarily imply that the tree has a cycle which will lead to a contradiction. Counting Labeled Trees and Pruefer SequencesWe will now present a very elegant method of representing labeled trees and count them using Pruefer sequences. Labelled vs. Unlabelled TreesWe distinguish between instances of trees where the names of the vertices matter and instances where all that matters are the connection between vertices. The former class of trees are called labelled trees and the latter are called unlabelled trees. Take the following three trees: ![]() ![]() ![]() They have the same number of nodes and are ‘‘isomorphic+ to each other. The tree on the left is obtained by substituting
We can view a tree in two ways:
All the examples above represent the following unlabelled tree: ![]() Pruffer SequencesGiven a labelled tree Basic idea behind a Pruefer Sequence is to keep removing the leaves one at a time and write down a number corresponding to each leaf that we remove:
ExampleLet us write down the Pruffer sequence for this tree: ![]() To start with, the smallest numbered leaf is ![]() The smallest number leaf is now ![]() The smallest numbered leaf is a To complete out the process, we get the Pruefer sequence: 2,1,2 Example-2Let us try other trees and write down their Pruefer sequences. ![]() The Pruefer sequence is Example-3![]() The Pruefer sequence is Reconstructing Trees From Pruefer CodeLet us now figure out what a valid Pruefer Sequence is and how to reconstruct a tree uniquely from its Pruefer Sequence. Pruefer SequencesFor a tree with
Reconstruction Algorithm: ExampleLet us try and reconstruct a tree from its given Pruefer sequence: Q1 How many nodes does the tree have? 9. The algorithm looks at the smallest number that we have not yet seen so far in the Pruefer sequence.
The remaining sequence is
The remaining sequence is
The remaining sequence is
The remaining sequence is Here is the complete run of the algorithm
The last step is special since we have two nodes unseen, the Pruefer sequence is exhausted and we conclude that they must form an edge. Clayley's FormulaThere are exactly |