Data Structure _ tree _ Binary Search Tree

Source: Internet
Author: User
Binary Search Tree

Binary Search Tree (BST) is also known as binary search tree and binary sorting tree.

1. Features

The binary search tree is a binary tree first;
For any node, if the left subtree is not empty, the value of any node on the left subtree is not greater than the value of its root node;
If the right subtree is not empty, the value of any node on the right subtree is not greater than the value of its root node;
The left and right subtree of any node is also a binary search tree.

2. Sequential Traversal

Traverse the binary search tree in the middle order and the result is an ordered data column.

// Traverse the binary search tree in the middle order. The result is the sorted data public void midorder (treenode node) {If (node = NULL) {return;} midorder (node. getleft (); system. out. print (node. getkey () + ""); midorder (node. getright ());}
3. Add nodes

New nodes added to the Binary Search Tree always become leaf nodes.

// Add a node to construct the public treenode addnode (treenode node, int key) of the Binary Search Tree {// if no node exists, create a node. Otherwise, if (node = NULL) is returned) {node = new treenode (key);} If (Key <node. getkey () {node. setleft (addnode (node. getleft (), key);} else if (Key> node. getkey () {node. setright (addnode (node. getright (), key);} else {return node ;}
4. Search nodes

The binary search tree needs to be traversed. the time complexity depends on the depth of the traversal.

// Find a node. the time complexity is the number of layers that have elapsed. The worst is O (h) Public treenode searchnode (treenode node, int key) {// if no node is found, null is returned, if (node = NULL | node. getkey () = Key) {return node;} else if (Key <node. getkey () {return searchnode (node. getleft (), key);} else {return searchnode (node. getright (), key );}}
5. delete a node

Data Structure _ tree _ Binary Search Tree

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.