HOME

TheInfoList



OR:

: In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a ternary tree is a
tree data structure In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be conn ...
in which each node has at most three child
nodes In general, a node is a localized swelling (a "knot") or a point of intersection (a Vertex (graph theory), vertex). Node may refer to: In mathematics *Vertex (graph theory), a vertex in a mathematical graph *Vertex (geometry), a point where two ...
, usually distinguished as "left", “mid” and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Outside the tree, there is often a reference to the "root" node (the ancestor of all nodes), if it exists. Any node in the data structure can be reached by starting at root node and repeatedly following references to either the left, mid or right child. Ternary trees are used to implement
Ternary search tree In computer science, a ternary search tree is a type of trie (sometimes called a ''prefix tree'') where nodes are arranged in a manner similar to a binary search tree, but with up to three children rather than the binary tree's limit of two. Lik ...
s and
Ternary heap Ternary (from Latin ''ternarius'') or trinary is an adjective meaning "composed of three items". It can refer to: Mathematics and logic * Ternary numeral system, a base-3 counting system ** Balanced ternary, a positional numeral system, useful ...
s.


Definition

* Directed Edge - The link from the parent to the child. * Root - The node with no parents. There is at most one root node in a rooted tree. * Leaf Node - Any node that has no children. * Parent Node - Any node connected by a directed edge to its child or children. * Child Node - Any node connected to a parent node by a directed edge. * Depth - Length of the path from the root to the node. The set of all nodes at a given depth is sometimes called a level of the tree. The root node is at depth zero. * Height - Length of the path from the root to the deepest node in the tree. A (rooted) tree with only one node (the root) has a height of zero. In the example diagram, the tree has height of 2. * Sibling - Nodes that share the same parent node. - A node p is an ancestor of a node q if it exists on the path from q to the root. The node q is then termed a descendant of p. - A size of a node is the number of descendants it has including itself.


Properties of ternary trees

* Maximum number of nodes – Let h be height of a ternary tree. – Let M(h) be the maximum number of nodes in a ternary tree of height h – M(h) =1 + 3 + 9 + \cdots + 3^h = \sum_^h 3^i=\frac – Every tree of height h has at most \frac nodes. * If a node N occupies TREE /math>, then its Left Child is stored in TREE k-1/math>. * Mid Child is stored in TREE k/math>. * Right Child is stored in TREE k+1/math>.


Common operations


Insertion

Nodes can be inserted into ternary trees in between three other nodes or added after an
external node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be con ...
. In Ternary trees, a node that is inserted is specified as to which child it is.


External nodes

Say that the external node being added onto is node A. To add a new node after node A, A assigns the new node as one of its children and the new node assigns node A as its parent.


Internal nodes

Insertion on
internal node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be con ...
s is more complex than on external nodes. Say that the internal node is node A and that node B is the child of A. (If the insertion is to insert a right child, then B is the right child of A, and similarly with a left child insertion or mid child.) A assigns its child to the new node and the new node assigns its parent to A. Then the new node assigns its child to B and B assigns its parent as the new node.


Deletion

Deletion is the process whereby a node is removed from the tree. Only certain nodes in a ternary tree can be removed unambiguously.


Node with zero or one child

Say that the node to delete is node A. If a node has no children (
external node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be con ...
), deletion is accomplished by setting the child of A's parent to
null Null may refer to: Science, technology, and mathematics Computing * Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value * Null character, the zero-valued ASCII character, also designated by , often use ...
and A's parent to null. If it has one child, set the parent of A's child to A's parent and set the child of A's parent to A's child.


Comparison with other trees

The picture below is a
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
that represents 12 two-letter words. All nodes on the left child have smaller values, while all nodes on the right child have greater values for all nodes. A search starts from the root. To find the word "ON", we compare it to "IN" and take the right branch. Every comparison could access each character of both words. in / \ be of / \ / \ as by is or \ \ \ / \ at he it on to Digital search tries to store strings character by character. The next picture is a tree that represents the same set of 12 words; _ _ _ _ _ _ _ _ _ _ _ _ _ / / / \ \ \ / / / \ \ \ a b h i o t / \ / \ , / , \ /, \ , s t e y e n s t f n r o as at be by he in is it of on or to each input word is shown beneath the node that represents it. In a tree representing words of lower case letters, each node has 26-way branching. Searches are very fast: A search for "IS" starts at the root, takes the "I" branch, then the "S" branch, and ends at the desired node. At every node, one accesses an array element, tests for null, and takes a branch. i / , \ / , \ b s o / , \ / \ , \ a e h n t n t , \ , / \ , s y e f r o \ t The above picture is a balanced ternary search tree for the same set of 12 words. The low and high pointers are shown as angled lines, while equal pointers are shown as vertical lines. A search for the word "IS" starts at the root, proceeds down the equal child to the node with value "S", and stops there after two comparisons. A search for "AX" makes three comparisons to the first letter "A" and two comparisons to the second letter "X" before reporting that the word is not in the tree.Jon Bentley and Bob Sedgewick (1998), Dr. Dobb's Journal


Examples of ternary trees

*
Ternary search tree In computer science, a ternary search tree is a type of trie (sometimes called a ''prefix tree'') where nodes are arranged in a manner similar to a binary search tree, but with up to three children rather than the binary tree's limit of two. Lik ...
*
Ternary binary tree Ternary (from Latin ''ternarius'') or trinary is an adjective meaning "composed of three items". It can refer to: Mathematics and logic * Ternary numeral system, a base-3 counting system ** Balanced ternary, a positional numeral system, useful ...
*
Ternary heap Ternary (from Latin ''ternarius'') or trinary is an adjective meaning "composed of three items". It can refer to: Mathematics and logic * Ternary numeral system, a base-3 counting system ** Balanced ternary, a positional numeral system, useful ...
* Two infinite ternary trees containing all primitive Pythagorean triples are described in
Tree of primitive Pythagorean triples 500px, Berggrens's tree of primitive Pythagorean triples. In mathematics, a tree of primitive Pythagorean triples is a data tree in which each node branches to three subsequent nodes with the infinite set of all nodes giving all (and only) primit ...
and in
Formulas for generating Pythagorean triples Besides Euclid's formula, many other formulas for generating Pythagorean triples have been developed. Euclid's, Pythagoras', and Plato's formulas Euclid's, Pythagoras' and Plato's formulas for calculating triples have been described here: The me ...
. The root node in both trees contains triple ,4,5


See also

*
Binary tree In computer science, a binary tree is a k-ary k = 2 tree data structure in which each node has at most two children, which are referred to as the ' and the '. A recursive definition using just set theory notions is that a (non-empty) binary t ...
*
Tree structure A tree structure, tree diagram, or tree model is a way of representing the hierarchical nature of a structure in a graphical form. It is named a "tree structure" because the classic representation resembles a tree, although the chart is gener ...


References

{{Reflist Trees (data structures)