### Representing a binary tree

• Representing a Node of a Binary Tree (in general)

• A node is an object

 Objects are represented with a class

• Information that a node must store:

Explanation:

• data fields = contains information

 left = contains a link (reference) to the left child node right = contains a link (reference) to the right child node

• Comment:

• You can view the node of a binary tree as a generalization of a node of a list:

 A node of a list has only one link (the next link filed points to the next node A node of a binary tree has two links pointing the the left and right child nodes

• The Node structure used in my examples

• Data fields used in my example:

• I will use a double value as data (information) field:

 ``` double value; // Information filed(s) ```

• Example of a binary tree with numbers as data:

• Java class used to define a node of the binary tree:

 ``` public class Node { /* ========================= Data fields ========================= */ public double value; // Information field public Node left; // reference to the left child node public Node right; // reference to the right child node /* =================================== Constructor: make a Node =================================== */ public Node( double x ) { value = x; left = null; right = null; } } ```

• How the links between the nodes are represented in Java:

• Representing a binary tree

• Facts:

• All nodes in a binary tree can be accessed through the root node of the binary tree

How:

• Starting at the root node, we can visit all nodes at level 1:

• From the level 1 nodes, we can visit all nodes at level 2:

• And so on....

• Just like the linked list, the programmer has immediate access to only one node of the binary tree:

 The programmer has immediate access to the root node of the binary tree: Starting at the root node, a program can gain access to any and every node in the binary tree

• Java class used to represent a binary tree

• Just like the linked list, we use a Node object reference variable to represent a binary tree

(Except, this variable is called: root)

 ``` public class BinTree { public Node root; // points to the root node of a Binary Tree .... (Rest of the class contains methods that manipulate the Binary Tree) } ```

• Example on how to use the root variable to represent a binary tree:

The root variable contains the address of the root node of the binary tree.

• Representing the empty binary tree

• A empty binary tree (i.e., a tree with no nodes is represented as follows:

• Constructing an empty binary tree

• Knowing how to represent an empty tree leads us to define the constructor for the Binary Tree class:

 ``` public class BinTree { public Node root; // points to the root node of a Binary Tree /* ==================================== Constructor ==================================== */ public BinTree() { head = null; // Empty binary tree } .... (Rest of the class contains methods that manipulate the Binary Tree) } ```

• Manipulating a binary tree

• We will study the following manipulations next:

 Find a node in a binary tree Insert a node into the binary tree Delete a node from the binary tree

• How to approach the material...

• Study node:

• It is a lot harder to traverse and update a binary tree than a linked list...

• But the principle to understanding the procedures remains the same:

• You should not memorize the code....

• Rather:

 understand what you need to change in the binary tree (which links need to be updated) understand the statements used that update the link variables