3.8.4 Tree

Knowledge Base > Algorithm > Data Structure


Overview

A tree is a nonempty and recursively defined structure, consisted of vertices and edges. A vertex is a object that contains either a value or a pointer to a subtree or NULL, if it is a leaf node. An edge is a connection between two vertices, and a path is a list of distinct vertices in which successive vertices are connected by edges in the tree.

Each node in the tree can have zero (then is NULL) or more child nodes or descendent nodes. A node that has a child is called the parent node . If two nodes have the same parent, they are sibling nodes. The topmost node in a tree is called the root node, and which by definition, does not have parents. The root node is where the operations on the tree commonly begin. Nodes at the bottommost level of the tree are called leaf or external nodes, and they will not have any child nodes. An internal node is any node of a tree that has child nodes. A successor of a node x is the node with the smallest key greater than x, and a predecessor is the node with the largest key smaller than x. Every node in a tree can be seen as the root node of the subtree rooted at that node.

The number of children of a node x is the tree is called the degree of x. The height of a node is the length of the longest path to a leaf from that node. The depth of a node is the length of the path to the root. The height of a tree is equal to the largest depth of any node in the tree.

The idea of tree structure can help us understand and analyze recursive program as well as advanced data structure. Usually, operations in tree follows the divide-and-conquer mechanism (of method 3.7.3.1). There are many well-known sorting and graph algorithms that are built on tree structure.

In Graph theory, there are many different kinds of tree, where their mathematical properties may be slightly different.

Basic Operation

Binary Tree

A binary tree is defined to be an ordered tree, in which each internal node must contain two children. Here are the mathematical properties of a binary tree [2]:

Grammar 3.8.4.1. Backus-Naur Form for Binary Tree

<bintree>::=<null>
::=< <symbol> <number> <bintree> <bintree> >

Source Code

Here is the Binary Tree implementation, written in Scheme:

Related Topics