binary-tree

Height of binary search tree iteratively

╄→尐↘猪︶ㄣ 提交于 2019-12-12 18:17:19
问题 I was trying out an iterative method to find the height/depth of a binary search tree. Basically, I tried using Breadth First Search to calculate the depth, by using a Queue to store the tree nodes and using just an integer to hold the current depth of the tree. Each node in the tree is queued, and it is checked for child nodes. If child nodes are present, then the depth variable is incremented. Here is the code: public void calcDepthIterative() { Queue<TreeNode> nodeQ = new LinkedList

How do I remove the leaves of a binary tree?

好久不见. 提交于 2019-12-12 15:41:24
问题 I'm trying to remove all of the leaves. I know that leaves have no children, this is what I have so far. public void removeLeaves(BinaryTree n){ if (n.left == null && n.right == null){ n = null; } if (n.left != null) removeLeaves(n.left); if (n.right != null) removeLeaves(n.right); } 回答1: It's much easier if you break this down like this: public void removeLeaves(BinaryTree n){ if (n.left != null) { if (n.left.isLeaf()) { n.removeLeftChild(); } else { removeLeaves(n.left); } } // repeat for

Prolog binary search tree test - unwanted parents' parent node comparison

白昼怎懂夜的黑 提交于 2019-12-12 13:28:28
问题 I'm a Prolog rookie, please keep that in mind. I try to write a predicate to determine if some given term is a binary search tree. I figured out this code: is_btree(nil). is_btree(node(N,L,R)) :- number(N), is_btree(L), is_btree(R), small(N, R), big(N, L). small(N, nil). small(N, node(M,L,R)) :- N < M, small(N, L), small(N, R). big(N, nil). big(N, node(M,L,R)) :- N > M, big(N, L), big(N, R). It works quite fine until I test a graph that has a node on the right side which passes the condition

Making a very basic binary tree in Scala

拥有回忆 提交于 2019-12-12 12:40:04
问题 I am trying to make a very simple binary tree in Scala for data storage and traversal. Right now I have: trait Tree case class Node(left: Tree, value: String, right: Tree) extends Tree My questions: How can I also include a pointer to a parent? Can I have left and right point to null in any way? Or the parent pointer for the root node? How can I actually traverse the tree? Is it easy to update the values of a node? 回答1: Not with immutable case classes. It is a circular dependency: you need

Generating all possible topologies in a full binary tree having n nodes

爱⌒轻易说出口 提交于 2019-12-12 10:12:19
问题 I want to create all possible topologies of a full binary tree which must have exactly n+1 leaf nodes and n internal nodes. I want to create it using recursion and tree must be simple binary tree not a binary search tree or BST. Kindly suggest algorithm to achieve this task. example: with 4 leaf nodes and 3 internal nodes. N N N / \ / \ /\ N N N N N N /\ /\ /\ /\ N N N N N N N N / \ /\ N N N N PS: There is a simlar thread .It will be helpful If anybody can elaborate the tree generation

Generic binary tree node destructor issue

梦想与她 提交于 2019-12-12 10:01:53
问题 I've been working on an assignment and now I'm stuck with buggy destructors. I have to create a generic binary tree with all the usual member functions and some special operators. There's also a restriction: everything must work iteratively so no nasty recursive hacks this time. There is obviously something very wrong with the destructor of BinTreeNode class because if I delete the node like this: BinTreeNode<int> * node = new BinTreeNode<int>(); delete node; I can still access its data: node

Convert recursive binary tree traversal to iterative

风格不统一 提交于 2019-12-12 09:52:54
问题 I was asked to write the iterative version, but I wrote the recursive version i.e. void inorderTraverse(BinaryTree root) { if(root==NULL) printf("%d",root->id); else { inorderTraverse(root->left); printf("%d",root->id); inorderTraverse(root->right); } } I'm not looking for the code, I want to understand how this can be done. Had it been just the last recursive call, I would have done void inorderTraverse(BinaryTree root) { while(root!=NULL) { printf("%d",root->id); root=root->right; } } But

How to traverse a binary tree in O(n) time without extra memory

感情迁移 提交于 2019-12-12 08:10:27
问题 Given a binary tree with an integer, Left & Right pointers, how can one traverse the tree in O(n) time and O(1) extra memory (no stack/queue/recursion)? This guy gave a solution which is not O(n) total time that encoded the current path as an integer (and thus works on for trees of limited depth). I am looking for the classical solution (SPOILER) that encoded the parent of each node in the children. 回答1: Any good algorithm book will have this algorithm, look e.g. in Knuth (TAOCP I.2.3.1

Algorithm to Render a Horizontal Binary-ish Tree in Text/ASCII form

做~自己de王妃 提交于 2019-12-12 07:51:41
问题 It's a pretty normal binary tree, except for the fact that one of the nodes may be empty. I'd like to find a way to output it in a horizontal way (that is, the root node is on the left and expands to the right). I've had some experience expanding trees vertically (root node at the top, expanding downwards), but I'm not sure where to start, in this case. Preferably, it would follow these couple of rules: If a node has only one child, it can be skipped as redundant (an "end node", with no

Find lowest common ancestor in Binary Search Tree

让人想犯罪 __ 提交于 2019-12-12 07:37:47
问题 I've got the following code to find the lowest common ancestor (the lowest node that has both a and b as descendants): public static Node LCA(Node root, Node a, Node b) { if (root == null) return null; if (root.IData == a.IData || root.IData == b.IData) return root; if (root.RightChild != null && (root.RightChild.IData == a.IData || root.RightChild.IData == b.IData)) return root; if (root.LeftChild != null && (root.LeftChild.IData == a.IData || root.LeftChild.IData == b.IData)) return root;