binary-tree

Implementing a binary heap using a linked list [duplicate]

谁说胖子不能爱 提交于 2019-12-25 09:28:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Linked list implementation of Binary Min Heap (Having trouble with manipulation…) Greetings, I'm having trouble figuring out an algorithm that will give me the location of a tree node in a linked list implementation of a binary heap. I've implemented a heap using arrays now I want to try using a linked list; Is there a way to find the tree node whose array index would have been i had I used an array to represent

Delete node from a C binary tree without messing it up

吃可爱长大的小学妹 提交于 2019-12-25 08:16:40
问题 I am a beginner working on a C binary tree library.I am wondering on how could I delete a node from a binary tree without messing up the entire thing.Here is how I create the tree: the structure: struct Node { int value; struct Node *left; struct Node *right; }; typedef struct Node TNode; typedef struct Node *binary_tree; Creation of the tree: binary_tree NewBinaryTree(int value_root) { binary_tree newRoot = malloc(sizeof(TNode)); if (newRoot) { newRoot->value = value_root; newRoot->left =

Binary Search Tree To List Scheme

此生再无相见时 提交于 2019-12-25 08:13:49
问题 I am having trouble understanding how to take in a BST and convert it into a list without using append or any advanced techniques. For example, you are given a BST with each node having a number and a name (sorted by string smallest to largest) and you want to output a list, in order, of the items in that BST with a value of 3 or something along these lines. I understand this can be done recursively but I think my biggest problem with understanding this has to do with the splitting of the

Format Jlabel - JTextBox

佐手、 提交于 2019-12-25 07:45:11
问题 I have a simple binary tree printer: public String displayTree(){ if(root != null){ return this.toString(new StringBuilder(), true, new StringBuilder(), root).toString(); }else{ return "Empty tree"; } } private StringBuilder toString(StringBuilder prefix, boolean isLeft, StringBuilder sb, BinaryNode<T> node) { if(node.getRight() !=null) { toString(new StringBuilder().append(prefix).append(esIzquierdo ? "│ " : " "), false, sb, node.getRight()); } sb.append(prefix).append(isLeft? "└── " : "┌──

Implementing Binary Tree in Java with Generic Comparable<T> data?

不问归期 提交于 2019-12-25 07:05:19
问题 Q: In my implementation of a binary tree below, why does the compiler choke at if (data.compareTo(this.data) <= 0) , producing Error: incompatible types: java.lang.Comparable<T> cannot be converted to T ? Both data and this.data are of type Comparable<T> and should be able to use or be an argument to the compareTo() method...right? Well, clearly not. But I really don't understand why. Generics are still baffling me. public class MyBinaryTreeNodeG<T>{ Comparable<T> data; MyBinaryTreeNodeG<T>

creating binary tree not binary search tree

大城市里の小女人 提交于 2019-12-25 06:46:25
问题 I want to create a binary tree which fills from left to right. i.e. if 1,2,3 are to be inserted then the tree should look like 1 / \ 2 3 I wrote an insert function, to insert the nodes in the tree. For the first node everything works okay..But, for the next node's (if I want to insert 4,5 as children to 2 and later 6,7 as children to 3) how should I switch between the parents (2,3)? Here's my insert function struct node * Insert(struct node * node, int data) { if(node == NULL) return (newNode

Search a tree with three nodes?

岁酱吖の 提交于 2019-12-25 03:15:30
问题 I can't seem to figure this out My tree nodes have the following 3 string fields and 3 node fields which are left, middle and right. This is what I have public TreeNode findNode(String name) { TreeNode pointer = this.getRoot(); if (!(name.equals(pointer.getName()))) { pointer = pointer.getLeft(); findNode(name); pointer = pointer.getMiddle(); pointer = pointer.getRight(); } return pointer; } I see that the problem is that I keep reseting the pointer to root with every iteration but I think

Nonrecursive/Iterative Binary Search Tree in C (Homework)

ぐ巨炮叔叔 提交于 2019-12-25 02:22:27
问题 How can I create/delete a node in a Binary Search Tree using Iterative Algorithm in C? 回答1: Iterative insertion: struct tree_node *Insert_Element (struct tree_node *root, void *key, void *data) { struct tree_node *new_node, *node; node = root; do { switch (compare(key, node->key)) { case -1: { if (node->left == NULL) { if ((new_node = create_node(key, data)) == NULL) { return NULL; } node->left = new_node; return new_node; } node = node->left; } break; case 1: { if (node->right == NULL) { if

to find out if a binary tree is balanced or not efficiently

笑着哭i 提交于 2019-12-25 01:45:17
问题 A binary tree is said to be height balanced if both its left sub-tree & right sub-tree are balanced and the difference between the height of left sub-tree and right sub-tree is less than or equal to 1. i have to find out if a given binary tree is balanced or not! based on the above concept i have used the following code: bool isbalanced(struct node *root) { int left,right; if(root==NULL) return true; else { left=height(root->left); right=height(root->right); if(abs(left-right)<=1 &&

Speeding up binary tree traversal with multiple processors Haskell (parallel)

你离开我真会死。 提交于 2019-12-25 01:22:22
问题 Following the examples in Chapter 24 of "Real World Haskell" and Chapter 2 of "Parallel and Concurrent Programming in Haskell", I've managed to build the following functions for building and traversing a binary tree quickly using multiple processors. import Control.Parallel (par, pseq) import Control.DeepSeq data Tree x = Empty | Node x (Tree x) (Tree x) deriving (Show, Read, Eq) -- Create instance of NFData for Tree data type (defining "normal form") instance NFData a => NFData (Tree a)