For those who are new to Binary Search Tree, note that Binary Search Tree is defined as tree that satisfy some of the following criteria:
Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Pay attention to some of the following:
public class BinaryStringTree { private String data; private BinaryStringTree left; private BinaryStringTree right; public BinaryStringTree() { this.data = null; this.left = null; this.right = null; } public BinaryStringTree(String data) { this.data = data; this.left = null; this.right = null; } public static BinaryStringTree createTree( String content ) { BinaryStringTree bstree = new BinaryStringTree(); if( content != null ) { // // Remove the punctuation from the content // content = content.replaceAll("(\\w+)\\p{Punct}(\\s|$)", "$1$2"); String[] words = content.split( " " ); bstree = new BinaryStringTree(); for( int i = 0; i < words.length; i++ ) { bstree.addNode( words[i] ); } } return bstree; } // 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(String data) { if (this.data == null) { this.data = data; } else { if (this.data.compareTo(data) < 0) { if (this.left != null) { this.left.addNode(data); } else { this.left = new BinaryStringTree(data); } } else { if (this.right != null) { this.right.addNode(data); } else { this.right = new BinaryStringTree(data); } } } } public void traversePreOrder() { System.out.println(this.data); if (this.left != null) { this.left.traversePreOrder(); } if (this.right != null) { this.right.traversePreOrder(); } } public void traverseInOrder() { if (this.left != null) { this.left.traverseInOrder(); } System.out.println(this.data); if (this.right != null) { this.right.traverseInOrder(); } } public void traversePostOrder() { if (this.left != null) { this.left.traversePostOrder(); } if (this.right != null) { this.right.traversePostOrder(); } System.out.println(this.data); } }
Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…
Last updated: 2nd May, 2024 The success of machine learning models often depends on the…
When working on a machine learning project, one of the key challenges faced by data…
Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…
Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…
Last updated: 1st May, 2024 In this post, you will learn the concepts related to…
View Comments
so please can you tell me how can add a contractor to find the node?