85940

How can i traverse a binary tree from right to left in java?

I want to traverse a binary tree from right to left and add to a queue every item with same last name. I have correctly implement a Queue List class and a Tree Node class but, i get a null pointer exception when i try to find something. (Of course i have written an insertion method for the binary tree).

public class ST { private TreeNode root; private int size; private Queue q; public Queue searchByLastName(String last_name) { searchByLastNameRec(this.root, last_name); return q; } private void searchByLastNameRec(TreeNode newroot, String last_name) { if (newroot == null) return; if (newroot.right != null) { if (newroot.right.item.getLast_name().equalsIgnoreCase(last_name)) { q.put(newroot.right.item); } searchByLastNameRec(newroot.right, last_name); } if (newroot.left != null) { if (newroot.left.item.getLast_name().equalsIgnoreCase(last_name)) { q.put(newroot.left.item); } searchByLastNameRec(newroot.left, last_name); } } public class TreeNode { Suspect item; TreeNode left, right, parent; int N; public TreeNode(Suspect item) { if (item == null) throw new IllegalArgumentException(); this.item = item; } }

Answer1:

Try this

private void searchByLastNameRec(TreeNode newroot, String last_name) { if (newroot == null || newroot.item == null) return; if (Objects.equals(last_name, newroot.item.getLast_name())) q.put(newroot.item); searchByLastNameRec(newroot.right, last_name); searchByLastNameRec(newroot.left, last_name); }

Answer2:

I'm guessing some of the items in your tree are null, or some have null last names. Since you check for a null item when you construct a node, I'm leaning towards the latter.

Try this:

public TreeNode(Suspect item) { if (item == null) throw new IllegalArgumentException(); if (item.getLast_name() == null) throw new IllegalArgumentException(); this.item = item; }

Recommend

  • refreshing treeview component from other form
  • -1 #IND Question
  • On a postback the ajax call to a WCF webservice uses postData but the JSON.stringify fails on mainly
  • C++: Why does dereferencing this vector iterator segfault?
  • Best way to represent list of XPaths in java
  • Why the time complexity of an array insertion is O(n) and not O(n+1)? [duplicate]
  • Find 4 minimal values in 4 __m256d registers
  • pure javascript dom dynamic insert, update and delete
  • Cut the background to expose the layer below
  • Get the number 18437736874454810627
  • In loopback documentation what does variable 'cb' stands for?
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Simple linked list-C
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Elasticsearch script query involving root and nested values
  • Object and struct member access and address offset calculation
  • How to assign byte[] as a pointer in C#
  • CakePHP ACL tutorial initDB function warnings
  • JBoss External Properties Files in Classpath
  • Insert new calendar with SyncAdapter- Calendar API Android
  • htaccess add www if not subdomain, if subdomain remove www
  • Ensure fsync did its job
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • output of program is not same as passed argument
  • javascript inside java/jsp code
  • Javascript + PHP Encryption with pidCrypt
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Android Studio and gradle
  • Acquiring multiple attributes from .xml file in c#
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can I use threading to 'tick' a timer to be accessed by other threads?