DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Solving Unique Search Requirements Using TreeMap Data Structure
  • Red-Black Trees in C#: A Guide to Efficient Self-Balancing Binary Search Trees
  • Understanding AVL Trees in C#: A Guide to Self-Balancing Binary Search Trees
  • Unlocking the Potential of Binary Search Trees with C# Programming

Trending

  • How To Set up a Push Notification Service in 30 Minutes or Less
  • Real-Time Anomaly Detection
  • Unblock Your Software Engineers With Unblocked
  • Amazon EC2 Deep Dive: Optimizing Workloads With Hardware Insights
  1. DZone
  2. Coding
  3. Java
  4. Java - How to Create a Binary Search Tree

Java - How to Create a Binary Search Tree

Ajitesh Kumar user avatar by
Ajitesh Kumar
·
Dec. 28, 14 · Interview
Like (2)
Save
Tweet
Share
73.3K Views

Join the DZone community and get the full member experience.

Join For Free

this article represents the high level concept and code samples which could be used to create a binary search tree in java. please feel free to comment/suggest if i missed to mention one or more important points. also, sorry for the typos.

following are the key points described later in this article:

  • what is a binary search tree?
  • what are different kind of traversals?
  • code samples
what is a binary search tree?

a binary search tree is a binary tree in which every node contains a key that satisfies following criteria:

  • the key in left child is less than the key in the parent node
  • the key in the right child is more than the parent node
  • the left and right child are again binary search trees.

following diagram represents a binary search tree:

binary_search_tree

what are different kind of traversals?

following are three different kind of traversals:

  • preorder traversal : in preorder traversal, the node is visted first and then, left and right sub-trees.
  • inorder traversal : in inorder traversal, the node is visited between left and right sub-tree.
  • postorder traversal : in postorder traversal, the node is visited after left and right subtrees.
code sample – how to create a binary search tree

if the numbers such as {20, 15, 200, 25, -5, 0, 100, 20, 12, 126, 1000, -150} are to be stored in a binarytree (represented by code below), following would get printed using different kind of traversal mechanism:

//preorder traversal
20, 15, -5, -150, 0, 12, 200, 25, 20, 100, 126, 1000

// inorder traversal
-150, -5, 0, 12, 15, 20, 20, 25, 100, 126, 200, 1000

//postorder traversal
-150, 12, 0, -5, 15, 20, 126, 100, 25, 1000, 200, 20

following is the code for creating binary tree that uses following binarytree class and traversals:

binarytree tree = new binarytree( 20 );
int[] nums = {15, 200, 25, -5, 0, 100, 20, 12, 126, 1000, -150};
for(int i : nums ) {
tree.addnode( i );
}

tree.traversepreorder();
tree.traverseinorder();
tree.traversepostorder();

following is the code for binarytree class:

public class binarytree {

private int data;
private binarytree left;
private binarytree right;

public binarytree(int num) {
this.data = num;
this.left = null;
this.right = null;
}
// as a convention, if the key to be inserted is less than the key of root node, then key is inserted in
// left sub-tree; if key is greater, it is inserted in right sub-tree. if it is equal, as a convention, it 
// is inserted in right sub-tree
public void addnode(int num) {
if (num < this.data) {
if (this.left != null) {
this.left.addnode(num);
} else {
this.left = new binarytree(num);
}

} else {
if (this.right != null) {
this.right.addnode(num);
} else {
this.right = new binarytree(num);
}

}
}
// visit the node first, then left and right sub-trees
public void traversepreorder() {
system.out.println( this.data );
if( this.left != null ) {
this.left.traversepreorder();
}
if( this.right != null ) {
this.right.traversepreorder();
}
}
// visit left sub-tree, then node and then, right sub-tree
public void traverseinorder() {
if( this.left != null ) {
this.left.traverseinorder();
}
system.out.println( this.data );
if( this.right != null ) {
this.right.traverseinorder();
}
}
// visit left sub-tree, then right sub-tree and then the node
public void traversepostorder() {
if( this.left != null ) {
this.left.traversepostorder();
}
if( this.right != null ) {
this.right.traversepostorder();
}
system.out.println( this.data );
}

}


Binary search tree Tree (data structure) Java (programming language)

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Solving Unique Search Requirements Using TreeMap Data Structure
  • Red-Black Trees in C#: A Guide to Efficient Self-Balancing Binary Search Trees
  • Understanding AVL Trees in C#: A Guide to Self-Balancing Binary Search Trees
  • Unlocking the Potential of Binary Search Trees with C# Programming

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: