binary-tree

binary search tree impelementation and java

跟風遠走 提交于 2019-12-01 01:06:40
I am trying to implement BST algorithm using Cormen's pseudo code yet having issue. Here is my Code for Node: public class Node { Node left; Node right; int value; Node(int value){ this.value = value; this.left = null; this.right = null; } } and for the Bstree: public class Btree { Node root; Btree(){ this.root = null; } public static void inorderWalk(Node n){ if(n != null){ inorderWalk(n.left); System.out.print(n.value + " "); inorderWalk(n.right); } } public static Node getParent(Btree t, Node n){ Node current = t.root; Node parent = null; while(true){ if (current == null) return null; if(

Print all paths from root to leaf in a Binary tree

若如初见. 提交于 2019-12-01 00:04:12
Here is the code I wrote to print all paths of a Binary tree from root to leaf: public static void printRootToLeaf(Node1 root, List<Integer> list) { if(root == null) { return; } list.add(root.data); if(root.left == null && root.right == null) { System.out.println(list); return; } printRootToLeaf(root.left, list); printRootToLeaf(root.right, list); } I am calling this method in main like this: public static void main(String[] args) { Node1 root = new Node1(1); Node1 two = new Node1(2); Node1 three = new Node1(3); Node1 four = new Node1(4); Node1 five = new Node1(5); Node1 six = new Node1(6);

Find all nodes in a binary tree on a specific level (Interview Query)

左心房为你撑大大i 提交于 2019-11-30 23:53:14
I mean on a specific level, NOT up to that specific level. Could someone please check my modified BFS algorithm? (most of which is taken from Wikipedia) Queue levelorder(root, levelRequested){ int currentLevel = 0; q = empty queue q.enqueue(root) while not q.empty do{ if(currentLevel==levelRequested) return q; node := q.dequeue() visit(node) if(node.left!=null || node.right!=null){ currentLevel++; if node.left ≠ null q.enqueue(node.left) if node.right ≠ null q.enqueue(node.right) } } } I think a recursive solution would be much more concise: /* * node - node being visited * clevel - current

First Common Ancestor of a Binary Tree

假如想象 提交于 2019-11-30 23:50:32
If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1? According to the Wikipedia definition of the Lowest common ancestor I correct myself: The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with n nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants ( where we allow a node to be a descendant of itself ). So yes going by this definition the correct answer would be 6 . If this is an interview question would be good

Left balanced binary trees

亡梦爱人 提交于 2019-11-30 22:30:54
I am reading a book on data structures and it says that a left balanced binary tree is a tree in which the leaves only occupy the leftmost positions in the last level. This seemed a little vague to me. Does this mean that leaves are only on the left side of a root and are distributed throughout the whole level, or leave exists only on the left side of the entire tree. Exactly what constitute left balanced? I am not sure if my guess even covers any of the answer, so if anyone could help, it would be greatly appreciated :-). You can think of a left-balanced binary tree as a balanced binary tree

binary search tree impelementation and java

丶灬走出姿态 提交于 2019-11-30 20:40:34
问题 I am trying to implement BST algorithm using Cormen's pseudo code yet having issue. Here is my Code for Node: public class Node { Node left; Node right; int value; Node(int value){ this.value = value; this.left = null; this.right = null; } } and for the Bstree: public class Btree { Node root; Btree(){ this.root = null; } public static void inorderWalk(Node n){ if(n != null){ inorderWalk(n.left); System.out.print(n.value + " "); inorderWalk(n.right); } } public static Node getParent(Btree t,

counting number of leaf nodes in binary tree

落花浮王杯 提交于 2019-11-30 20:20:55
I want to count the no of leaf nodes: Note:Cannot use global/class level variable I implmeted following algo, and it works fine.But i want method signature to be countLeaves(Node node) I know that i can overload methds and call the 2 args method sig from 1 args, but dont want to do so.Can anyone suggest any other method? int countLeaves(Node node,int count){ if(node==null) return 0; if(node.left==null && node.right==null){ return 1+count; }else{ int lc = countLeaves(node.left, count); int total = countLeaves(node.right, lc); return total; } } int countLeaves(Node node){ if( node == null )

generate all structurally distinct full binary trees with n leaves

心不动则不痛 提交于 2019-11-30 20:20:17
问题 This is a homework, I have difficulties in thinking of it. Please give me some ideas on recursions and DP solutions. Thanks a lot generate and print all structurally distinct full binary trees with n leaves in dotted parentheses form, "full" means all internal (non-leaf) nodes have exactly two children. For example, there are 5 distinct full binary trees with 4 leaves each. 回答1: U can use recursion, on i-th step u consider i-th level of tree and u chose which nodes will be present on this

Java equivalent of C++ std::map?

社会主义新天地 提交于 2019-11-30 19:33:57
I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/search Each element is composed of a unique key and a mapped value Keys follow a strict weak ordering I'm looking for implementations with open source or design documents; I'll probably end up rolling my own support for primitive keys/values. This question's style is similar to: Java equivalent of std::deque , whose answer was "ArrayDeque from Primitive Collections for Java". ConcurrentSkipListMap is a

Can we use binary search tree to simulate heap operation?

做~自己de王妃 提交于 2019-11-30 19:02:44
I was wondering if we can use a binary search tree to simulate heap operations (insert, find minimum, delete minimum), i.e., use a BST for doing the same job? Are there any kind of benefits for doing so? Sure we can. but with a balanced BST. The minimum is the leftest element. The maximum is the rightest element. finding those elements is O(logn) each, and can be cached on each insert/delete, after the data structure was modified [note there is room for optimizations here, but this naive approach also doesn't contradict complexity requirement!] This way you get insert,delete: O(logn) , findMin