Tree & two fork Tree

Source: Internet
Author: User

(Haverman, Havermann coding, sorting binary tree, balanced binary tree, red black tree, 3 traversal (first order, post, middle order), depth-breadth-first traversal)

Keywords, statements:

    1. Three storage structures for trees: parent node notation, child node notation, linked list storage
    2. The tree represents a nonlinear data structure
    3. If there is a complex one-to-many association between a set of array nodes, the program can consider using a tree to hold this set of data
    4. A pair of more? For example, a parent node can contain multiple child nodes
    5. TreeMap is the use of red and black trees to achieve

Tree: A collection (there are N nodes with a parent-child relationship, n finite, that is, a finite set)

Meet the conditions (please recite down):

A. When n=0, the node collection is empty, called the "Empty Tree"

B. In any non-empty tree, there is only one root node

C. n>1, in addition to the root node, the remaining nodes can be divided into M-associative subsets, called the root of the subtree (subtree)(recursive)

Note: The root node has no parent node, and the leaf node has no child nodes. (except for the root node, all nodes have a unique parent node)

Graphic:

Terminology: (memorize)
   Node: Contains data fields and pointer (reference) fields
   Degree of node: number of sub-trees of the node
   The degree of the tree: the maximum value of all node degrees
   Leaf nodes: Nodes with a degree of 0
   Branch node: A node with a degree of not 0
   Sibling node: a node with the same parent node
   The layer of the node: the more hierarchy is 1, the remaining nodes are added in turn by 1
   Tree Depth: The maximum level of the node
   Ordered tree: Tree nodes from left to right are ordered and cannot be exchanged (distinguish between
   Ancestor node: All nodes from the root to the branch that the node is on
   Descendant nodes: Any node in a subtree that is followed by a node
   Forest: 2 or more than 2 sets of disjoint trees, deleting the root of a tree and getting a forest
Common operations for Trees:
   1. Initialize: Specifies that the constructor creates an empty tree, or creates a tree with the specified node as the root
   2. Adding child nodes for the specified node
   3. Determine if the tree is empty
   4. Return to the root node
   5. Return the parent node of the specified node (not the root)
   6. Return all child nodes of the specified node (non-leaf)
   7. Returning the sub-node of the specified node (not the leaf)
   8. Returns the depth of the tree (the parent class is found from the node in the node array)
   9. Return the location of the specified node

Tree implementation (that is, the relationship between parent and child nodes)

Parent node notation: Each child node records its parent node (a bit like an infinite class classification in the database)----the child to remember the father (the child has only one father, 1 to remember one)

Child node notation: each non-leaf node records all of its child nodes through a linked list------father remember the child (father can have multiple children, 1 to remember multiple)

Detailed Description:

1. Parent node notation:

A. Add a parent field for each of the points to hold a reference or index number

Array index

Data

Parent

0 A -1
1 B 0
2 C 0
3 D 1

Code implementation:

Importjava.util.ArrayList;Importjava.util.List; Public classTreeparent<e> {     Public Static classNode<t>{T data; intParent//Record parent Node                 PublicNode () {} PublicNode (T data) { This. data =data; }         PublicNode (T data,intparent) {             This. data =data;  This. Parent =parent; }                 PublicString toString () {return"Treeparent--node[data=" +data+ ", parent=" +parent+ "]"; }    }    Private Final intdefault_tree_size = 100; Private inttreeSize = 0;//The capacity of the array that holds the nodes (the capacity of the tree)    Private intNodenums;//Current number of nodes        PrivateNode[] nodes;//record all the nodes     PublicTreeparent (E data)//to create a tree with the specified root node{treeSize=default_tree_size; Nodes=NewNode[treesize]; nodes[0] =NewNode<e> (data,-1); Nodenums++;//Increased number of current nodes    }     PublicTreeparent (E data,intTreeSize)//specify the capacity of the tree    {         This. treeSize =treeSize; Nodes=NewNode[treesize]; nodes[0] =NewNode<e> (data,-1); Nodenums++;//Increased number of current nodes    }     Public voidAddNode (E data,node parent)//Add a child node for the specified node, note that node is passed in, and node is static    {        //first, determine if the tree is full, and there are no null elements in the array                 for(inti = 0; i < treeSize; i++)        {            if(Nodes[i] = =NULL)//finds the first NULL element in the array that holds the new node            {                //creates a new node and saves it with the specified array elementNodes[i] =NewNode (data, POS (parent));//Note that the static inner class is instantiated herenodenums++; return; }        }        Throw NewRuntimeException ("The tree is full, unable to add a new node"); }    Private intPOS (Node Parent)//returns the index of the specified node    {         for(inti = 0; i < treeSize; i++)        {            //Locate the specified node            if(Nodes[i] = =parent) {                returni; }        }        return-1; }     Public BooleanEmpty ()//Determines whether the tree is empty.     {        returnNodes[0] = =NULL;//whether the root node is null    }     PublicNode<e> Root ()//returns the root node    {        returnNodes[0];//returns the root node    }     PublicNode<e> Parent (Node node)//returns the parent node of the specified node (not the root node)    {        //The parent of each node records the location of its parents        returnNodes[node.parent]; }     PublicList<node<e>> Children (Node Parent)//returns all child nodes of the specified node (non-leaf node){List<Node<E>> list =NewArraylist<node<e>>();  for(inti = 0; i < treeSize; i++)        {            //If the position of the parent node of the current node equals the location of the parent node            if(Nodes[i]! =NULL&&nodes[i].parent==POS (parent))            {List.add (nodes[i]); }        }        returnlist; }     Public intDeep ()//returns the depth of the tree-from each node in the node array, constantly looking for the parent node, adding 1    {                intmax = 0;//to record the maximum depth of a node         for(inti = 0; I < treeSize && Nodes[i]! =NULL; i++)        {            intdef = 1;//Initialize the depth of this node            intm = nodes[i].parent;//m Records the location of the parent node of the current node             while(M! =-1 && nodes[m]! =NULL)//if the parent node exists{m= Nodes[m].parent;//continue searching parent node updef++; }            if(Max <def) {Max=def; }        }        returnMax//return Maximum Depth    }    /*Public static void Main (string[] args) {treeparent<string> tp = new treeparent<string> ("root")        ;        Treeparent.node root = Tp.root ();        System.out.println (root);        Tp.addnode ("Node 1", root);        System.out.println ("Depth of this tree:" + tp.deep ());        Tp.addnode ("Node 2", root);        Gets all child nodes of the root node list<treeparent.node<string>> nodes = Tp.children (root);        System.out.println ("The first child node of the root node:" + nodes.get (0));        Add a child node Tp.addnode ("Node 3", Nodes.get (0)) to the first child node of the root node;    System.out.println ("Depth of this tree:" + tp.deep ()); }*/}

Special attention:

1. The depth of the time, is from the child node to start up step by step to find.

But to go through each of the nodes in the group, when looking for nodes that have previously appeared, you may have looked for the nodes behind, and then looked again and again.

2. public static class node<t> the "static internal classes" appear here, as well as the instantiation of the operation (when adding nodes)

Supplemental Knowledge: Static class instantiation

Http://www.2cto.com/kf/201304/206692.html

http://kenby.iteye.com/blog/1603803

In another article in this blog, I have organized the above links: a summary of internal classes

To be continued//

Tree & two fork 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.