binary-tree

counting number of leaf nodes in binary tree

瘦欲@ 提交于 2019-11-30 16:57:26
问题 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

Binary Tree Insert Algorithm

北战南征 提交于 2019-11-30 14:58:20
问题 I recently finished implementing a Binary search tree for a project I was working on. It went well and I learned a lot. However, now I need to implement a regular Binary Tree... which for some reason has me stumped. I'm looking for a way to do my InsertNode function.. normally in a BST you just check if data < root then insert left and vice versa. However, In a normal Binary tree, it is just filled from left to right, one level at a time.. could anyone help me implement a function that just

Constructing a Binary tree in Java [closed]

时光怂恿深爱的人放手 提交于 2019-11-30 14:52:37
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I am constructing a binary tree. Let me know if this is a right way to do it. If not please tell me how to?? I could not find a proper link where constructing a general binary tree has been coded. Everywhere BST is coded. 3 / \ 1 4 / \ 2 5 This is the binary tree which i want to make.I should be able to do all

Average height of a binary search tree

纵然是瞬间 提交于 2019-11-30 14:39:38
How do you compute the average height of a binary search tree when adding 1000 random ints? What is the average height? You can compute the height of a binary tree using this recursive definition: height(empty) = 0 height(tree) = 1 + max(height(tree.left), height(tree.right)) One way to empirically measure the average height of such a tree is to repeatedly create an empty tree and add 1000 random items to it. Measure the height of each trial using the above function, and average them. I suspect your task is probably to find a formula for the average height of a binary tree. Andrew Shepherd

How to determine whether a binary tree is complete?

南楼画角 提交于 2019-11-30 14:21:54
A complete binary tree is defined as a binary tree in which every level, except possibly the deepest, is completely filled. At deepest level, all nodes must be as far left as possible. I'd think a simple recursive algorithm will be able to tell whether a given binary tree is complete, but I can't seem to figure it out. Similar to: height(t) = if (t==NULL) then 0 else 1+max(height(t.left),height(t.right)) You have: perfect(t) = if (t==NULL) then 0 else { let h=perfect(t.left) if (h != -1 && h==perfect(t.right)) then 1+h else -1 } Where perfect(t) returns -1 if the leaves aren't all at the same

deletion in a binary search tree

Deadly 提交于 2019-11-30 14:04:11
I have been given two binary search trees. For example, A and B. Next, I was asked to delete the tree B from the tree A. By deletion, I mean delete all the nodes present in B from A. Note: B is not necessarily a subtree of A. eg: A: 50 / \ 10 75 / / \ 1 60 90 B: 10 / \ 1 75 Resulting tree should be: 50 \ 60 \ 90 Two approaches came to my mind: A1: node* deleteTree(node* A, node* B) ; Take the root of tree B. Delete this node from tree A(by normal BSt deletion method). Next divide the problem into two parts - for the left subtree of B and the right subtree of B. For each of the subtree, recurse

Find whether a tree is a subtree of other

心不动则不痛 提交于 2019-11-30 12:20:19
问题 There are two binary trees T1 and T2 which store character data, duplicates allowed. How can I find whether T2 is a subtree of T1 ? . T1 has millions of nodes and T2 has hundreds of nodes. 回答1: Traverse T1. If the current node is equal to the root node of T2, traverse both of the trees (T2 and the current subtree of T1) at the same time. Compare the current node. If they are always equal, T2 is a subtree of T1. 回答2: I suggest an algorithm, that uses preprocessing: 1) Pre-process the T1 tree,

Constructing a Binary tree in Java [closed]

谁说我不能喝 提交于 2019-11-30 11:35:20
I am constructing a binary tree. Let me know if this is a right way to do it. If not please tell me how to?? I could not find a proper link where constructing a general binary tree has been coded. Everywhere BST is coded. 3 / \ 1 4 / \ 2 5 This is the binary tree which i want to make.I should be able to do all the tree traversals.Simple stuff. public class Binarytreenode { public Binarytreenode left; public Binarytreenode right; public int data; public Binarytreenode(int data) { this.data=data; } public void printNode() { System.out.println(data); } public static void main(String ar[]) {

Algorithm to print all paths with a given sum in a binary tree

折月煮酒 提交于 2019-11-30 11:00:58
问题 The following is an interview question. You are given a binary tree (not necessarily BST) in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree - it does not have to start at the root. Although I am able to find all paths in tree that start at the root have the given sum, I am not able to do so for paths not not starting at the root. 回答1: Well, this is a tree, not a graph. So, you can do something like

How to Serialize Binary Tree

偶尔善良 提交于 2019-11-30 10:59:35
问题 I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right child. The interviewer seemed more or less pleased, but I'm wondering what serialize means exactly? Does it specifically pertain to flattening the tree for writing to disk, or would serializing a tree also include just turning the tree into a linked