Two-fork Tree

Source: Internet
Author: User

Binary tree definition: The two fork tree is a set of N (n>0) finite nodes. N=0 tree becomes i empty binary tree; The tree of n>0 has a root node and two sub-binary trees, called Saozi right subtree, respectively, which means that each node is either a left or right tree or a left tree, or a subtree.

A binary tree is an ordered tree, and a binary tree is a Zuozi or a right subtree even when there is only one subtree.

Full two fork tree: All branches in the binary tree have Saozi right subtree, and all the leaf nodes are on the same level, that is, two fork trees.

Complete binary tree: If there are n nodes two tree structure and the structure of the top N nodes full two fork tree is the same such a two-fork tree is called a complete binary tree.

Abstract data types for binary trees:

Data collection: Two node set of the fork tree.

Operation Collection:

Create a two-fork tree.

Undo Two Fork Tree:

Left Insert node: Insertleftnode (curr,x); If the current node is not empty, insert the left node in the Curr, and the original left node is the left node of the new node.

Right Insert node: Insertleftnode (curr,x); If the current node is not empty, insert the right node in the Curr and the original left node is the right node of the new node

Left Delete subtree: Deletelefttree (curr) If Curr is non-empty, delete the left subtree of the Curr point;

Left Delete subtree: Deletelefttree (curr) If Curr is non-empty, delete the right subtree of the curr point;

Traversing the binary tree: Traveerse (t,visit ()) If the binary tree T is not empty, it accesses each node of the binary tree T in a certain order, and each node accesses only the operation to be implemented with the Access node in turn visite operation. The traversal order of binary tree has the first order traversal, the middle sequence traversal, the post-order traversal, the level traversal four kinds.

The nature of the binary tree:

Property 1: If the specified junction level is 0, then the first layer of a maximum of only 2i nodes

A 20=1

B C 21=2

D E F G 22=4

Feature 2: If the degree of the specified empty tree is-1, then the depth of the K two fork tree up to 2k+1-1 nodes, in fact, full tree node is 20+21+ .... 2n=2n+1-1;

Feature 3: Full binary tree with N nodes with a maximum depth of k=log2 (n+1)-1

2k-1<n<2k+1-1

K<LOG2 (n+1) <k+1

K must be an integer

For a non-empty binary tree, the N=n0 (the number of degrees is 0 nodes) +n1 (the degree of 1 nodes) +n2 (the number of 2 nodes);

Number of branches b=n-1; a node with a degree of 1 emitting a branch with a node of degree 2 emitting two branches

B=N1 (the number of degrees is 1 nodes) +2*n2 (the degree of 2 nodes);

n0=n2+1;

Property 5:

A two-fork tree with n nodes is sorted from the top down, from left to right, from 0.

When i>0, the parent node is (i-1)/2 (divisible, that is, the number of the left sub-tree divided) when i=0, I is the heel node.

When 2i+1<n, the number of the left Dial hand node of the node is numbered, and when the 2i+1>n is not a child left.

When 2i+2<=n, the number of the right child node of the node is numbered, and the 2i+2>n has no right left

A (k=0,i=0) 2 of 0 Square =1

B (K=1,i=1) C (k=1,i=2) 2 One-time Square =2

D (k=2,i=3) E (k=2,i=4) F (k=2,i=5) G (k=2,i=6) 2 two-time Square =4
(k=3,i=7) i=8 i=9,i=10 i=11,i=12 i=13,i=14
n = 2 of k+1 -1= 2 of 3 -1=7
Ordinal i= starting from 0, the k+1 of the maximum sequence number 2-2

A K-tier has a maximum of 2 K-th square nodes.
Then the smallest i = 2 of the k+1-1-2 of the K-th-square
Maximum number i = 2 of k+1-2

Up to 2 (K-1) sub-nodes in the previous layer of a K-layer
(left) the smallest i = 2 of the K-th-1-2 (K-1) sub-square
(right) the largest number i = 2 of the K-th square-2

(2 of K-th-1-2 of the K-1) +1 = 2 of the k+1-2-2 of the K-th square is not likely to be greater than 2 of the k+1-1-2 of the K-square

Binary Tree Characteristics Summary:

1: Specify a root node level of 0, then I layer up to 2 i-th node

2: Specifies that the depth of the empty tree is-1, then the maximum number of nodes with a depth of K is 2 k+1 minus 1 nodes.

3: Maximum depth of a complete binary tree with N nodes log2 (n+1)-1

4:n=n0 (the number of degrees is 0 nodes) +n1 (degree 1 nodes) +n2 (degree 2 nodes); n0=n2+1;

5: If specified from top to bottom, from left to right, sorted from 0

When i=0 is the root node, i>0,i minus one divided by 2 is the parent node, and the child node is the multiple of the parent node.

