binary-tree

Finding size of max independent set in binary tree - why faulty “solution” doesn't work?

谁都会走 提交于 2019-12-04 04:59:39
问题 Here is a link to a similar question with a good answer: Java Algorithm for finding the largest set of independent nodes in a binary tree. I came up with a different answer, but my professor says it won't work and I'd like to know why (he doesn't answer email). The question: Given an array A with n integers, its indexes start with 0 (i.e, A[0] , A[1] , …, A[n-1] ). We can interpret A as a binary tree in which the two children of A[i] are A[2i+1] and A[2i+2] , and the value of each element is

How to finding first common ancestor of a node in a binary tree?

微笑、不失礼 提交于 2019-12-04 04:41:09
Following is my algorithm to find first common ancestor. But I don’t know how to calculate it time complexity, can anyone help? public Tree commonAncestor(Tree root, Tree p, Tree q) { if (covers(root.left, p) && covers(root.left, q)) return commonAncestor(root.left, p, q); if (covers(root.right, p) && covers(root.right, q)) return commonAncestor(root.right, p, q); return root; } private boolean covers(Tree root, Tree p) { /* is p a child of root? */ if (root == null) return false; if (root == p) return true; return covers(root.left, p) || covers(root.right, p); } Ok, so let's start by

Is the root node an internal node?

妖精的绣舞 提交于 2019-12-04 01:59:54
So I've looked around the web and a couple of questions here in stackoverflow here are the definition: Generally, an internal node is any node that is not a leaf (a node with no children) Non-leaf/Non-terminal/Internal node – has at least one child or descendant node with degree not equal to 0 As far as i understand it, it is a node which is not a leaf. I was about to conclude that the root is also an internal node but there seems to be some ambiguity on its definition as seen here: What is an "internal node" in a binary search tree? As the wonderful picture shows, internal nodes are nodes

Create a Complete Binary Tree using Linked Lists w/o comparing node values

安稳与你 提交于 2019-12-03 22:43:52
问题 I am trying to create a complete binary tree using a linked list, instead of arraylist, without comparing node values. What I mean is on inserting a new value, I do not wish to compare if the value is less, greater or equal than the root's value, so as to add it either to the left link or to the right link, but still be able to create a complete binary tree. Do you think it's possible? If yes, do you have any idea or can you point me to something I can use/read to do it? EDIT: Here's an

Binary Tree insertion (in order sorted)

笑着哭i 提交于 2019-12-03 22:02:00
I've scoured the internet for help on this problem but I need help. This isn't exactly an ordinary insertion problem for a binary tree, since we don't get to work directly with the tree structure itself. My prof wrote that himself and has given us functions we can use to write functions pertaining to binary trees. As such, I can't use nodes and pointers and things. Also this is in C++. Anyway, so here is the description of the recursive function I have to write ( along with my starting attempt at working with the problem). Notice it returns a new tree entirely, it does not actually add

deletion in a binary search tree

那年仲夏 提交于 2019-12-03 20:08:52
问题 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

Traversing through all nodes of a binary tree in Java

北城余情 提交于 2019-12-03 19:06:56
问题 Let's say I have a simple binary tree node class, like so: public class BinaryTreeNode { public String identifier = ""; public BinaryTreeNode parent = null; public BinaryTreeNode left = null; public BinaryTreeNode right = null; public BinaryTreeNode(BinaryTreeNode parent, String identifier) { this.parent = parent; //passing null makes this the root node this.identifier = identifier; } public boolean IsRoot() { return parent == null; } } How would I add a method which is able to recursively

Binary Tree implementation C++

丶灬走出姿态 提交于 2019-12-03 17:28:38
Binary Tree insertion: #include "stdafx.h" #include <iostream> using namespace std; struct TreeNode { int value; TreeNode* left; TreeNode* right; }; struct TreeType { TreeNode* root; void insert(TreeNode* tree, int item); void insertItem(int value) { insert(root, value); } }; void TreeType::insert(TreeNode* tree, int number) { if (tree == NULL) { tree = new TreeNode; tree->left = NULL; tree->right = NULL; tree->value = number; cout << "DONE"; } else if (number < tree->value) { insert(tree->left, number); } else { insert(tree->right, number); } } int main() { TreeType* MyTree = new TreeType;

How to construct a binary tree from just the level order traversal string

你说的曾经没有我的故事 提交于 2019-12-03 16:36:01
Consider a binary tree with the following properties: An internal node (non-leaf node) has a value 1 if it has two children. A leaf node has a value 0 since it has no children. A level order traversal on the tree would generate a string of 1s and 0s (by printing the weird value at each node as they are visited). Now given this string construct the binary tree and perform a post order traversal on the tree. The post order string should be the output of the program. For example: Input String is 111001000 . Create a binary tree from this. Then perform the post order traversal on the tree which

Binary search tree traversal that compares two pointers for equality

荒凉一梦 提交于 2019-12-03 15:27:09
I'm reading the Cormen algorithms book (binary search tree chapter) and it says that there are two ways to traverse the tree without recursion: using stack and a more complicated but elegant solution that uses no stack but assumes that two pointers can be tested for equality I've implemented the first option (using stack), but don't know how to implement the latter. This is not a homework, just reading to educate myself. Any clues as to how to implement the second one in C#? Sure thing. You didn't say what kind of traversal you wanted, but here's the pseudocode for an in-order traversal. t =