0% found this document useful (0 votes)
46 views

Today: Trees

This document defines and explains trees and binary trees. It begins by defining what a tree is in graph theory as a linked set of nodes without loops. Binary trees are then introduced as trees where each node has at most two children. The document discusses tree terminology like root, leaf nodes, and sub-trees. It provides examples of searching, traversing, and inserting nodes into binary trees and presents recursive algorithms for each.

Uploaded by

sans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Today: Trees

This document defines and explains trees and binary trees. It begins by defining what a tree is in graph theory as a linked set of nodes without loops. Binary trees are then introduced as trees where each node has at most two children. The document discusses tree terminology like root, leaf nodes, and sub-trees. It provides examples of searching, traversing, and inserting nodes into binary trees and presents recursive algorithms for each.

Uploaded by

sans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

I.B.

COMPUTER SCIENCE

Today: Trees
Students let us examine ...

THE DEFINITION OF TREES


Trees
The ADT structures we have examined so far have been
linear in nature (e.g. linked lists, stacks, queues), however
data can be organized in a non-linear, hierarchical fashion,
where an item can have more than one immediate
successor
This structure is known as a tree
In graph theory, the definition of a tree is a linked set of
nodes that does not have any loops
In our I.B. HL curriculum, we will study a type of tree known
as a binary tree
However, if time remains, we may examine other types of
trees (in theory)
Trees (cont’d)
Trees are used to represent relationships
All trees 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 here:
In this tree: A is the parent of B and A
C, and B and C are the children of A
B and C are defined as siblings
(children of the same parent) B
C
Each node in a tree has one
parent, and one node (only
one) has no parents, this node
D E
is called the root of the tree
In this tree, the node A is the root (Computer Scientists usually draw
trees from the root down, opposite to the way they grow in nature)
Trees (cont’d)
Nodes that have no children are
known as leaf nodes A
The paths between nodes are
known as branches (or edges) B
C
In this tree, D, E, and C are
leaf nodes
The terminology for trees can D E
also be extended so that we
can say that A is an ancestor of D, E, and F
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
Trees (cont’d)
A sub-tree is a portion of the larger A
tree, it is defined as a node
together with all of its
descendants
B
For example, this is a C
sub-tree of the main tree
So using these definitions,
in this tree, we can say that D E
B is the parent of D and E in
the sub-tree, A has no parent,
B and C are children of A, C has no children and is therefore a
leaf, A has no siblings, A is an ancestor of B, C, D, and E and
has no ancestors, therefore is the root, D has no descendants
and is therefore a leaf
Students now we turn our attention to…

BINARY TREES
Trees (cont’d)
A general tree is defined as one or more nodes such that the tree T
is partitioned into disjoint subsets:
A single node, the root r
Sets that are general trees, called subsets or r
The I.B. curriculum want you to focus on binary trees, which 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
Two possibly empty sets that are binary trees, called the
left and right sub-trees of r r
So simply, T is a binary tree if it
has no nodes, or is of the form: TL TR
Trees (cont’d)
A full binary tree is when every node up to a certain level is present:
1 level, 1 node 2 levels, 3 nodes 3 levels, 7 nodes 4 levels, 15 nodes

number of nodes (n),


and level (l) n = 2l − 1

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
This is an example of tree code:
Now
we
write a
Tree
class
Trees (cont’d)
In the class signature line:

The code that adds this:


Is being used to enforce the Comparable interface with the generic
data type (T)
Remember that interfaces are used to ensure that classes that
“implements” them ensure that certain methods are overridden
In this case, it ensures that any class used as a generic with the
TreeNode data has enforced this and has overridden the
compareTo() method
The compareTo() method is a method that can see if two of the same
objects are greater than >, less than <, or equal to == one another
If you write a custom class and want to use this Tree ADT, you will have to
write a compareTo() method and define how it works with your class
Trees (cont’d)
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, for e.g.
D

B E
Equal nodes aren’t
A C G
usually allowed in
ordered
H
binary trees F

We need methods to add a node (insert into the tree), delete a


node (prune the tree), travel through the tree (traverse),
search the tree, print the tree, etc.
Students now we turn our attention to…

SEARCHING A BINARY TREE


Trees (cont’d)

If a tree is unordered, then to search, you must traverse the whole tree node by node
(seen later)
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, for
example:

A base case, where a leaf


node is passed

The recursive call to


itself, it searches the
left sub-tree then the
right sub-tree

Another base case,


where the node data is
equal
Trees (cont’d)
Notice that the method recursiveSearch is
private, and the method search is public (it
calls recursiveSearch) so the use of search
hides the node pointer from the users of the class (this
can be called a “wrapper” method)
Modifications to a search might want to return the level
the data was found at, or otherwise, so more
parameters and code modification would have to take
place
The search algorithm could also be coded non-
recursively but in a longer more counter-intuitive way
Students now we turn our attention to…

TRAVERSING A TREE
Trees (cont’d)
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
2. In-order
3. Post-order
These traversals can also be accomplished recursively
Trees (cont’d)
In a Pre-order
traversal:
60
1. Visit the
current node 20 70
(starting at the 10 40
root)
2. Traverse the 30 50

left sub-tree 60 20 10 40 30 50 70

3. Traverse the
right sub-tree
The algorithm for this can be seen here:
Trees (cont’d)
In a Post-order
traversal: 60
1. Traverse the 20 70
left sub-tree
10 40
2. Traverse the
right sub-tree 30 50
3. Visit the 10 30 50 40 20 70 60
root node
(of the
sub-tree)
The algorithm for this can be seen here:
Trees (cont’d)
In a In-order
traversal: 60
1. Traverse the 20 70
left
10 40
sub-tree
2. Visit the root 30 50
node 10 20 30 40 50 60 70
(of the
sub-tree)
3. Traverse the right sub-tree
The algorithm for this can be seen here:
Students now we turn our attention to…

INSERTING NODES INTO A TREE


Trees (cont’d)
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 inserting 15,8,12,19,5,23
1. 15 becomes the root
2. Insert 8 (8<15) 15
3. Insert 12 (12<15, 12>8) 8 19
4. Insert 19 (19>15)
5. Insert 5 (5<15, 5<8) 5 12 23
6. Insert 23 (23>15, 23>19)
If you receive the numbers in a different order, you build a different
shaped binary search tree (BST)
A code sample for insertion is seen here, part of which is done in the
Node class, with a recursive algorithm:
Trees (cont’d)

Add this method to the TreeNode class


Trees (cont’d)

And an insert method in the Tree class


Add these
remaining
methods
to the
Tree
class, and
test the
tree…
5

2
9

1 4 8 10

3 7

6
Students now we turn our attention to…

MORE ABOUT TREES (THEORY)


Trees (cont’d)
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:
60 60
20 70 20, 70

10 40 10, 40

30 50 30, 50

There is no easy recursive method for doing this, the best way to
program this is using a queue:
You would get a node from the queue, put it’s left child on the queue,
then put it’s right child on the queue
Trees (cont’d)
Removing a node from a tree can be more difficult, and three scenarios exist:
1. The node has 0 children Set to
2. The node has 1 child 5 null

3. The node has 2 children


To remove a leaf (0 children) you
2 10
just set the pointer to the leaf
node to null
To remove a node with 1 child,
just redirect the pointer of the 5
node pointing to the node to
be deleted to that node’s child
To remove a node with 2
2 10
children, a shift of the tree
needs to occur (this Redirect this
complexity does not 1 pointer
need to be examined here)
I.B. COMPUTER SCIENCE
Let’s try
this in an
example!

Thursday, February 23, 2017

You might also like