"Data structure and algorithm" Java implementation and characteristics summary of binary tree

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

Binary tree is a very important data structure, it has both the array and the list of the characteristics of each: it can be as fast as an array to find, or as fast as a linked list can be added. But he also has his own shortcomings: the removal operation is complex.

Let's start by introducing some concept nouns about binary trees.

Binary tree: Is an ordered tree with a maximum of two subtrees per node. When using a two-fork tree, the data is not arbitrarily inserted into the node, the key value of the left child node of a node must be less than this node, the key value of the right child node must be greater than or equal to this node, so it is also called binary search tree, binary sorting tree, binary searching tree.

Complete binary tree: If the height of the two-fork tree is H, except for the H layer, the nodes of the other layers (1~h-1) reach the maximum number, the layer h has leaf nodes, and the leaf nodes are arranged from left to right, which is the complete binary tree.

Full two fork tree--except the leaf node, each node has left and right cotyledons and leaf nodes are at the bottom of the two fork tree.

Depth-the number of layers of a two-fork tree is the depth.

Summary of the characteristics of the binary tree:

(1) The time complexity of searching, deleting and inserting trees is O (logn)

(2) The method of traversing the binary tree includes the pre-order, the middle sequence, the order

(3) Non-equilibrium tree refers to the number of child nodes on the left and right side of the root is inconsistent

(4) In a non-empty binary tree, the total number of nodes in layer I does not exceed, i>=1;
(5) A maximum of two fork trees with a depth of H has a node (h>=1), at least h nodes;
(6) For any binary tree, if its leaf node is N0, and the number of nodes with a degree of 2 is N2, then n0=n2+1;


Java code Implementation of binary tree

The first is the definition of the node of the tree, and the following code uses the simplest int basic data type as the data of the node, if you want to use a node with a more complex data type, replace it with the corresponding object.

/** * * @ClassName: Com.tree.TreeNode * @Description: Node * @author Zhaokaiqiang * @date 2014-12-5 pm 4:43:24 * */public C Lass TreeNode {//Left node private TreeNode leftreenode;//right node private TreeNode rightnode;//Data private int Value;private Boolean Isdelete;public TreeNode Getleftreenode () {return leftreenode;} public void Setleftreenode (TreeNode leftreenode) {this.leftreenode = Leftreenode;} Public TreeNode Getrightnode () {return rightnode;} public void Setrightnode (TreeNode rightnode) {this.rightnode = Rightnode;} public int GetValue () {return value;} public void SetValue (int value) {this.value = value;} public Boolean isdelete () {return isdelete;} public void Setdelete (Boolean isdelete) {this.isdelete = Isdelete;} Public TreeNode () {super ();} public TreeNode (int value) {This (null, NULL, value, FALSE);} Public TreeNode (TreeNode Leftreenode, TreeNode rightnode, int value,boolean isdelete) {super (); This.leftreenode = Leftreenode;this.rightnode = Rightnode;this.value = Value;this.isdelete = Isdelete;}@Overridepublic String toString () {return "TreeNode [leftreenode=" + Leftreenode + ", rightnode=" + Rightnode + ", value=" + Value + ", isdelete=" + isdelete+ "]";}}

The following is a code implementation for a two-fork tree. Since the deletion of nodes in a binary tree is cumbersome, the following code uses the node's Isdelete field to identify the state of the node

/** * * @ClassName: Com.tree.Tree * @Description: Two definition of fork tree * @author Zhaokaiqiang * @date 2014-12-8 Morning 7:51:49 * */public C Lass BinaryTree {//root node private TreeNode root;public TreeNode getroot () {return root;} /** * Insert Operation * * @param value */public void insert (int value) {TreeNode NewNode = new TreeNode (value); if (root = null) {RO OT = Newnode;root.setleftreenode (null); Root.setrightnode (null);} else {TreeNode CurrentNode = root; TreeNode Parentnode;while (true) {parentnode = currentnode;//put right if (Newnode.getvalue () > Currentnode.getvalue ()) { CurrentNode = Currentnode.getrightnode (); if (CurrentNode = = null) {Parentnode.setrightnode (newNode); return;}} else {//go left CurrentNode = Currentnode.getleftreenode (); if (CurrentNode = = null) {Parentnode.setleftreenode (NewNode); return;}}}} /** * Find * * @param key * @return */public TreeNode find (int key) {TreeNode CurrentNode = root;if (CurrentNode! = null) { while (Currentnode.getvalue ()! = key) {if (Currentnode.getvalue () > key) {currentnode = curRentnode.getleftreenode ();} else {CurrentNode = Currentnode.getrightnode ();} if (CurrentNode = = null) {return null;}} if (Currentnode.isdelete ()) {return null;} else {return currentnode;}} else {return null;}} /** * Middle Sequence traversal * * @param treeNode */public void inorder (TreeNode treeNode) {if (TreeNode! = null && treenode.isdelet E () = = False) {inorder (Treenode.getleftreenode ()); System.out.println ("--" + treenode.getvalue ()); Inorder (Treenode.getrightnode ());}}}

In the above two-fork tree traversal operation, the use of the middle sequence traversal, so that the data traversed by the sequence.

Here is the test code:

public class Main {public static void main (string[] args) {BinaryTree tree = new BinaryTree ();//Add Data Test Tree.insert (ten); Tre E.insert (Tree.insert); Tree.insert (3); Tree.insert (+); Tree.insert (+); Tree.insert (123); System.out.println ("root=" + tree.getroot (). GetValue ());//Sort Test Tree.inorder (Tree.getroot ());//Find Test if (Tree.find (10 ) = null) {System.out.println ("found");} else {System.out.println ("not Found");} Delete Test Tree.find (max). Setdelete (True); if (tree.find) = null) {System.out.println ("found");} else {System.out.println ("not Found");}}}



"Data structure and algorithm" Java implementation and characteristics summary of binary tree

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.