inorder

Inorder Tree Traversal algorithm for a Binary Search Tree using Stack

僤鯓⒐⒋嵵緔 提交于 2019-12-11 08:30:20
问题 My inputs results 24, 4, 2, 3, 9, 10, 32 , and I am getting following result 2, 3, 4, 24 . I am using a stack . When I have manually checked this program the node is not going through else if at 4 on stack, even if has right sub tree. public void inorderNonRcursive(Node root){ Stack s = new Stack(); Node node = root; Node k; int c=0; if(node != null) { s.push(node); } while(!s.stack.isEmpty()) { //node=(Node) s.stack.get(s.stack.size()-1); System.out.println("first condition" + (node.getleft(

How to return a list of values through recursion in java?

血红的双手。 提交于 2019-12-11 05:21:23
问题 Returning a single value through recursion works just fine. But what if I want to return a list of values that recursion goes through each call. Here's my code. public void inOrder(Node focusNode) { /* ArrayList<Integer> tempList = new ArrayList<Integer>(); */ if ( focusNode != null) { inOrder(focusNode.getLeftNode()); System.out.println(focusNode); /* tempList.add(focusNode.getElement()); */ inOrder(focusNode.getRightNode()); } /* int[] elems = new int[tempList.toArray().length]; int i = 0;

Reconstructing binary tree from inorder and preorder traversals

末鹿安然 提交于 2019-12-09 19:24:18
问题 I have written the following code for constructing a tree from its inorder and preorder traversals. It looks correct to me but the final tree it results in does not have the same inorder output as the one it was built from. Can anyone help me find the flaw in this function? public btree makeTree(int[] preorder, int[] inorder, int left,int right) { if(left > right) return null; if(preIndex >= preorder.length) return null; btree tree = new btree(preorder[preIndex]); preIndex++; int i=0; for(i

InOrder Traversal in Python

為{幸葍}努か 提交于 2019-12-08 05:06:32
The problem I am tackle with is to find the first occurrence node in its inorder traversal in a BST. The code I have is given below def Inorder_search_recursive(node,key): if not node: return None InOrder_search_recursive(node.lChild) if node.value==key: return node InOrder_search_recursive(node.rChild) This code always return None, what's wrong with it. I think I've return node when I find a node with value k. Why cannot python pass this node???Thanks in advance When you call yourself recursively, like this: InOrder_search_recursive(node.lChild) That's just a normal function call, like any

Understanding stack unwinding in recursion (tree traversal)

不羁的心 提交于 2019-12-08 01:52:16
问题 I am writing a program to traverse a binary search tree.Here's my code: Main.java public class Main { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); binaryTree.add(50); binaryTree.add(40); binaryTree.add(39); binaryTree.add(42); binaryTree.add(41); binaryTree.add(43); binaryTree.add(55); binaryTree.add(65); binaryTree.add(60); binaryTree.inOrderTraversal(binaryTree.root); } } Node.java public class Node { int data; Node left; Node right; Node parent; public

Understanding stack unwinding in recursion (tree traversal)

本秂侑毒 提交于 2019-12-06 12:07:42
I am writing a program to traverse a binary search tree.Here's my code: Main.java public class Main { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); binaryTree.add(50); binaryTree.add(40); binaryTree.add(39); binaryTree.add(42); binaryTree.add(41); binaryTree.add(43); binaryTree.add(55); binaryTree.add(65); binaryTree.add(60); binaryTree.inOrderTraversal(binaryTree.root); } } Node.java public class Node { int data; Node left; Node right; Node parent; public Node(int d) { data = d; left = null; right = null; } } BinaryTree.java public class BinaryTree { Node

How do I infer the usage of parentheses when translating an expression tree?

喜欢而已 提交于 2019-12-04 19:46:51
问题 I am working on translating an expression tree to a format that resembles infix notation; I am not evaluating the tree or executing its operations. The tree contains both logical and relational operations, and I would like to emit parentheses in an intelligent manner during the translation. To illustrate, consider the following contrived expression: a < x & (a < y | a == c) & a != d If I walk the expression tree produced by this expression in-order, then I will print out the following

Reconstructing binary tree from inorder and preorder traversals

江枫思渺然 提交于 2019-12-04 14:36:28
I have written the following code for constructing a tree from its inorder and preorder traversals. It looks correct to me but the final tree it results in does not have the same inorder output as the one it was built from. Can anyone help me find the flaw in this function? public btree makeTree(int[] preorder, int[] inorder, int left,int right) { if(left > right) return null; if(preIndex >= preorder.length) return null; btree tree = new btree(preorder[preIndex]); preIndex++; int i=0; for(i=left; i<= right;i++) { if(inorder[i]==tree.value) break; } tree.left = makeTree(preorder, inorder,left,

How do I infer the usage of parentheses when translating an expression tree?

泄露秘密 提交于 2019-12-03 12:53:52
I am working on translating an expression tree to a format that resembles infix notation; I am not evaluating the tree or executing its operations. The tree contains both logical and relational operations, and I would like to emit parentheses in an intelligent manner during the translation. To illustrate, consider the following contrived expression: a < x & (a < y | a == c) & a != d If I walk the expression tree produced by this expression in-order, then I will print out the following expression, which is incorrect. a < x & a < y | a == c & a != d // equivalent to (a < x & a < y) | (a == c & a

Rebuild a binary tree from preorder and inorder lists

穿精又带淫゛_ 提交于 2019-12-02 11:17:47
问题 Hi I'm trying to rebuild a binary tree, I almost got it, except it throws me an error and I don't know why buildTree :: (Ord a, Eq a) => [a] -> [a] -> Tree a buildTree [] [] = Empty buildTree preOrd inOrd = Node root left right where root = head preOrd left = buildTree leftPreOrd leftInOrd right = buildTree rigthPreOrd leftInOrd Just rootInd = elemIndex root inOrd leftPreOrd = tail (take (rootInd + 1) preOrd) rigthPreOrd = tail (drop rootInd preOrd) leftInOrd = take rootInd inOrd rightInord =