Tree Introduction: Understanding Trees in Computer Science, Study notes of Data Structures and Algorithms

This worksheet covers the basics of trees as a data structure in computer science. Topics include identifying root, children, and leaf nodes, understanding tree height and depth, and discussing binary trees, complete binary trees, and traversals. Examples and exercises are provided.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-jtp
koofers-user-jtp 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
worksheet 17: Tree Introduction Name:
An Active Learning Approach to Data Structures using C 1
Worksheet 17: Tree Introduction
Trees are the third most used data structure in computer science, after arrays
(including vectors and other array variations) and linked lists. They are
ubiquitous, found everywhere in computer algorithms.
A tree consists of a collection of nodes connected by directed arcs. A tree has a
single root node. A node that points to other nodes is termed the parent of those
nodes while the nodes pointed to are the children. Every node except the root
has exactly one parent. Nodes with no children are termed leaf nodes, while
nodes with children are termed interior nodes. Identify the root, children of the
root, and leaf nodes in the following tree.
There is a single unique path
from the root to any node;
that is, arcs don’t join
together. A path’s length is
equal to the number of arcs
traversed. A node’s height is
equal to the maximum path
length from that node to a left
node. A leaf node has height
0. The height of a tree is the
height of the root. A nodes
depth is equal to the path
length from root to that node.
The root has depth 0. Identify
the height of the root in the
tree shown at right, and the
node with largest depth.
Explain why the following are not legal trees.
pf3
pf4
pf5

Partial preview of the text

Download Tree Introduction: Understanding Trees in Computer Science and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Worksheet 17: Tree Introduction

Trees are the third most used data structure in computer science, after arrays (including vectors and other array variations) and linked lists. They are ubiquitous, found everywhere in computer algorithms.

A tree consists of a collection of nodes connected by directed arcs. A tree has a single root node. A node that points to other nodes is termed the parent of those nodes while the nodes pointed to are the children. Every node except the root has exactly one parent. Nodes with no children are termed leaf nodes , while nodes with children are termed interior nodes. Identify the root, children of the root, and leaf nodes in the following tree.

There is a single unique path from the root to any node; that is, arcs don’t join together. A path’s length is equal to the number of arcs traversed. A node’s height is equal to the maximum path length from that node to a left node. A leaf node has height

  1. The height of a tree is the height of the root. A nodes depth is equal to the path length from root to that node. The root has depth 0. Identify the height of the root in the tree shown at right, and the node with largest depth.

Explain why the following are not legal trees.

A binary tree is a special type of tree. Each node has at most two children. Children are either left or right. Is the tree shown on the first page a binary tree? Why not?

A full binary tree is a binary tree in which every leaf is at the same depth. Every internal node has exactly 2 children. Trees with a height of n will have 2 n+1^ – 1 nodes, and will have 2n^ leaves. A complete binary tree is a full tree except for the bottom level, which is filled from left to right. How many nodes can there be in a complete binary tree of height n? If we flip this around, what is the height of a complete binary tree with n nodes?

A binary tree can be stored in a vector. The root is stored at position 0, and the children of the node stored at position i are stored in positions 2i+1 and 2i+2. The parent of the node stored at position i is found at position floor((i-1)/2).

A tree is a common way to represent an arithmetic expression. For example, the following tree represents the expression A + (B + C) * D. Polish notation is a way of representing expressions that avoids the need for parentheses by writing the operator for an expression first. The polish notation form for this expression is + A * + B C D. Reverse polish writes the operator after an expression, such as A B C + D * +. Describe the results of each of the first three traversals on the following tree, and give their relationship to polish notation.

B

A

C

D

On Your Own

  1. Trees are used in a variety of different situations in everyday life. Give an example of each of the following, and describe the characteristics of the tree, such as the meaning (if any) of the root node, whether the tree is a binary tree, and so on.
    • A tree used to represent pairings of teams or competitors in a sporting event, such as a tennis tournament.
    • A tree used to represent the ancestors of a single individual
    • A tree used to represent the descendants of a single individual
    • A tree used to show the steps involved in evaluating an arithmetic expression
  2. What is the smallest number of nodes that can be found in a tree of height n? Does it matter if it is a binary tree or not? What is the largest number of nodes? Now does it matter if it is a binary tree? What about a complete tree? A full tree?
  3. Using mathematical induction, prove that the number of leaves in a full tree of height n is 2n^.
  4. Using mathematical induction, prove that the number of nodes in a full binary tree of height n is 2n+1^ – 1.
  5. Programming languages are normally defined by a formal grammar. A statement in this language is then described using a parse tree, a tree that shows the relationship between the grammar and a statement. An example grammar and parse tree is given in the silly sentence generation program in Appendix D. Give another grammar and an example parse tree. Is a parse tree a binary tree?
  6. A tree can be used as a database for the computer game “guess the animal”. Leaves in the tree represent animals, while interior nodes represent questions. A left child represents the yes answer to a question, and a right child a no answer. Initially the database might contain only the one interior node with the question “does it swim”, and the two animals “fish” and “cat”. The program traverses the tree, asking questions until a leaf node is reached. If the guess is correct, the game is over. If not correct, the program asks the user for their animal, and a question that separates their animal from the most recent guess. With these two values a new interior node is then created. Implement this game based on this idea.

(this page is blank)