binary-tree

A complete Binary Search Tree with level order insert in Java

喜你入骨 提交于 2019-12-02 09:55:49
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 insert would be: 4, 2, 6, 1, 3, 5, 7, 0 Just taking the middle of the Array and put it as root doesn't work.

Intersection of 2 binary trees throws Stack Overflow error

瘦欲@ 提交于 2019-12-02 08:54:05
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.data); return result; } else { intersection(other, root, other.root); result = resultIntersect; }

Getting all nodes at a level in full binary tree, array format

坚强是说给别人听的谎言 提交于 2019-12-02 08:25:12
I need to get all the nodes at a certain level in a full binary tree from either the left or right subtree. I currently retrieve the binary tree from the DB as an array, for example: [1,2,3,4,5,6,7] represents a tree like this: 1 / \ / \ 2 3 / \ / \ / \ / \ 4 5 6 7 So what I need to do is basically grab a level of the tree and return it as an array. Something like level(3,"left") -> [4,5] or level(2, "right") -> [3] . I was thinking about creating a BinaryTree object doing it recursively, but I can't think of a way to keep track of the level within the call without having to tag every node

How to find height of BST iteratively?

夙愿已清 提交于 2019-12-02 07:51:26
public void HeightIterative() { int counter = 0; int counter2 = 0; TreeNode current=root; if(current != null) { while(current.LeftNode!=null) { counter++; current = current.LeftNode; } while(current.RightNode!=null) { counter2++; current = current.RightNode; } } int res = 1+Math.Max(counter, counter2); Console.WriteLine("The Height Of Tree Is: "+res); } I wrote iterative method, to calculate height of tree. but in some cases its not working properly. As in case: 10 1 2 3 4 5 18 17 16 15 14 13 what's the problem. according to this sequence height of tree is 6 where as my code is showing 5. You

Rebuild a binary tree from preorder and inorder lists

亡梦爱人 提交于 2019-12-02 06:44:25
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 = drop (rootInd + 1) inOrd When I call it using buildTree [10,5,2,6,14,12,15] [2,5,6,10,12,14,15] it

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

感情迁移 提交于 2019-12-02 06:34:39
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.dat". have spent a lot of hours on it. here is some code snippets to help better understand what

Can this implementation of in-order traversal of a binary tree be improved?

给你一囗甜甜゛ 提交于 2019-12-02 05:37:28
问题 I wrote a straightforward in-order-traversal function ( toList1 ) for a binary tree. However, I worry about its complexity (memory / time). Is there a better way to implement it? data Tree a = Empty | Node a (Tree a) (Tree a) toList1 :: (Tree a) -> [a] toList1 Empty = [] toList1 (Node x lx rx) = (toList lx) ++ [x] ++ (toList rx) 回答1: Haskell's append ++ performs linearly in the length of its left argument, which means that you may get quadratic performance if the tree leans left . One

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

别来无恙 提交于 2019-12-02 05:34:31
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 something wrong it returns not as i want.. def paths_to_illness(self, illness): head=self.__root new_list=[]

Complete binary tree definitions

∥☆過路亽.° 提交于 2019-12-02 04:40:11
I have some questions on binary trees: Wikipedia states that a binary tree is complete when "A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible." What does the last "as far left as possible" passage mean? A well-formed binary tree is said to be "height-balanced" if (1) it is empty, or (2) its left and right children are height-balanced and the height of the left tree is within 1 of the height of the right tree, taken from How to determine if binary tree is balanced? , is this correct or there's

Constructing full binary tree given only postorder?

╄→гoц情女王★ 提交于 2019-12-02 03:01:31
问题 I'm trying to construct a full binary tree (full meaning that every non leaf node has two leaf nodes connecting to it, i.e. node->right and node->left are != NULL ) given only the postorder traversal of the tree. In addition, I am given whether or not the node in the postorder traversal is a leaf node or not. The given postorder traversal looks like this: 2(1.000000e+00) 3(1.000000e+00) (1.000000e+00 1.000000e+00) 1(1.000000e+00) (1.000000e+00 2.000000e+00) for example, where a line of the