Binary search tree (search, add, traverse)--java

Source: Internet
Author: User

stacks, queues, and lists all have their own advantages, and there are drawbacks as well.
If I want an ordered array and a linked list of course this is very well implemented. Now I'm going to look for a value in these data structures. First say the array, because it is ordered by binary search quickly can be found. The efficiency of the search is still very high, but if it is inserted, in order to ensure orderly, I have to find the insertion position, and then the number is larger than the insertion of numbers in turn backward, then the first reaction is linked list! He hit the insertion speed quickly, just change the pointer to point to it. But the list of large search to start from the beginning to find AH. The next address can be known only if you know the address of the previous element. So it's hard to look up the list. At this time, someone introduced the tree.
The tree is divided into a number of different types, only the special two-fork tree in the two-fork search tree.
Two fork Search tree definition: The key value of the left child node of a node is less than this node, and the right child node's key is greater than or equal to the parent node.
Two forks the search tree when inserting can directly change the left tree right tree pointer pointing, the search can be based on the characteristics of binary tree sorting.

This is a binary search tree.


Now we begin to describe the tree in code. See Node class first

Package test;   /**    * Tree Node class */Public  class TreeNode {public      int keyValue;    Keyword value public      TreeNode leftnode;//left node public      TreeNode rightnode;//right node public            TreeNode () {} public      TreeNode (int Key)   {          this.keyvalue = Key;      }  

The code is not much, it describes the content of a node. The description of binary search tree is mainly described from query node, add node, traverse, maximum value, minimum value, delete node. This does not include cases where there are equal nodes.
Query Node: This is relatively simple, according to the definition of binary tree query on it. It is most convenient to look at the diagram and write code.


look at code  

Public TreeNode Search (int Key) {          TreeNode node = root;          First define a node to point to the root, in the following loop          //As long as the node value is not equal to the value of the node to be looked up into the loop if not found then return null while          (node.keyvalue! = key) {If              (key < Node.keyvalue)            {//If the value you are looking for is less than the node value then point to left node                  = Node.leftnode;              }            else            {//otherwise point to right node                  = Node.rightnode;              }              if (node = = null)            {//If the node is empty then return null return                  null;}          }          return node;      }  

Adding nodes, the process of adding a node is now searchable and then added. Look at the picture first

&NBSP;

code is as follows:

public void Insert (int Key) {TreeNode node = new TreeNode (Key);              Before adding a node, first find the location to add, so remember to add the parent node of the node//Let the parent node to point to the node to be added if (root = null) {///If the root node is empty, the root point points to the new node          root = node;              } else {TreeNode CurrentNode = root;//defines the current node and points to the root node TreeNode parentnode;                  while (true) {//Find the location where the node was added parentnode = CurrentNode;                      if (Key < currentnode.keyvalue) {currentnode = Currentnode.leftnode;                          if (CurrentNode = = null) {//When an empty node is found, the left node of the parent node points to the new node Parentnode.leftnode = node;                      Return                      }} else {currentnode = Currentnode.rightnode;                          if (CurrentNode = = null) {//When an empty node is found, the right node of the parent node points to the new node Parentnode.rightnode = node;                      Return   }                  }           }          }      }  

Traverse Tree: Traversal is divided into mid-sequence traversal (most often and most useful), pre-sequence traversal, and subsequent traversal.
here is a middle sequence traversal diagram, understanding this pre-order and follow-up are very good understanding.

public void Display (TreeNode node) {          if (node! = null)         {              display (node.leftnode);              System.out.println (Node.keyvalue + ",");              Display (Node.rightnode);          }      

maximum value, minimum value: This does not have to say, the maximum value has been to the right, the minimum value has left.
directly on the code:
public int Max () {      TreeNode node = root;      TreeNode parent = null;      while (node! = null)   {          parent = node;          node = Node.rightnode;      }      return parent.keyvalue;  }      public int min () {      TreeNode node = root;      TreeNode parent = null;      while (node! = null)   {          parent = node;          node = Node.leftnode;      }      return parent.keyvalue;  }  




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Binary search tree (search, add, traverse)--java

Related Article

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.