Let us see How to define the structure of the binary tree. For simplicity. We assume that the data of the nodes are GenericType so that Tree can hold any type of data. one way to represent a node (which contains data) is to have two links/pointer that point to left and right children along with data field as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kkjavatutorials.tree; /** * Structure of Binary Tree Node * @author KK JavaTutorials */ public class Node<T> { //Data hold by Binary Tree Node private T data; //Left pointer/reference private Node<T> left; //Right pointer/reference private Node<T> right; public Node(T data) { super(); this.data = data; left = null; right = null; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getLeft() { return left; } public void setLeft(Node<T> left) { this.left = left; } public Node<T> getRight() { return right; } public void setRight(Node<T> right) { this.right = right; } } |
In trees, the default flow is usually from parent to children and it is not mandatory to show directed branches. for our discussion, we assume both the Representations shown below are the same
You May Also Like:
Introduction to Tree Data Structure
Introduction to Binary Tree
That’s all about Structure of Binary Trees
If you have any feedback or suggestion please feel free to drop in below comment box.