I define a binary tree as a search table. The tree is a linked list binary tree, the node is like

```
typedef struct node{
int key;
char *buf;
...
}node_t;
```

typedef table_t node_t *; so I have functions like

```
insert(table_t table, node_t node)
search(table_t table, node_t node)
```

now I have multiple keys, like

```
typedef struct node{
int key1;
int key2;
char *buf;
...
}node_t;
```

and I want to have functions like:

```
search_by_key1(table_t table, node_t node, int key1)
search_by_key2(table_t table, node_t node, int key2)
```

indeed, it is like a database, I can search any keys for an item.

are there any source code examples? I'm using linux C thanks!

Well, in a binary tree, key's only make sense in relation to each-other. Meaning that key1_a makes sense in the left branch only if key1_b is larger/smaller in the right branch (depending how you structure it). If you had a tree of two set of keys, hypothetically they could be in the same tree data-structure if they had the same proportions. As in, if each key in key2 was exactly 50 above key1. But that wouldn't really be useful at all.

The only idea I have is making it so there are some null value keys. So that if you're traversing your tree and you're at a leaf of key1 but not key2, you just keep going, and the rest of the tree lower down will have a null value (maybe -1?) in the key, or will have a boolean trigger making it so key1 is no longer present in this branch of the tree. It's kind of hard to describe, but do you see what I mean? Like basically treat it almost as 2 inter-weaved trees, and go along together until you have to diverge (and do that while keeping the actual tree structure the same, but have null values for one of the keys)

It sounds overly complex though, so I don't know if it'd be worth it.

A binary-tree can only be indexed by a single key.

If you want to index using multiple keys, you can build a meta-tree for every key, meta-tree have meta-node(s) where:

```
typedef struct meta_node
{
int index;
node * data;
...
}
meta_node_t;
```

You could have two trees, but still use the same nodes:

```
struct basic_node {
int key;
struct basic_node *left, *right;
struct node *node;
};
struct node {
struct basic_node tree1;
struct basic_node tree2;
char *data;
};
struct node *tree1_root, *tree2_root;
```

When you create a node, you'll allocate a `struct node`

, then insert each `basic_node`

separately to the respective tree, according to its key. In each `basic_node`

, set the `node`

pointer to the node containing it.

When looking up, just start with either tree root, and do a simple lookup, which would lead you to the desired `basic_node`

, and then to the node itself.

If you want to delete, you'll have to first unlink the node from both trees, then you can free it.

Similar Questions

Search(T,k) x<- root[T] while x != NULL and k != key[x] do if k<key[x] then x <- left[x] else x <- right[x] return x I just started with algorithms and I often see <- this and key[x]

This is my program, but I don't know how to show the binary search tree results in array (one dimensional). I use random as inputs. How to show binary search tree results in arrays? Help me please. #i

I've been stuck on the insertion part of the binary search tree. I get so confused with nested structs. The basic idea of this program is to create a bst that is able to hold names and double values w

I'm currently trying to implement an immutable BST in clojure. This is my make-tree function: (defn make-tree [v] {:v v :l nil :r nil}) and insertion: (defn insert [tree v] (if (nil? tree) (make-tree

How much does it cost the search operation in a Binary tree? Is it O(n)?

