



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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
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.
(this page is blank)