Java implementation of the two-fork search tree addition, pre-order, order, sequence and sequence traversal, the number of nodes to find the tree, the maximum value of the tree, the minimum value, find and other operations

Source: Internet
Author: User
Tags comparable

Say nothing, directly on the code.

The first is the node class, we all know

/** * Binary tree node class *  * @author heyufan * *  @param <T> */class node<t extends comparable<? Super t>>{/ * * Node stored value */private T data;/** * Left child node */private node<t> leftnode;/** * Right child node */private node<t> RIGHTNODE;PUBL IC Node () {this (null);} Public Node (T data) {this.data = Data;this.leftnode = Null;this.rightnode = null;} /** * @return Data */public T getData () {return data;} /** * @param data *            to set data */public void SetData (T data) {this.data = data;} /** * @return Leftnode */public node<t> getleftnode () {return leftnode;} /** * @param leftnode *            leftnode */public void Setleftnode to be set (Node<t> leftnode) {this.leftnode = Leftnode;} /** * @return Rightnode */public node<t> getrightnode () {return rightnode;} /** * @param rightnode *            rightnode */public void Setrightnode to be set (Node<t> rightnode) {This.rightnode = RightNod e;}}

  

And then the implementation of the two-fork search tree.

 

/** * */package datastructure.tree.binarytree;import java.util.linkedlist;import java.util.Queue;/** * binary sorting tree * * @author Heyufan * */public class binarytree<t extends Comparable<? Super t>>{//two fork tree root node private node<t> root;//two fork tree total number of nodes private int size;public BinaryTree () {this.root = null; this.size = 0;} Generates a two-fork tree based on one node (the node acts as the root node) public BinaryTree (node<t> root) {this.root = Root;this.size = 1;} /** * Insert * * @param value * To insert binary tree values */public void Insert (T value) {root = insert (this.root, value);} /** * Total number of nodes of the binary tree * * @return */public int size () {return this.size;} /** * Binary tree is empty tree * * @return */public boolean isEmpty () {return this.size = = 0;} /** * Detects if a binary tree node contains value * * @param value * @return */public boolean iscontain (T value) {return Iscontain (root, value);} /** * Recursively compares the values of node nodes with value * @param node * @param value * @return */private boolean Iscontain (node<t> node, T value {//If the specified node is empty, indicating that the current binary tree does not contain value, returns falseif (node = = null) {return false;} Node node values are compared to valueThe result of int result = Node.getdata (). CompareTo (value);//If result equals 0, the node node has the same value as the value, indicating that the two fork search tree contains value and returns Trueif (result = = 0) {return true;} If result is less than 0, the value of the node nodes is smaller than value, continuing to recursively compare the right child node of node with Valueelse if (Result < 0) {return Iscontain (Node.getrightnode (), value);} If result is greater than 0, the node value is larger than value, continuing to recursively compare the left child node of node with Valueelse{return Iscontain (Node.getleftnode (), value);}} /** * Gets the minimum value in the binary tree * * @return */public T getmin () {//If the binary tree is empty, return Nullif (IsEmpty ()) {return null;} return Getmin (Root). GetData ();} /** * Depending on the nature of the binary search tree, we can know that the minimum value of a binary search tree must be the leftmost node of the tree * * @param node * root node to search * @return */private node<t> get Min (node<t> node) {node<t> Leftnode = Node.getleftnode ()//If the left node of the current node is empty, the value of this node is the smallest value of the tree if (Leftnode = = NULL) {return node;} else{//Otherwise, recursively traverse the left subtree of the current node return getmin (Leftnode);}} /** * Gets the maximum value in the binary tree (not recursive, recursive like getmin) * * @return */public T Getmax () {//If the current binary tree is empty, return Nullif (IsEmpty ()) {return null;} Node<t> Rightnode = this.root;//Determine if the current node (starting from the root node) has a right child node//If not, the value of the current node is the two-fork search treeLarge value, current loop end//Otherwise, let Rightnode point to the right child node of the current node, continue while loop while (Rightnode.getrightnode ()! = null) {Rightnode = Rightnode.getrightnode ();} return Rightnode.getdata ();} /** * Inserts the value values into the two-tree node-root nodes * * @param the root node of the node * target subtree * @param value * Values to be inserted * @return return here nod E is actually the updated root node */private node<t> Insert (node<t> node, T value) {//if the target root node to be inserted is NULL, this node is where the value is to be inserted if (node = = NULL) {node = new node<t> (value); This.size++;return node;} 0 means value equals node, 1 means value is less than node, 1 means value is greater than node, int result = Value.compareto (Node.getdata ()); if (Result < 0) {//When value is less than the value of node, the value is inserted into the left subtree of the node, at which point the target node of value is Node.getleftnode () Node.setleftnode (insert ( Node.getleftnode (), value));} else if (Result > 0) {//when value is greater than the value of node, the value is inserted into the right subtree of the node, at which point the target node of value is Node.getrightnode () Node.setrightnode ( Insert (Node.getrightnode (), value));} else{//if result = 0, indicating that the inserted value already exists, you can choose to update the value or do nothing}return node;} /** * First Order traversal */public void preorder () {preorder (root);} /** * post-traversal */public void Postorder () {pOstorder (root);} /** * Middle sequence traversal */public void Inorder () {inorder (root);} /** * Sequence traversal */public void level () {if (root = null) return; queue<node<t>> queue = new linkedlist<node<t>> (); Queue.add (root); node<t> node = null;while (Queue.size ()! = 0) {node = Queue.poll (); System.out.println (Node.getdata ()); if (node.getleftnode () = null) Queue.add (Node.getleftnode ()); Node.getrightnode ()! = null) Queue.add (Node.getrightnode ());}} /** * Sequential traversal: Iterates over node-root subtree to print the values of the nodes, then traverses the left subtree of the node, and finally traverses the right subtree of the node * * @param node * The root node of the subtree to traverse */private void preorder (No De<t> node) {if (node = = null) {return;} else{//the value of the Print node System.out.println (Node.getdata ());//recursively traverse the left subtree preorder (Node.getleftnode ()) of the node; Recursively traverse the right subtree of the node preorder (Node.getrightnode ());}} /** * post-order traversal: traverse the subtree with node as root, traverse the left subtree of the node, then traverse the right subtree of the node, and finally the value of the print node * @param node * The root node of the subtree to traverse */private void Postorder (N Ode<t> node) {if (node = = null) {return;} The else{//recursively iterates through the node's left subtree Postorder (Node.getleftnode ()), or recursively traverses the right subtree of the node Postorder (node.getrightnodE ());//The value of the Print node System.out.println (Node.getdata ());}} /** * Middle sequence traversal: traverse a subtree with node as its root, traverse the left subtree, then print the value of the node, and finally traverse the right subtree * * @param node */private void Inorder (node<t> node) {if (node = = Nu ll) {return;} The else{//recursively iterates through the node's left subtree Inorder (Node.getleftnode ());//The value of the print node is System.out.println (Node.getdata ());//recursively traverses the right subtree of the node inorder ( Node.getrightnode ());}}}

  

Test:

/** *  */package datastructure.test;import datastructure.tree.binarytree.binarytree;/** * @author Administrator *  */public class Testbinarytree{public static void Main (string[] args) {binarytree<integer> tree = new Binarytree<integer> (); Tree.insert (5); Tree.insert (3); Tree.insert (2); Tree.insert (7); Tree.insert (8); Tree.insert (4); Tree.insert (1); Tree.insert (4); Tree.insert (1); SYSTEM.OUT.PRINTLN ("Whether contains 1:" + tree.iscontain (1)); SYSTEM.OUT.PRINTLN ("whether contains:" + Tree.iscontain (10)); System.out.println ("Number of nodes in the tree:" + tree.size ()); SYSTEM.OUT.PRINTLN ("Maximum value of the tree:" + tree.getmax ()); SYSTEM.OUT.PRINTLN ("Minimum value of the tree:" + tree.getmin ()); System.out.println ("First Order Traversal:"); Tree.preorder (); System.out.println ("post-post traversal:"); Tree.postorder (); System.out.println ("Middle sequence Traversal:"); Tree.inorder (); SYSTEM.OUT.PRINTLN ("Sequence Traversal:"); Tree.level ();}}

  

Structure of the tree:

Output Result:

Whether it contains 1:true
Whether it contains 10:false
Number of nodes in the tree: 7
Maximum number of trees: 8
Minimum tree value: 1

First-order Traversal:

5
3
2
1
4
7
8
Post-post traversal:
1
2
4
3
8
7
5
Middle Sequence Traversal:
1
2
3
4
5
7
8
Sequence Traversal:
5
3
7
2
4
8
1

Java implementation of the two-fork search tree addition, pre-order, order, sequence and sequence traversal, the number of nodes to find the tree, the maximum value of the tree, the minimum value, find and other operations

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.