binary-search-tree

MIPS - implementing a binary search tree

大兔子大兔子 提交于 2019-12-31 04:12:06
问题 As our term project, we're implementing a binary search tree. The thought behind it is as follows: Assume a bst with 3 nodes: 10 / \ / \ 8 14 Its address representation is as follows (value, left node address, right node address, root node address)t: 400:|----------| | 8 | |----------| | 0 | |----------| | 0 | |----------| | 620 | |----------| | . | | . | | . | $a0=620:|----------| | 10 | |----------| | 400 | |----------| | 1000 | |----------| | 0 | |----------| | . | | . | | . | 1000:|------

Converting a heap to a BST in O(n) time?

落爺英雄遲暮 提交于 2019-12-30 06:28:06
问题 I think that I know the answer and the minimum complexity is O(nlogn) . But is there any way that I can make an binary search tree from a heap in O(n) complexity? 回答1: There is no algorithm for building a BST from a heap in O(n) time. The reason for this is that given n elements, you can build a heap from them in O(n) time. If you have a BST for a set of values, you can sort them in O(n) time by doing an inorder traversal. If you could build a BST from a heap in O(n) time, you could then have

To find largest element smaller than K in a BST

旧街凉风 提交于 2019-12-29 20:29:08
问题 Given a binary search tree and an integer K, i would like to find the largest element less than K. In the below tree, for K = 13, result = 12 for K = 10, result = 8 for K = 1 (or) 2, result = -1 10 5 12 2 8 11 14 I tried the below logic. But is there any better way to do this ? int findNum(node* node, int K) { if(node == NULL) { return -1; } else if(K <= node->data) { return findNum(node->left,K); } else if(K > node->data) { int t = findNum(node->right,K); return t > node->data ? t : node-

Animate Change of color of nodes in insertion of binary search tree

故事扮演 提交于 2019-12-28 18:39:14
问题 I already have implemented display of binary search tree . Here's the code , which paints the binary tree in a jpanel . public void paint(Graphics g) { super.paint(g); System.out.println(" in paint"); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int num = bst.size; int y = 25; int nodes = 1; int level = 1; int length = getWidth(); Queue<Node> q = new LinkedList<Node>(); Queue<Integer> q2 = new LinkedList<Integer>(); q

Insert sorted array into binary search tree

∥☆過路亽.° 提交于 2019-12-28 17:57:21
问题 I want to implement an algorithm that inserts sorted arrays into binary search trees but I don't want ending up with a tree that only grows to one side. Do you have any ideas? Thanks. 回答1: This should give you a balanced tree (in O(n)): Construct a node for the middle element in the array and return it (this will be the root in the base case). Repeat from 1. on the left half of the array, assigning the return value to the left child of the root. Repeat from 1. on the right half of the array,

Change binary search tree to balance

无人久伴 提交于 2019-12-25 19:49:13
问题 I have just learnt how to create a binary search data structure, which is going to be used to store thousands of words from a dictionary. The problem that I am getting is that it is taking a long time to count add and remove data. Usually 199263ms or 200 seconds for 100000 words to count. I was told that having a tree that can self balance will improve the efficiency and make the operations faster. My question is how can I make my tree auto balance so to make it efficient. I have made slight

Change binary search tree to balance

南楼画角 提交于 2019-12-25 19:49:07
问题 I have just learnt how to create a binary search data structure, which is going to be used to store thousands of words from a dictionary. The problem that I am getting is that it is taking a long time to count add and remove data. Usually 199263ms or 200 seconds for 100000 words to count. I was told that having a tree that can self balance will improve the efficiency and make the operations faster. My question is how can I make my tree auto balance so to make it efficient. I have made slight

Binary search Tree Implementation.

谁说胖子不能爱 提交于 2019-12-25 08:58:48
问题 I was trying to implement binary search tree but I think I have made a mistake in my insert function. Here's my code #include<iostream> #include<memory.h> #include <cstddef> using namespace std; struct bst_node { int info; struct bst_node *left_node_ptr; struct bst_node *right_node_ptr; }; struct bst_node* getnode(int x) { struct bst_node* ret= new bst_node; ret->info=x; ret->left_node_ptr=NULL; ret->right_node_ptr=NULL; return ret; } void insert(struct bst_node **root, int var_info) { struct

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