JAVA Binary Search Tree and java Binary Search

Source: Internet
Author: User

JAVA Binary Search Tree and java Binary Search

I. Binary Tree concept:

A binary tree refers to an ordered tree with a maximum of two Subtrees. The left subtree is usually called the left subtree, And the right subtree is called the right subtree ". It can be seen that a binary tree is still a tree, but a special tree.

Each node of a binary tree can have at most two Subtrees (no node greater than 2 exists ). A binary tree can be left or right, and cannot be reversed.

 

Two important differences between a tree and a binary tree are as follows:

1. There is no limit on the maximum number of nodes in the tree, while the maximum number of nodes in the binary tree is 2, that is, the maximum number of nodes in the binary tree is 2.

2. the nodes of the disordered tree have no left or right points, while the nodes of the binary tree have left or right points. That is to say, the binary tree is an ordered tree.

 

Full Binary Tree:

      

A k-depth binary tree. If it contains 2 ^ K-1 nodes, it is called a full binary tree. The full Binary Tree feature that the number of nodes on each layer is the maximum number of nodes, that is, the number of nodes on each layer is 1, 2, 4, 8, 16, 32 ,......, 2 ^ K-1 ).

 

Complete Binary Tree:

      

If all the other nodes except the last layer of a binary tree are full and the last layer is full, or only several consecutive nodes are missing on the right, this binary tree is a Complete Binary Tree.

Differences:A full Binary Tree is a special full binary tree. When all the nodes at the last layer of the Complete Binary Tree are full, the full binary tree becomes full.

 

Ii. Basic operations for Binary Trees:

First, define our node class:

1 package mytree; 2 3 public class Node {4 int value; // value Field 5 Node left; // left subnode 6 Node right; // right subnode 7 public Node (int value) {8 this. value = value; 9} 10 @ Override11 public String toString () {12 return String. valueOf (value); 13} 14 15 public void display () {16 System. out. print (this. value + "\ t"); 17} 18 19}

 

Define our method class:

1 public class BinaryTree {2     private Node root = null;3 4     public BinaryTree(int value) {5         root = new Node(value);6         root.left = null;7         root.right = null;8     }9 }

 

1. Add nodes:

First:

1 public String insert (int value) {// insert 2 String error = null; // error 3 Node now = new Node (value ); // create the Node 4 Node curr = root; // obtain the root Node 5 if (curr = null) {// if the root Node is empty 6 curr = now; // use the Node to be inserted as the root Node 7} else {8 while (true) {9 Node parent = null; // create a temporary storage Node 10 if (curr. value> value) {// if the current node exists> the node to be inserted, insert the node to the left subnode 11 parent = curr; // assign 12 curr = curr to the master node. left; // get the left subnode 13 if (curr = null) {// 14 parent if the left subnode is empty. left = now; // insert 15 break; 16} 17} else if (curr. value <value) {18 parent = curr; 19 curr = curr. right; 20 if (curr = null) {21 parent. right = now; 22 break; 23} 24} else {25 error = "the same value exists in the tree:"; 26} 27} 28} 29 return error; 30}

Recursive addition:

1/* 2 * Insert recursive call Implementation 3*4 **/5 public Node insert2 (Node node, int data) {6 if (node = null) {7 node = new Node (data); 8} else {9 if (data <= node. value) {10 node. left = insert2 (node. left, data); 11} else {12 node. right = insert2 (node. right, data); 13} 14} 15 return (node); 16} 17

 

2. Define a method to directly return the entire Binary Tree:

1 public Node getrootNode () {// return the entire binary tree 2 return root; 3}

3. Define a method for Traversing nodes (in the middle order ):

1/** 2 * // ordinal traversal (recursion ): 3*1. Call itself to traverse the left subtree of the node 4*2. Get the current node 5*3. Call itself to traverse the right subtree of the node 6 */7 public void inOrderTraverse (Node node) {8 if (node = null) 9 return; 10 inOrderTraverse (node. left); 11 node. display (); 12 inOrderTraverse (node. right); 13} 14

4. Create a test class to check whether all of our methods are correctly written:

1 package mytree; 2 3 public class Test {4 5 public static void main (String [] args) {6 // TODO Auto-generated method stub 7 BinaryTree B = new BinaryTree (12); 8 B. insert (10); // normal Insertion Method 9 Node no = B. getrootNode (); // obtain the binary tree object 10 B. insert2 (no, 20); // recursively insert 11 no = B. getrootNode (); 12 B. inOrderTraverse (no); // traverse in the Middle Order 13} 14}

 

Run:

It seems that adding nodes and traversing nodes can be written;

 

 

5. Forward traversal:

1/* Forward traversal 2 * Access Node 3 * access itself to traverse the left subtree 4 * access itself to traverse the right subtree 5 **/6 public void PreOrderTraverse (node Node) {7 if (node = null) 8 return; 9 inOrderTraverse (node. left); 10 node. display (); 11 inOrderTraverse (node. right); 12}

 

6. Post-order traversal:

1/* post-order traversal 2*3 * access itself to traverse the left subtree 4 * access itself to traverse the right subtree 5 * Access Node 6 **/7 public void nexOrderTraverse (node Node) {8 if (node = null) 9 return; 10 inOrderTraverse (node. left); 11 inOrderTraverse (node. right); 12 node. display (); 13}

 

7. Get the minimum value: (that is, traverse the left subtree until it is null)

1  public int getMinValue() {  2             Node current = root;  3             while (true) {  4                 if (current.left== null)  5                     return current.value;  6                   7                 current = current.left;  8             }  9         }

8. Get the maximum value: (that is, traverse the right subtree until it is null)

 1   2         public int getMaxValue() {   3             Node current = root;   4             while (true) {   5                 if (current.right== null)   6                     return current.value;   7                    8                 current = current.right;   9             }  10         }

If there is something temporary, search for and delete and sort it out;

 

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.