binary-tree

PHP Binary Tree Recursion Algorithm

岁酱吖の 提交于 2019-12-17 18:44:25
问题 I want to create a PHP recursive program using a Binary Tree and Recursion. I want to print the binary tree level by level using recursion. I want to recurse through the tree, push the node into a hashmap that has the level as the reference point. Here's what I have so far: $binary_tree = array(array(1 => array(2 => array(4,5),4=>array(5,6)))); 1 | ------------------ | | 2 4 | | ---------- ---------- | | | | 4 5 5 6 And the end result should look like this: $data[0] = array(1); $data[1] =

Write a non-recursive traversal of a Binary Search Tree using constant space and O(n) run time

别等时光非礼了梦想. 提交于 2019-12-17 17:24:48
问题 This is not homework, this is an interview question. The catch here is that the algorithm should be constant space. I'm pretty clueless on how to do this without a stack, I'd post what I've written using a stack, but it's not relevant anyway. Here's what I've tried: I attempted to do a pre-order traversal and I got to the left-most node, but I'm stuck there. I don't know how to "recurse" back up without a stack/parent pointer. Any help would be appreciated. (I'm tagging it as Java since that

Red-Black Trees

拜拜、爱过 提交于 2019-12-17 17:24:44
问题 I've seen binary trees and binary searching mentioned in several books I've read lately, but as I'm still at the beginning of my studies in Computer Science, I've yet to take a class that's really dealt with algorithms and data structures in a serious way. I've checked around the typical sources (Wikipedia, Google) and most descriptions of the usefulness and implementation of (in particular) Red-Black trees have come off as dense and difficult to understand. I'm sure for someone with the

BinaryTree implementation in java

一笑奈何 提交于 2019-12-17 16:33:23
问题 I have this code for BinaryTree creation and traversal class Node { Integer data; Node left; Node right; Node() { data = null; left = null; right = null; } } class BinaryTree { Node head; Scanner input = new Scanner(System.in); BinaryTree() { head = null; } public void createNode(Node temp, Integer value) { Node newnode= new Node(); value = getData(); newnode.data = value; temp = newnode; if(head==null) { head = temp; } System.out.println("If left child exits for ("+value+") enter y else n");

Binary Trees vs. Linked Lists vs. Hash Tables

泪湿孤枕 提交于 2019-12-17 15:04:13
问题 I'm building a symbol table for a project I'm working on. I was wondering what peoples opinions are on the advantages and disadvantages of the various methods available for storing and creating a symbol table. I've done a fair bit of searching and the most commonly recommended are binary trees or linked lists or hash tables. What are the advantages and or disadvantages of all of the above? (working in c++) 回答1: Your use case is presumably going to be "insert the data once (e.g., application

How to fix remove in RedBlackTree implementation?

烈酒焚心 提交于 2019-12-17 07:50:52
问题 Here is the implementation of RedBlackTree I am using (from Mark Allen Weiss, Data Structures public class RedBlackTree<AnyKey extends Comparable<? super AnyKey>, AnyValue extends Comparable<? super AnyValue>> implements MyTreeMap<AnyKey, AnyValue>{ private static final int BLACK = 1; private static final int RED = 0; // The psuedo(bogus) root, has a key value of negative infinity and a right link to the real root. private RedBlackNode<AnyKey, AnyValue> header; // Used in place of a null link

What is the fastest way to change a key of an element inside std::map

懵懂的女人 提交于 2019-12-17 06:27:31
问题 I understand the reasons why one can't just do this (rebalancing and stuff): iterator i = m.find(33); if (i != m.end()) i->first = 22; But so far the only way (I know about) to change the key is to remove the node from the tree alltogether and then insert the value back with a different key: iterator i = m.find(33); if (i != m.end()) { value = i->second; m.erase(i); m[22] = value; } This seems rather inefficient to me for more reasons: traverses the tree three times (+ balance) instead of

How to find the lowest common ancestor of two nodes in any binary tree?

浪尽此生 提交于 2019-12-17 02:30:48
问题 The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as - struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort - Consider this binary tree : The inorder traversal yields - 8, 4, 9, 2, 5, 1, 6, 3, 7 And the postorder traversal yields - 8, 9, 4, 5, 2, 6, 7, 3, 1 So for instance, if we want to find the common ancestor of nodes 8 and 5, then we make a list of

Find kth smallest element in a binary search tree in Optimum way

限于喜欢 提交于 2019-12-17 02:03:34
问题 I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently? The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an inorder traversal of the entire tree. But deep down I feel that I am not using the BST property here. Is my assumptive solution correct or is there a better one available ? 回答1: Here's just an outline of the idea: In a BST, the left subtree of

Building a Binary Tree

六眼飞鱼酱① 提交于 2019-12-14 04:21:15
问题 I build an expression into a binary tree each time it rolls through the loop, creating a new tree at each ending of ")" and pushing those operators/operands into a stack to be popped back into one complete binary tree. My Build Method: package lab5; import net.datastructures.*; public class Expression<T> { /** Contain Linked Tree and Linked Stack instance variables **/ LinkedBinaryTree<T> tree; LinkedStack<LinkedBinaryTree<T>> stack; public Expression () { tree = new LinkedBinaryTree<T> ();