When 2*i+1 < n is the child node of this node, when 2*i+1 > N No left child node

When 2*i+2<= n is the right child node of this node,

The chain-structured implementation of the binary tree:

Binary tree sorting can be calculated according to the characteristics of the binary tree.

The sequential storage structure of binary tree is not suitable for storing incomplete binary tree, which can cause a lot of wasted space.

Binary tree chain storage structure:

Binary tree chain storage structure: the relationship between nodes in a two-fork tree is established with pointers. The most commonly used chain storage structure of a binary tree is a two-fork chain. Each node of the binary chain storage structure contains three fields, a data field, a left child pointer field, and a right child pointer field.

The chain-structured implementation of the binary tree:

<?php
/**
*
* Basic realization of binary tree
*/

/**
* Define the node data type.
*/

Class Node {
Public $content; Data fields
public $leftnext;//left child
public $rightnext;//Right Child
}

Class Three {
Public $root;
/**
* Initialization
*/
Public function __construct ($content) {
$this->root = new Node;
$this->root->content = $content;
$this->root->leftnext = null;
$this->root->rightnext = null;
}

/**
* Insert left child node.
*/
Public Function Insertleftnode ($curr, $content) {

if ($curr = = null) return null;
if ($curr->leftnext = = null) {
$node = new node;
$node->content = $content;
$node->leftnext = null;
$node->rightnext = null;
$curr->leftnext = $node;
} else {
$left = $curr->leftnext;
$node = new node;
$node->content = $content;
$node->leftnext = $left;
$node->rightnext = null;
$curr->leftnext = $node;
}
return $curr->leftnext;
}

/**
* Insert right child node.
*/
Public Function Insertrightnode ($curr, $content) {

if ($curr = = null) return null;
if ($curr->rightnext = = null) {
$node = new node;
$node->content = $content;
$node->leftnext = null;
$node->rightnext = null;
$curr->rightnext = $node;
} else {
$right = $curr->rightnext;
$node = new node;
$node->content = $content;
$node->leftnext = null;
$node->rightnext = $right;
$curr->leftnext = $node;
}
return $curr->rightnext;
}

/**
* Delete left node.
*/
Public Function Deleteleftnode ($curr) {
if ($curr = = NULL | | $curr->leftnext = = null) return null;
$curr->leftnext = = null;
return $curr;
}

/**
* Delete Right child node
*/
Public Function Deleterightnode ($curr) {
if ($curr = = NULL | | $curr->rightnext = = null) return null;
$curr->rightnext = = null;
return $curr;
}

/**
* Traverse the binary tree
* Recursive method of pre-sequence traversal method
* Method Steps:
* Traverse from the node.
* Traverse left node.
* Traverse right node.
* No traversal for NULL
*/
Public Function Listthree ($curr) {
if ($curr = = null) return;
Static $vlist = Array ();
$vlist [] = $curr->content;

if ($curr->leftnext! = null) {

$this->listthree ($curr->leftnext);
}
if ($curr->rightnext! = null) {
$this->listthree ($curr->rightnext);
}
return $vlist;
}


/**
* Traverse the binary tree
* Recursive method of middle sequence traversal method
* Method Steps:
*
* Traverse left node.
Follow the knot.
* Traverse right node.
* No traversal for NULL
*/

Public Function Inlistthree ($curr) {
Static $vlist = Array ();
if ($curr! = null) {
$this->inlistthree ($curr->leftnext);
$vlist [] = $curr->content;
$this->inlistthree ($curr->rightnext);
}
return $vlist;
}

/**
* Recursive method of hierarchical sequential traversal method
* Method Steps:
* Deposit with Knot point
* Access to left dial hand nodes.
* Access right child node

*/
Public Function Arrthree ($curr) {
if ($curr = = null) return null;
Static $vlist = Array ();
$vlist [0] = $this->root->content;
if ($curr->leftnext) $vlist [] = $curr->leftnext->content;
if ($curr->rightnext) $vlist [] = $curr->rightnext->content;
$this->arrthree ($curr->leftnext);
$this->arrthree ($curr->rightnext);
return $vlist;
}
}

$three = new Three (' a ');
$left = $three->insertleftnode ($three->root, ' B ');
$right = $three->insertrightnode ($three->root, ' C ');
$three->insertleftnode ($left, ' d ');
$three->insertrightnode ($left, ' e ');

$three->insertleftnode ($right, ' f ');
$three->insertrightnode ($right, ' G ');
Print_r ($three->listthree ($three->root)); Array ([0] = a [1] = b [2] = + d [3] = = e [4] = c [5] + f [6] = g)
Print_r ($three->arrthree ($three->root)); Array ([0] = a [1] = b [2] = = c [3] = + d [4] = e [5] + f [6] = g)

Two-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.