Introduction and implementation of tree, binary tree and binary search tree

Source: Internet
Author: User
Tags data structures empty

Characteristics and definitions of trees

A tree is a collection of elements. Let's first introduce the tree in a more intuitive way. The following data structure is a tree:

A tree has multiple nodes (node) that are used to store elements. Each node can have multiple child nodes (children), and the node is the parent of the corresponding child node. For example, 3,5 is a child of 6, 6 is the parent of the 3,5, 1,8,7 is a child of 3, and 3 is the parent node of 1,8,7. The tree has a node that does not have a parent node, called the root node (root), as shown in Figure 6. Nodes that do not have child nodes are called leaf nodes (leaf), such as the 1,8,9,5 node in the graph. As you can see from the diagram, there are 4 levels of the tree above, 6 in the first layer and 9 in the fourth layer. The maximum level of a node in a tree is called depth. In other words, the depth of the tree (depth) is 4.

If we look down from node 3, we're actually a tree with node 3 as the root node:

The triangle represents a tree.

Further, if we define an isolated node as a tree, the original tree can be expressed as a relationship between the root node and the subtree (subtree):

The above observations actually give us a strict method of defining the tree:

1. A tree is a collection of elements.

2. The collection can be empty. When there are no elements in the tree, we call the tree empty (empty tree).

3. If the collection is not empty, then the collection has a root node and 0 or more subtrees. The root node is connected to the root node of its subtree with one edge (edge).

The 3rd above is a recursive way to define the tree, which is to use the tree itself (subtree) in the process of defining the tree. Due to the recursive characteristics of the tree, many tree-related operations can also be conveniently implemented using recursion. We'll see in the back.

(The above definition comes from "Data structures and algorithm analysis in C, by Mark Allen Weiss".) I think there's a little bit less stringent here. If the empty tree belongs to the tree, the 3rd should be "... and 0 and more nonempty trees ... ")

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.