Lapping data structures and algorithms-basic operation of 112-fork Tree

Source: Internet
Author: User

Node:

/*

* Binary tree node

*/

public class Node {

Data item

public Long data;

Data item

Public String SData;

Left Dial Hand node

Public Node Leftchild;

Right child node

Public Node Rightchild;

/**

* Construction Method

* @param data

*/

Public Node (Long data,string sData) {

This.data = data;

This.sdata = SData;

}

}

Two-fork Tree:

/*

* Two-fork Tree class

*/

public class Tree {

Root node

public Node root;

/**

* Insert Node

* @param value

*/

public void Insert (long value,string svalue) {

Encapsulating nodes

Node NewNode = new node (value,svalue);

Referencing the current node

Node current = root;

Reference parent Node

Node parent;

If root is null, which is the first insertion

if (root = = null) {

root = NewNode;

Return

} else {

while (true) {

Parent node points to current node

parent = current;

If the node data that you are pointing to is larger than the insert, go left

if (Current.data > value) {

current = Current.leftchild;

if (current = = null) {

Parent.leftchild = NewNode;

Return

}

} else {

current = Current.rightchild;

if (current = = null) {

Parent.rightchild = NewNode;

Return

}

}

}

}

}

/**

* Find Nodes

* @param value

*/

Public Node find (Long value) {

Reference the current node, starting at the root node

Node current = root;

Loop, as long as the lookup value is not equal to the current node's data item

while (current.data! = value) {

Compare the lookup value and the size of the current node

if (Current.data > value) {

current = Current.leftchild;

} else {

current = Current.rightchild;

}

If you do not find the

if (current = = null) {

return null;

}

}

return current;

}

/**

* Delete Node

* @param value

*/

public void Delte (Long value) {

}

}

Test:

public class Testtree {

public static void Main (string[] args) {

Tree tree = new Tree ();

Tree.insert ("James");

Tree.insert ("YAO");

Tree.insert ("Kobi");

Tree.insert (3, "Mac");

System.out.println (Tree.root.data);

System.out.println (Tree.root.rightChild.data);

System.out.println (Tree.root.rightChild.leftChild.data);

System.out.println (Tree.root.leftChild.data);

Node node = tree.find (3);

System.out.println (Node.data + "," + node.sdata);

}

}


This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696554

Lapping data structures and algorithms-basic operation of 112-fork 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.