binary-tree

A complete Binary Search Tree with level order insert in Java

大憨熊 提交于 2019-12-20 06:20:47
问题 We got an assignment where we need to code: A Binary Search Tree The Tree has to be complete , not perfect (which means all nodes which are not on the lowest level or second lowest level should have 2 children, while the nodes on the lowest level should be as far left as possible) We need to insert to the tree in level order So if I have an Array with elements {0, 1, 2, 3, 4, 5, 6, 7} the root should be 4 , with 2, 1, 3, 0 on the left side, and 6, 5, 7 on the right side. The level order

Intersection of 2 binary trees throws Stack Overflow error

假如想象 提交于 2019-12-20 05:56:06
问题 I am trying to intersect two binary trees, and create a new binary tree with the nodes that are the same, but the following creates a stackOverflow error. Can anyone help me? private OrderedSet<E> resultIntersect = new OrderedSet<E>(); public OrderedSet<E> intersection(OrderedSet<E> other) { OrderedSet<E> result = new OrderedSet<E>(); if (other.root == null || root == null) return result; else if (height() == 0 && other.height() == 0 && other.root.data.equals(root.data)) { result.insert(root

Converting a Binary Tree into an Array (and later save) in C

白昼怎懂夜的黑 提交于 2019-12-20 04:37:01
问题 So, I'm doing this customer application where you can create/Modify/Search/List Customers. Later on this expands to linking customers to products with an order and so on, but my focus right now is just on customers. I have created a binary tree and all these functions work, however I need a way to store created customers for another time. I figured that in some way I would have to transfer all the customers (found in each node) into an array, and then fwrite that array into a file "customer

Python Checking paths to leaf in binary tree python giving data in the leaf

有些话、适合烂在心里 提交于 2019-12-20 03:55:09
问题 Lets say i have this tree: cough Yes / \ No sneezing sneezing Yes / \ No Yes / \ No fever fever fever fever Yes / \ No Yes/ \No Yes / \ No Yes/ \No dead cold influenza cold dead influenza cold healthy And i want the paths to the illness "influenza" What the output should be is like this: [[True,False,True],[False,True,False]] If you go to right of the root it return True ( Yes ) , if you go to Left its False( No) This is the code I have been trying to do for this function but im doing

how to find the horizontal distance between two nodes at the same level in a binary tree?

江枫思渺然 提交于 2019-12-20 01:58:05
问题 Given a binary tree: Binary tree of height 3 I want to find the horizontal distance between two nodes at the same level also counting the nodes that are not there in between while not counting the nodes themselves , Say in f / \ g h / \ / \ a d in between the nodes a and d there is a horizontal distance of 2. Edit: Please see distance between a to d is calculated on the same level not including the parent or child nodes of either a or d but only the missing nodes at the same level. so

Issue checking if binary tree is also binary search tree

走远了吗. 提交于 2019-12-19 19:46:26
问题 I'm trying to solve this problem but I'm having some troubles: In a binary search tree (BST): The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node: class Node { int data; Node left; Node right; } Determine if the binary tree is also a binary search tree I have this code: boolean check(Node root) { //node doesn't have any children if

Issue checking if binary tree is also binary search tree

大憨熊 提交于 2019-12-19 19:46:03
问题 I'm trying to solve this problem but I'm having some troubles: In a binary search tree (BST): The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node: class Node { int data; Node left; Node right; } Determine if the binary tree is also a binary search tree I have this code: boolean check(Node root) { //node doesn't have any children if

How to add elements in Binary search tree iteratively?

℡╲_俬逩灬. 提交于 2019-12-19 10:56:29
问题 public void Insert(int value) { if (value < Data) { if (LeftNode == null) { LeftNode = new TreeNode(value); } else { LeftNode.Insert(value); } } else if (value > Data) { if (RightNode == null) { RightNode = new TreeNode(value); } else { RightNode.Insert(value); } } } I wrote method to add element in BST recursively, It checks for value to add less than or greater than and add it in its proper place, but I want to know how iterative method works? I need iterative add method for my BST. 回答1:

Big O of finding out if a binary tree is balanced (From CTCI Book)

喜欢而已 提交于 2019-12-19 09:48:33
问题 In Cracking the Coding Interview 6th Edition there's a question (4.4) where you're suppose to find out if a binary tree is balanced, where balanced in this case means if any side is deeper than the other by more than 1. I solved this recursively like this: def isBalanced(root): return abs(getDepth(root.left) - getDepth(root.right)) > 1 def getDepth(node): if node is None: return 0 return 1 + max([getDepth(node.left), getDepth(node.right)]) So to walk through it. It recursively check each side

Distributing AND over OR in a binary tree (Conjunctive Normal Form)

人走茶凉 提交于 2019-12-19 09:03:26
问题 I'm trying to convert a binary tree e.g. OR (Implementation of Operator - a specialisation of TreeNode... see below) |-A (Implementation of TreeNode... see below) |-OR |-B |-AND (Implementation of Operator - a specialisation of TreeNode... see below) |-C |-OR |-D |-E into it's equivalent Conjunctive Normal Form (CND) representation. I believe that because I'm only using the logical OR + AND operators that the only step I'd have to perform is the distribution of AND over OR. This would produce