Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

1

Artificial Intelligence with


Machine Learning in Java
3-1
Binary Trees

Copyright © 2020, Oracle and/or its affiliates. All rights reserved.

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

node every other node


must be directly
4 5 6
connected to a parent
node
7

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

• Two nodes that have


the same parent are 6
4 5
called siblings

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

• Each instance of the Node


class will represent a node on
4 5 6
the tree

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

set its data type at design time

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

Left Data Right Null


Null

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;

public Node(int data) {


this.data = data;
left = null;
right = null;
}

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

Method Signature Description


void setLeft(Node node) To add a node to the current left node. If the current left
node is null then set left equal to node.
void setRight(Node node) To add a node to the current right node. If the current
right node is null then set right equal to node.
Node getLeft() Return the current left node.
Node getRight() Return the current right node.
void setData(int data) Set the data value of the current node.
void getData() Get the data value of the current node.
void print() Display the data in the given node and all data
underneath.

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;
}

public void setRight(Node node) {


if (right == null)
right = node;
}

public Node getLeft() {


return left;
}

AiML 3-1
Binary Trees Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 18

18
Solution – Methods
public Node getRight() {
return right;
}

public int getData() {


return data;
}

public void setData(int data) {


this.data = data;
}

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

You might also like