generic binary search tree java

Solutions on MaxInterview for generic binary search tree java by the best coders in the world

showing results for - "generic binary search tree java"
Lucas
18 Jan 2018
1public class BinaryTree<T extends Comparable<T>> {
2	TreeNode<T> root;
3	
4	public BinaryTree() {
5		super();
6		this.root = new TreeNode<T>();
7	}
8 
9	public BinaryTree(T o) {
10		super();
11		// TODO Auto-generated constructor stub
12		TreeNode<T> node = new TreeNode<T>(o);
13		this.root = node;
14	}
15	
16	public void add(T o) {
17		if (root == null) {
18			root = new TreeNode<T>(o);
19		} else {
20			root.insert(o);
21		}
22	}
23	
24	public boolean isMember (TreeNode<T> node, T o) {
25		if(node == null) return false;
26		
27		if (node.element.compareTo(o) == 0) {
28			return true;
29		} else if (node.element.compareTo(o) < 0) {
30			isMember(node.right, o);
31		} else {
32			isMember(node.left, o);
33		}
34		return false;
35	}
36	
37	public void preOrderHelper (TreeNode<T> node) {
38		if (node != null) {
39			node.visit();
40			preOrderHelper(node.left);
41			preOrderHelper(node.right);
42		}
43	}
44	
45	public void inOrderHelper(TreeNode<T> node) {
46		if (node != null) {
47			inOrderHelper(node.left);
48			node.visit();
49			inOrderHelper(node.right);
50		}
51	}
52	
53	public void postOrderHelper(TreeNode<T> node) {
54		if (node != null) {
55			postOrderHelper(node.left);
56			postOrderHelper(node.right);
57			node.visit();
58		}
59	}
60	
61	public void preOrder() {
62		if (root != null) {
63			preOrderHelper(root);
64		}
65	}
66	
67	public void inOrder() {
68		if (root != null) {
69			inOrderHelper(root);
70		}
71	}
72	
73	public void postOrder() {
74		if (root != null) {
75			postOrderHelper(root);
76		}
77	}
78	
79}
80
queries leading to this page
binary search tree in java examplehow to change binary search tree to generic type in javasearch a generic binary treeimplementing a generic binary search tree in javasearch a binary search tree javageneric binary tree javabinary search tree java codeimplement a binary search tree in javageneric binary search tree java useselect method in binary search tree javafind value in binary search tree javahow to create binary search tree in javahow to create a binary search tree javafind node in binary search tree javahow to implement binary search tree in javasearch binary tree javacreate binary search tree javais binary search tree javahow to search a binary tree java search method in binary search tree java codebinary search tree implementation in javajava library binary treebinary tree generics javasearch in binary tree javahow to make binary search tree in javageneric binary tree implementation in javaverify a binary search tree in javahow to create a binary search tree in javabinarysearch tree javajava binary search tree codebinary search tree java using string keyshow to find a node in a binary search tree javaaccess value in binary search tree javasearching method binary tree code in javatree binary search javasearch in binary search tree in javaimplementing a binary search tree in javasearching in binary search tree javasearch a binary tree javageneric binary search tree javajava create binary search treehow to make a binary search tree javahow to search the binary tree javacreate a binary search tree in javajava binary tree genericbuilt in binary search tree javabinary search tree example in java what is a binary search tree javabinary tree search program in java exampleform a binary search tree code in javabinary tree implementation java genericscreating binary search tree in javabinary search tree java examplegeneric binary search tree java