Trees (Unit Review) PDF
Trees (Unit Review) PDF
Trees (Unit Review) PDF
Trees are data structures used to represent relationships, that are hierarchical in nature, meaning
that a ‘parent’ and ‘child’ relationship exists in the tree
Nodes (or vertices) are shown with paths (or edges) between them:
Each node in a tree has one parent, and one node (only one) has no parents, this node is called the
root of the tree
Nodes that have no children are known as leaf nodes
The paths between nodes are known as branches (or edges)
The terminology for trees can also be extended so that we can say that A is an ancestor of D, E, Sub-tree
and F and D is a descendant of F, and the root (A) is an ancestor of all nodes in the tree
In-Degree: means the number of branches that lead into a node, in this tree, the root has degree 0, all other nodes have degree 1
Out-degree: the number of branches that lead out of a node, leaf nodes have out-degree 0, all other nodes >= 1
A sub-tree is a portion of the larger tree; it is defined as a node together with all of its descendants
BINARY TREE:
Binary trees are defined as a set of nodes T in which: T is empty, or T is partitioned into three disjoint
subsets: a single node r, the root, and two possibly empty sets that are binary trees, called the left sub-
tree TL and right sub-tree TR of the root r
So simply, T is a binary tree if it
has no nodes, or is of the form:
A full binary tree is when every
node up to a certain level is
present:
In a binary tree, every node has 0,
1 or 2 children, and in a full binary
tree, every node has 0 or 2
children
In a binary tree, each node can
have a left sub-tree and a right
sub-tree attached to it
In ordered binary trees, every node is
arranged so that for every node, the node item is less
than (<) the all the node items in it’s left sub-tree and is
greater than (>=) all the node items in the right sub-tree
If a tree is unordered, then to search, you must traverse the whole tree node by node
However, if a tree is ordered, you can use a binary search of the tree to increase efficiency, as well this can be done recursively
To ‘traverse’ a tree means to visit every node (traverse algorithms are the same for ordered and unordered trees)
The reasons for traversals could include: printing all node values, counting nodes, searching the nodes, etc.
There are 3 orders to the traversals: (1) Pre-order (means you visit ‘before’ left, right), (2) Post-order (go left, right, then visit ‘after’),
(3) In-order (visit in-between left and right)
These traversals can also be accomplished recursively
Pre-order traversal: (1) Visit the current node (starting at the root), (2) Traverse the left sub-tree, (3) Traverse the right sub-tree
Post-order traversal: (1) Traverse the left sub-tree, (2) Traverse the right sub-tree, (3) Visit the current node (starting at the root)
In-order traversal: (1) Traverse the left sub-tree, (2) Visit the current node (starting at the root), (3) Traverse the right sub-tree
BINARY TREES – UNIT REVIEW TUESDAY, APRIL 25, 2017 COMPUTER SCIENCE
BINARY TREES – UNIT REVIEW PAGE 2 OF 2 COMPUTER SCIENCE
The following list represents some methods and properties that could be coded into a Tree ADT to increase
its functionality:
− A TreeNode class different than the LinkedList Node class that has self-references to left and
right (not next and previous)
− A recursive insert(data) method in the TreeNode class that inserts a new node recursively to the
left or right based on if the data is “less than” (to the left), or “greater than or equal to” (to the right)
− Tree properties include a root TreeNode reference
− The search(data) → boolean method: determines if the data is in the tree or not
− The preOrder() method: traverses the list and ‘outputs’ the nodes in pre-order
− The postOrder() method: traverses the list and ‘outputs’ the nodes in post-order
− The inOrder() method: traverses the list and ‘outputs’ the nodes in-order
BINARY TREES – UNIT REVIEW TUESDAY, APRIL 25, 2017 COMPUTER SCIENCE