binary-tree

Variations of folds on Haskell Trees

偶尔善良 提交于 2019-12-10 17:33:32
问题 Given a tree defined to be: data Tree a = Leaf | Node (Tree a) a (Tree a) deriving (Eq, Show) I want to use the function: foldTree :: (b -> a -> b -> b) -> b -> Tree a -> b foldTree _ b Leaf = b foldTree f b (Node lt x rt) = f (foldTree f b lt) x (foldTree f b rt) To be able to create equivalents of the normal foldr and foldl as: foldTreeR :: (a -> b -> b) -> b -> Tree a -> b foldTreeL :: (b -> a -> b) -> b -> Tree a -> b I thought these would be fairly straightforward since their definitions

Find first null in binary tree with limited memory

蹲街弑〆低调 提交于 2019-12-10 17:29:45
问题 I have a binary tree where each node can have a value. I want to find the node in the tree that has value null and is closest to the root. If there are two nodes with the same distance from the root, either will do. I need to minimize the number of read accesses to the binary tree. Assume that working memory is limited to just k nodes. DFS to depth k is exhaustive but will not find the closest node unless I run through the whole tree first. BFS will find the closest, but it might fail because

Recursive functional explanation in binary tree

人走茶凉 提交于 2019-12-10 15:32:22
问题 I was going through the tutorial of binary tree . And I am slightly stuck in use of recursive function . say for example I need to count no of nodes in a tree int countNodes( TreeNode *root ) { // Count the nodes in the binary tree to which // root points, and return the answer. if ( root == NULL ) return 0; // The tree is empty. It contains no nodes. else { int count = 1; // Start by counting the root. count += countNodes(root->left); // Add the number of nodes // in the left subtree. count

Returning recursive ternary freaks out

偶尔善良 提交于 2019-12-10 13:44:08
问题 assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return 0; } else { return 1 + max(findHeight(n->left), findHeight(n->right)); } } Pretty standard recursive treeHeight function for a given binary search tree binaryTree . Now, I was helping a friend (he's taking an algorithms course), and I ran into some weird issue with this function that I couldn't 100% explain to him. With max being defined as max(a,b) ((a)>(b)?(a):(b)) (which happens to be the max

represent binary search trees in python

夙愿已清 提交于 2019-12-10 13:36:44
问题 how do i represent binary search trees in python? 回答1: class Node(object): def __init__(self, payload): self.payload = payload self.left = self.right = 0 # this concludes the "how to represent" asked in the question. Once you # represent a BST tree like this, you can of course add a variety of # methods to modify it, "walk" over it, and so forth, such as: def insert(self, othernode): "Insert Node `othernode` under Node `self`." if self.payload <= othernode.payload: if self.left: self.left

Diameter of Binary Tree

我怕爱的太早我们不能终老 提交于 2019-12-10 12:25:25
问题 "The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between any two nodes in the tree". Will the C++ function below works for all cases to find the diameter of a binary tree? Called as s=0 , and root = root of the binary tree int helper(node *root,int &s){ int l = 0, r = 0; if(root == NULL){ return 0; } l= helper(root->left,s); //height of left subtree. r= helper(root->right,s); //height of right subtree. int mx_single=max(l,r)+1; int mx_top=max(mx

How to insert a node into a complete binary tree in Java?

左心房为你撑大大i 提交于 2019-12-10 11:49:46
问题 As we all know, when inserting into a complete binary tree we have to fill all the children for all the leafs from left to right. I have the following method that inserts a node into a complete binary tree. //fields private T item; private int size; private CBTree<T> left, right; //add method public void add(T item) { if(left == null) { left = new CBTree<T>(item); size += left.size; } else if(right == null) { right = new CBTree<T>(item); size += right.size; } else if(!((left.left != null) &&

BST : inOrder successor and predecessor

最后都变了- 提交于 2019-12-10 11:48:08
问题 Do they look correct? I implemented them and was looking to review them Node predecessor(Node node) { if ((node.left == null) && (node.right==null)) { return node; } if (node.right != null) { return predecessor(node.right); } if (node.left != null) { return predecessor(node.left); } } Node successor(Node node) { if ((node.left == null) && (node.right==null)) { return node; } if (node.left != null) { return successor(node.left); } if (node.right != null) { return successor(node.right); } } 回答1

Printing Successor and Predecessor in a BST

*爱你&永不变心* 提交于 2019-12-10 11:39:58
问题 My problem is as follows: I have a binary search tree with keys: a1<a2<...<an , the problem is to print all the (a_i, a_i+1) pairs in the tree (where i={1,2,3,...}) using a recursive algorithm in O(n) time without any global variable and using O(1) extra space. An example: Let the keys be: 1,2, ..., 5 Pairs that will be printed: (1,2) (2,3) (3, 4) (4, 5) So you can't do inorder traversal in the tree and find the successor/predecessor for each node. Because that would take O(nh) time and if

Can a red node have just 1 black child in a red-black tree?

痞子三分冷 提交于 2019-12-10 11:31:51
问题 The rules for a Red-Black Tree: Every node is either red or black. The root is black. Every leaf (NIL) is black. If a node is red, then both its children are black. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. Rule 4 mentions that red nodes need both black childs but what if there is just one child to begin with? Is there an argument to prove or disprove this? 回答1: No,a red node cannot have one child,consider the following cases:-