Binary Tree
Binary Tree
Root: The root of a tree is the topmost node of the tree that has no parent node. There is
only one root node in every tree.
Edge: Edge acts as a link between the parent node and the child node.
Leaf: A node that has no child is known as the leaf node. It is the last node of the tree. There
can be multiple leaf nodes in a tree.
Depth: The depth of the node is the distance from the root node to that particular node.
Height: The height of the node is the distance from that node to the deepest node of the
tree.
Height of tree: The Height of the tree is the maximum height of any node.
1. One reason to use trees might be because you want to store information that naturally forms a
hierarchy. For example, the file system on a computer:
2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and
slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered
Linked Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on the number of nodes as
nodes are linked using pointers.
Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
Binary Tree Representation: A tree is represented by a pointer to the topmost node of the tree. If
the tree is empty, then the value of the root is NULL.
A Tree node contains the following parts.
1. Data
2. Pointer to the left child
3. Pointer to the right child
In C, we can represent a tree node using structures. In other languages, we can use classes as part of
their OOP feature. Below is an example of a tree node with integer data.
C
struct node
int data;
};
Java
class Node
{
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
PreOrder Traversal: Here, the traversal is: root – left child – right child. It means that the
root node is traversed first then its left child and finally the right child.
InOrder Traversal: Here, the traversal is: left child – root – right child. It means that the
left child is traversed first then its root node and finally the right child.
PostOrder Traversal: Here, the traversal is: left child – right child – root. It means that
the left child is traversed first then the right child and finally its root node.
Tree
1 //Root Node
/ \
2 3
/\ /\
4 5 6 7 //Leaf Nodes
Code
// Class containing left and right child
// of current node and key value
class Node {
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
// A Java program to introduce Binary Tree
class BinaryTree {
// Root of Binary Tree
Node root;
// Constructors
BinaryTree(int key) { root = new Node(key); }
BinaryTree() { root = null; }
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
// create root
tree.root = new Node(1);
/* following is the tree after above statement
1
/ \
null null */
tree.root.left = new Node(2);
tree.root.right = new Node(3);
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
null null null null */
tree.root.left.left = new Node(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 null null null
/ \
null null
*/
}
}