On algorithm and data structure 72 Fork Lookup tree

Source: Internet
Author: User
Tags data structures

This paper introduces two kinds of realization of symbol table, unordered list and ordered array, the unordered list has high flexibility when inserting, and ordered array has high efficiency when searching, this article introduces the two Fork lookup tree (Binary search Tree,bst) This data structure combines the advantages of both of these data structures.

Binary lookup tree has a high flexibility, its optimization can generate a balanced binary tree, red and black trees and other efficient search and insert data structure, after the article will be introduced.

A definition

Binary lookup trees (Binary search tree), also known as ordered binary trees (ordered Binary), sorted binary trees (sorted Binary tree), refers to an empty tree or a two-pronged tree with the following properties:

1. If the left subtree of any node is not empty, the value of all nodes on the left subtree is less than the value of its root node;

2. If the right subtree of any node is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

3. The left and right subtree of any node is a two-fork lookup tree, respectively.

4. There is no node with equal key value (no duplicate nodes).

The following figure, this is the normal two-fork tree:

On this basis, coupled with the size of the relationship between the nodes, is the binary lookup tree:

Two implementation

In the implementation, we need to define an internal class node, which contains two nodes pointing to the left and right node, a key for sorting, and the value values that the node contains, and a value number that records that node and all the child nodes.

public class Binarysearchtreesymboltable<tkey, tvalue>: Symboltables<tkey, tvalue> where tkey:icomparable <tkey>, iequatable<tvalue>
{
    private Node root;
    Private class node
    {public
        node left {get; set;}
        Public Node Right {get;
        public int number {get; set;}
        Public TKey Key {get; set;}
        Public TValue Value {get; set;}
    
        Public Node (TKey key, TValue value, int number)
        {this
            . key = key;
            This. value = value;
            This. Number = number;
        }
    }
...
}

Find

Lookup operations and binary lookups are similar, compare key and node keys, if less than, then look in the left node nodes, and if they are greater, look in the right node node and, if they are equal, return value directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.