I have specific confusion in implementing function within the existing binary tree that stores pets' names and kinds, first what I've done: Declarations[tree.h]: typedef struct item { char petname[20

I am having a huge issue with my recursive function in my binary search tree. My project is due in a few hours and I cannot for the life of me get ahold of my instructor. My function seems to only be

First, I have already read C++ Binary Search Tree status access violation error with adding nodes and Inserting in a binary search tree access violation error but am still having trouble fully impleme

Im trying to create an insertion function on an binary search tree. I looked at other questions here but they re not like my problem. I can compile it but it crashes on the commented line below: void

I have a question with regards to the Binary Search Tree Implemetation in C++. Here is the question below Implement a simple (non-templated) BST which stores integers. Provide the following operations

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 na

I have a binary tree, it is strange: the root is the highest number, and the other go decreasing... (Example: Huffman tree) I need to make an algorithm that searches a key inside it. I tried a lot but

i am constructing the operations of binary search tree in OCaml. type ('a, 'b) bst = | Node of 'a * 'b * ('a, 'b) bst * ('a, 'b) bst | Leaf;; let rec insert k v = function | Leaf -> Node (k, v, Lea

So I've looked around a lot for some information on how to take elements from a binary search tree and write them to a file, and then later read them back into the tree as a type of load function. I

I am trying to connect the siblings of the binary search tree. You can find the logic in the Connect() method. My question is, is there any better way to do it? Am I overdoing by using two queues to i

I want to make a generic BST, that can be made up of any data type, but i'm not sure how I could add things to the tree, if my BST is generic. All of my needed code is below. I want my BST made up of

I can't figure out how to write a Binary Search Tree to file recursively. I open a BufferWriter with the file to wrtie too, in the Tree class. I then send the BufferWriter to the Node class to travers

I have implemented BST in C. Insert and lookup works fine. But delete has issues when deleting the root node. I'm not able to free the pointer to the root node. I could if I pass it as a double pointe

For a school project I am trying to make a binary search tree at the same time we are supposed to learn how to use 'friendship' in classes. The errors I get while compiling are: [I put comments in cod

I have already put in the following methods for a binary search tree: import java.util.Collections; import java.util.NoSuchElementException; import java.util.ArrayList; public class MyTree { private c

Hi i have a python related question, I have a sorted Numpy array which i have to find the index of certain values quickly, I've been using a binary search so far but the issue im having is that there

I am working on building a Binary Search Tree and I want to create a function that records the height of each node and sums it. I am trying to use recursion. For me, the difficulty lies in assigning a

I am building a binary search tree. Here is the code: #include<stdio.h> #include<stdlib.h> struct tree_node { int val; struct tree_node *left; struct tree_node *right; }; void insert(struc

I am constructing a binary search tree of size 7 and height 3. I only have to hard-code it, not generate it through a function. This is the tree that I have hard-coded Node (Node (Node (Empty, 0, Empt

I need to build a balanced binary search tree. So far my program inserts the numbers from 1 to 26, but my program does not build it into a balanced binary search tree. If anyone could look at my code

I have a binary search tree consists of nodes like : struct ProductNode { Product data; ProductNode* left; ProductNode* right; }; and i have a delete function that takes ProductNode pointer parameter

I'm currently trying to access a binary search tree I created in form1 within form2. My code for the first form is: public Home() { InitializeComponent(); } AddArtist secondForm = new AddArtist(); BS

I am still fairly new to generics and binary search trees, and I am trying to add a custom Student object to a BST, and don't know exactly how to go about implementing it. I have my Student class decl

so i need to implement a member function, pre-order and inorder traversal of a binary search tree using recursion. i'm having trouble implementing all three, since they're coming out with the wrong ou

I recently wrote a fairly simple piece of code attempting to implement a Binary Search Tree in C with insertion, search, deletion and display operations. Unfortunately, the code does not seem to work.

I've been coding up a bunch of different binary search tree implementations recently (AVL, splay, treap) and am curious if there's a particularly good way to write an iterator to traverse these stru

So I've stumped on this current problem I'm working on. Basically, I need to add an element to my array based binary search tree. According to my text it is similar to the compareTo method. I'm not ev

I am trying to create a custom Binary Search Tree, and I have everything done except for the rotate function, which seems to be not moving a node over. The rotate function only gets called when a node

For starters this is homework, I just really need help with a binary search tree. The program is to display polymorphism, using person as an abstract base class, and other types of people which inheri

I had a quesiton here, but it didn't save. I'm having trouble balancing a fully unbalanced tree (nodes 1-15 along the right side). I'm having trouble because I get stack overflow. > // balancing pu

If i construct a binary search tree adding the following values in order: 10, 7, 16, 12, 5, 11, 2, 20, 1, 14 I get a tree of height 5. Is there a method (other than trial and error) that I can use t

I am having trouble outputting a binary search tree. I have to build the tree, and then place all the doubles from the tree in order into a vector, and then output that vector in order. The problem I

This question already has an answer here: How do you validate a binary search tree? 18 answers Given a simple binary tree, how can we prove that tree is a binary search tree? When we traverse a

I had a question of exactly how a binary search tree of strings works. I know and have implemented binary search trees of integers by checking if the new data <= parent data then by branching left

I'm working on a program that works with a binary tree. I'm getting an error with adding new nodes into the tree. I can add one node, but after adding another, I get a STATUS_ACCESS_VIOLATION error. I

I'm using OCaml. I have type: type 'a bt = Empty | Node of 'a * 'a bt * 'a bt;; Also I have example BST: let tree = Node(1,Node(2,Node(4,Empty,Empty),Empty),Node(3,Node(5,Empty,Node(6,Empty,Empty)),

I'm working on this homework where I need to print my binary search tree in preorder, postorder, and inorder. However, it seems like only my inorder method is working. I have used the following test c

Ok, I have written a binary search tree in OCaml. type 'a bstree = |Node of 'a * 'a bstree * 'a bstree |Leaf let rec insert x = function |Leaf -> Node (x, Leaf, Leaf) |Node (y, left, right) as node

I have seen a lot of search algorithms to search in a binary sorted tree, but all of them are using the same way: recursion. I know recursion is expensive in comparison to loops, because every time we

I am trying to flatten a binary search tree to a singly linked list. Binary search tree: 6 / \ 4 8 / \ \ 1 5 11 / 10 Flattened Singly Linked List: 1 -> 4 -> 5 -> 6 -> 8 -> 10 -> 1

I'm working on a problem for class I'm stuck on. It involves adding a methods to the Binary Search Tree found here: http://algs4.cs.princeton.edu/32bst/BST.java.html I need to develop an iterative cei

I have the following problem I need to solve, but is strugling a bit. Would really appreciate it if someone can help. In short in comes down to the following: If the search key is in the array - it

I need to add an item to a binary tree given only the item to be added. Here is the code I'm given: void BinaryTree::add(Data * data) { if (root == NULL) { root = new BinaryTreeNode(data); } else { r

I have a homework assignment to implement a binary search tree (create, delete, search). I used the example provided by the teacher but I can't make it work. Here's my code so far: void insert_node(in

i'm trying to implement some functions that allow me to add Books to a binary search tree for the Student class, but I'm getting a strange error: msvcr100d.dll!strcmp(unsigned char * str1, unsigne