Okay so in class my professor was going over this code:

class BinaryTreeNode<T> {
    protected T element;
    protected BinaryTreeNode <T> left, right;
    
    BinaryTreeNode (T obj) {
       element = obj;
       left = null;
       right = null;      
    }  // constructor BinaryTreeNode

    public int numChildren() {
       int children = 0;
       if (left != null)            
          children = 1 + left.numChildren();
       if (right != null)            
          children = children + 1 + right.numChildren();
        return children;
    }     
}

I followed everything about the different kind of traversal sequences. And the code makes since but why is there <T> after the Class at the beginning. I know the<> declares of what type. But I couldn't find on google a type T? Any help?

I believe it comes from the word "template", and Java also uses the term "generic", which you can google. The idea is that you want a data structure that can operate on any kind of data. So you don't want to write a Binary Tree class that only deals with String data, because if you later have some other kind of data that's not a String, you'd have to write another Binary Tree class. So rather than writing a new class every time your data changes, you use a "template" for the data structure.

It's called Generics, and I think that this will give you some sense into it :)

<T> is used to represent a general datatype
for example we can write a code for adding two numbers
this addition can be either with int data type,float data type or double type.
For this we have to write three functions each working with int,float or double data types.
Using <T> we can write a single function which can be used to work with any datatype..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.