binary-tree

Convert sorted array to Binary Search Tree (Java) stack overflow exception

限于喜欢 提交于 2019-12-13 07:35:05
问题 This is a program for converting an array, where elements are sorted in ascending order, to a height balanced BST. I input five element, pass them to an array, sort the array and use methods. It produces this error: Exception in thread "main" java.lang.StackOverflowError at Solution.sortedArrayToBST(Node.java:26) How do I fix this error? import java.util.*; class Node { int val; Node left; Node right; Node(int x) { val = x; } } class Solution { public Node sortedArrayToBST(int[] num) { if

Deallocating binary-tree structure in C

穿精又带淫゛_ 提交于 2019-12-13 07:18:23
问题 I have a linked list, I guess a tree looks like this: -> grandma -> dad -> me -> sister -> niece -> brother -> uncle -> cousin and I have a struct as following struct Node{ Node *parent; Node *next; Node *child; } How would I free that linked list? My idea is to do a depth first search and deallocate each node? 回答1: Recursive depth-search (DFS) : You're right, it's a good way to dealocate binary-tree memory: remove(node): if node is null: return //else remove(left node) remove(right node)

Given a binary search tree and a number, find a path whose node's data added to be the given number.

余生颓废 提交于 2019-12-13 07:15:23
问题 Given a binary search tree and a number, find if there is a path from root to a leaf such that all numbers on the path added up to be the given number. I know how to do it by recursively. But, I prefer an iterative solution. If we iterate from root to a leaf each time, there will be overlap because some paths may have overlap. What if the tree is not binary search ? Thanks 回答1: Basically this problem can be solved using Dynamic Programming on tree to avoid those overlapping paths. The basic

Deep copying and swapping subtrees in java

♀尐吖头ヾ 提交于 2019-12-13 04:48:39
问题 thanks for taking the time to read this (sorry for the wall of text). Background Yes, this is a school project. No, I'm not asking you to do this for me. I've done all the work myself I've just been stumped by Java itself, not so much the problem. The project is about the basics of genetic programming. I randomly generate trees (which represent basic mathematical expressions), evaluate their fitness against data from the target function (x, y values), then manipulate the set by eliminating

How to create a linked list of nodes that are contained in the max-Depth of a Binary Tree using Java

ⅰ亾dé卋堺 提交于 2019-12-13 04:46:15
问题 I've already created the Binary Tree and Linked list classes, I'm just in need of an algorithm that prints ONLY the nodes of the largest path. The height and size of the Binary Tree are already stored in the root node, but my problem is traversing only the largest path while adding each node to my linked list. 回答1: I assume your binary tree nodes have a reference to their parent, is that right? Then you could use either a breadth-first search or a depth-first search and find root nodes where

Insertion function in a random binary tree

梦想与她 提交于 2019-12-13 04:00:21
问题 I'm having a problem with my insertion function in this binary tree in C++. The nodes are correctly inserted until I need to add again a node to the right or to the left. The function thinks I don't have any nodes to either the left or the right in the case being that I already inserted nodes in those places. Here is my code: void insert(string data) { srand(time(NULL)); int r; node *aux=head; node *n=new node(data); if (head==NULL) { head =n; return; } while (aux!=NULL) { r=rand()%100; if (r

finding a preorder of some digits with their levels in BST

眉间皱痕 提交于 2019-12-13 03:30:35
问题 I have a list that has e.g. 5 digits which each digit has its own level in BST : list--> [digit :6 level:1, digit :3 level:2, digit :5 level:3, digit :2 level:3, digit:1 level:4] how can I find its preorder which is { 6,3,2,1,5} ? Consider that I have 10000 digits in my list above. thanks 回答1: To traverse a BST in preorder you have to 1. Visit the root (i.e. your number) 2. Traverse the left subtree. 3. Traverse the right subtree. Each subtree is recursively traversed in the same way (root,

Right Threading a Binary Tree

喜夏-厌秋 提交于 2019-12-13 03:09:31
问题 I'm having a hell of a time trying to figure this one out. Everywhere I look, I seem to be only running into explanations on how to actually traverse through the list non-recursively (the part I actually understand). Can anyone out there hammer in how exactly I can go through the list initially and find the actual predecessor/successor nodes so I can flag them in the node class? I need to be able to create a simple Binary Search Tree and go through the list and reroute the null links to the

Tree delete node

落爺英雄遲暮 提交于 2019-12-13 02:57:07
问题 I am trying to finish the delete function. Here is the pseudo code, notice the end: I don't know if the pseudo code is wrong though. Here is how I interpreted it: Node* minNode = Minimum(toDelete->right); int tmp = 0; tmp = minNode->val; // delete(&tmp); free(minNode); minNode=NULL; toDelete->val=tmp; except once it deletes it, it starts filling a trillion zeroes when printing. Is what I am doing making sense? The rest of the code I have is right, or I think so anyway. It only screws up in

Get postorder BT traversal from given inorder and preorder traversal

浪尽此生 提交于 2019-12-13 00:43:30
问题 Can anyone help me out to get postorder traversal as output from gven two traversals: In-order :A, B, C, D, E, F, G, H, J, K, L, M, P, Q, N. Pre-order : C, D, E, B, G, H, F, K, L, P, Q, M, N, J, A. It will be more helpfull if the answere is graphical. 回答1: Non-Recursive Algorithm ;How to Calculate change Between inorder and preorder ؟؟ #include<stdio.h> #include<conio.h> #include <iostream> using namespace std; int find(int *p,int s,int f) { int k=0; if (f==0) return -1; for(;k<s;k++) if(*(p