You are on page 1of 2

BINARY TREES – UNIT REVIEW PAGE 1 OF 2 COMPUTER SCIENCE

BINARY TREES – UNIT REVIEW


COMPUTER SCIENCE – IN JAVA AND C#
DEFINITION:

 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

SEARCHING AND TRAVERSING A 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

INSERTING AND REMOVING FROM A TREE:

 If a tree is unordered, then to search, you must traverse the whole


tree node by node
 With an unordered tree, you can insert a node anywhere you have a
null
 With an ordered binary tree, there is only one spot you can insert a new node
(which you search for) – for example, insert: 15, 8, 12, 19, 5, 23
 If you receive the numbers in a different order, you build a different shaped binary
search tree (BST)
 Removing a node from a tree can be more difficult, and three scenarios exist: (1)
The node has 0 children, (2) The node has 1 child, (3) The node has 2 children
 To remove a leaf (0 children) you just set the pointer to the leaf node to null
 To remove a node with 1 child, just redirect the pointer of the node pointing to the
node to be deleted to that node’s child
 To remove a node with 2 children, a shift of the tree needs to occur (this
complexity does not need to be examined here)

SAMPLE LINKED TREE PROPERTIES AND METHODS:

 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

OTHER TREE FACTS:

 Non-binary trees can contain more than two branches from


a node
 Another traversal you can do is a breadth-first traversal, in
which you visit nodes level by level, for example a breadth
first traversal of this tree (there is no easy recursive method
for doing this)

BINARY TREES – UNIT REVIEW TUESDAY, APRIL 25, 2017 COMPUTER SCIENCE

You might also like