Binary Tree

Roshan Kumar Sah
Dev Genius
Published in
3 min readFeb 19, 2021

--

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

Structure of definition.
Here we are creating Binary Tree Node which is generic over Comparable
Parent: It’s a node which will check whether it’s a root or not.
LeftNode: It’s node which is aligned to left side of it’s parent Node.
RightNode: It’s node which is aligned to right side of it’s parent Node.

Binary Tree

Write: Adding new node based on input element. If it is greater than current payload it will be appended on left else right of current Node.

Create and Write

READ: Traversal can be done in two ways.
1. Depth First Traversal:
a. Inorder: Left Node — — (Value )— —RightNode
b. PreOrder: (Value ) — — Left Node — — RightNode
c. PostOrder: Left Node — — RightNode — — (Value )

Depth First Traversal

2. Breadth First Traversal
a. Level Order

Breadth First Traversal

SEARCH: Searching an element in B-Tree.

Searching

DELETE:
We will talk about delete operation later, Since deletion required some sort of rearrangement of B-Tree. (Hopefully in next post)

Some Common problems.
Finding minimum Value in B-Tree:
Minimum value is the value which will be in leftmost node in B-Tree.

Minimum Value

Finding maximum Value in B-Tree:
Maximum value is the value which will be in rightmost node in B-Tree.

Minimum Value

Finding minimum node in B-Tree:
Minimum node is the node which will be in leftmost node in B-Tree.

Minimum Node.

Finding maximum node in B-Tree:
Maximum node is the node which will be in rightmost node in B-Tree.

Maximum Node

Finding Height of B-Tree:
Height of binary tree.

Height of Tree

For Source Code:
https://gist.github.com/Roshankumar350/8d9cbc490391b11afa042af3105b9600

Keep reading..
Keep reading..
Reach me out if you have any doubt, suggestion and feedback.
Roshankumar350@gmail.com

--

--