Results 1 to 6 of 6
Thread: BSTree
- 05-27-2012, 02:27 PM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
BSTree
Hi programmers.
Please check if the following BSTree is ok?
I only want to crate a BSTree of Strings, nothing else.
Thank you.Java Code:import java.util.Scanner; class Node { private Node left; private String number; private Node right; public Node() { left = null; number = "0"; right = null; } public Node(String x) { left = null; number = x; right = null; } public String getNumber() { return number; } public Node getLeft() { return left; } public Node getRight() { return right; } public void setNum(String x){ number = x; } public void setLeft(Node leftNode) { left = leftNode; } public void setRight(Node rightNode) { right = rightNode; } } class BST{ private Node root; public BST(){ root = null; } public boolean isEmpty(){ return root == null; } public void insert(String x){ insertData(x, root); } private void insertData(String n, Node p){ if(p == null){ p = new Node(n); root = p; } else if(n.compareTo(p.getNumber()) < 0){ if(p.getLeft() == null){ p.setLeft(new Node(n)); } else { p = p.getLeft(); insertData(n,p); } } else { if(p.getRight() == null){ p.setRight(new Node(n)); } else { p = p.getRight(); insertData(n, p); } } } } public class BinarySearchTree{ public static void main(String[] args){ BST tree = new BST(); Scanner in = new Scanner(System.in); for (int y=0; y<5;y++){ String x = in.nextLine(); tree.insert(x); } } }
- 05-27-2012, 02:50 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: BSTree
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-27-2012, 03:14 PM #3
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: BSTree
I made it and i don't know if it is a correct Binary Search Tree.
Just i want be sure for this.
- 05-27-2012, 03:18 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-27-2012, 03:29 PM #5
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
- 05-27-2012, 03:58 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks