Professional Documents
Culture Documents
AiML 3 1 SG
AiML 3 1 SG
2
Objectives
• This lesson covers the following objectives:
−Understand a node
−Understand a binary tree
−Create a Node class
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 3
3
Decision Tree Algorithms
• Decision tree algorithms
construct a model based on
answers (or values) from data
• For more information, the
Oracle Academy Java
Programming course
introduces Binary Trees
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 4
4
Binary Trees
• A binary tree is a data
1
structure based on the
idea of nodes that have
both a left and a right
reference to other 2 3
nodes
• This creates a structure
4 5 6
where there are a
maximum of 2 child
nodes from every node
7
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 5
5
Binary Trees
• The first node is called
1
the root node, and is
the building block of all Root
other nodes
• Apart from the root 2 3
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 6
6
Binary Trees
• Each node can have 0,
Siblings 1
1 or 2 child nodes
• If a node has no
children, it is called a
leaf 2 3
7
Leaf
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 7
7
Binary Trees
• In the diagram, node 2 is
1
the parent of the siblings
nodes 4 and 5
• It is also the child of the
parent root node 1 2 3
TASK:
• What is the parent of 6? 4 5 6
• What is 1 the parent of?
• Explain the relationships
7
of 5
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 8
8
Binary Trees
TASK Solution:
1
• What is the parent of 6?
− 3
• What is 1 the parent of? 2 3
− 2 and 3
• Explain the relationships
of 5 4 5 6
− 5 is the child of 2
− 5 is the sibling of 4
− 5 is the parent of 7 7
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 9
9
Binary Trees
• Binary Trees are useful to
reflect linked hierarchical 1
scenarios
• We will create a class called
Node 2 3
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 10
10
Binary Trees
• The data type will be integer,
but this could be any data 1
type
• The class could be designed as
generic, and then we would 2 3
4 5 6
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 11
11
Binary Trees
• It will have a left and right
property that will be of type 1
Node
• If it is a leaf node, it will be
null 2 3
4 5 6
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 12
12
Binary Trees Structure
Node
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 13
13
Task – Binary Tree Creation
Try the following yourself before reading ahead to the
solution.
• Create the Node class in your Java IDE
• Define the properties for your Node
• Create a constructor that will have the following
Signature
Node(int data);
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 14
14
Solution – Node Class
public class Node {
int data;
Node left;
Node right;
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 15
15
Tree Methods
• These methods are required to manipulate your tree
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 16
16
Task - Define Methods
• Define the methods from the previous slide in the
Node class you created earlier
• Skip the print method for now, as we will discuss this in
the next lesson
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 17
17
Solution – Methods
public void setLeft(Node node) {
if (left == null)
left = node;
}
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 18
18
Solution – Methods
public Node getRight() {
return right;
}
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 19
19
Summary
• In this lesson, you should have learned how to:
−Understand a node
−Understand a binary tree
−Create a Node class
AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 20
20
21