Editing Tree

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 15: Line 15:
 
Some trees are '''rooted''', that is, they have one particular vertex designated the '''root'''. Others are '''unrooted''', perhaps because no significance can be attached to singling out one vertex to be a root. Here are examples, that serve to introduce and explain terminology:
 
Some trees are '''rooted''', that is, they have one particular vertex designated the '''root'''. Others are '''unrooted''', perhaps because no significance can be attached to singling out one vertex to be a root. Here are examples, that serve to introduce and explain terminology:
 
* In an organic tree, there are several points at which the trunk and its branches divide. Let these, along with the ground, and the endpoints of the smallest branches, be vertices, with the vertex at the ground being labelled the root, for obvious reasons, and let the trunk and branches be the edges. The significance of the rooting is that the trunk and all its branches are considered to emanate from the root (which makes sense given how trees actually grow). The vertices corresponding to the endpoints of the smallest branches, which always have degree one, are called '''leaves''', in strict analogy. The distance from the root node to any other node is sometimes called that node's '''height''', again in strict analogy, but, due to the practice in computer science of drawing trees upside-down, it is more commonly called the '''depth'''. The maximum height of any node in the tree is known as the tree's height.
 
* In an organic tree, there are several points at which the trunk and its branches divide. Let these, along with the ground, and the endpoints of the smallest branches, be vertices, with the vertex at the ground being labelled the root, for obvious reasons, and let the trunk and branches be the edges. The significance of the rooting is that the trunk and all its branches are considered to emanate from the root (which makes sense given how trees actually grow). The vertices corresponding to the endpoints of the smallest branches, which always have degree one, are called '''leaves''', in strict analogy. The distance from the root node to any other node is sometimes called that node's '''height''', again in strict analogy, but, due to the practice in computer science of drawing trees upside-down, it is more commonly called the '''depth'''. The maximum height of any node in the tree is known as the tree's height.
* The ''family tree'' consisting of all descendants of an individual: This, too, is named for its resemblance to an organic tree, and it is no surprise that it can be placed into analogy with a graph-theoretic tree as well. Let each person in the family tree be a vertex and let there be an edge between two vertices if one of the corresponding individuals is a child of the other. Label the vertex corresponding to the aforementioned individual as the root vertex. Then, all adjacent vertices represent that individual's child, and, in strict analogy, these nodes are said to be '''children''' of the root node. In general, in a rooted tree, if two nodes are adjacent, the one further away from the root is considered to be a child of the one closer, and the one closer is called the '''parent''' of the one further away. (A node will always have exactly one parent, unless it is the root.) Furthermore, given some node <math>u</math>, the node itself, its children, its children's children, and so on, are called the node's '''descendants'''; the term ''proper descendant'' is sometimes taken to mean the same but excluding <math>u</math> itself. Likewise, <math>u</math>, <math>u</math>'s parent, <math>u</math>'s parent's parent, and so on, are called '''ancestors''' (with the term ''proper ancestor'' being defined analogously). The root is ancestral to all nodes.
+
* The ''family tree'' consisting of all descendants of an individual: This, too, is named for its resemblance to an organic tree, and it is no surprise that it can be placed into analogy with a graph-theoretic tree as well. Let each person in the family tree be a vertex and let there be an edge between two vertices if one of the corresponding individuals is a child of the other. Label the vertex corresponding to the aforementioned individual as the root vertex. Then, all adjacent vertices represent that individual's child, and, in strict analogy, these nodes are said to be '''children''' of the root node. In general, in a rooted tree, if two nodes are adjacent, the one further away from the root is considered to be a child of the one closer, and the one closer is called the '''parent''' of the one further away. (A node will always have exactly one parent, unless it is the root.) Furthermore, given some node <math>u</math>, the node itself, its children, its children's children, and so on, are called the node's '''descendants'''; the term ''strict descendant'' is sometimes taken to mean the same but excluding <math>u</math> itself. Likewise, <math>u</math>, <math>u</math>'s parent, <math>u</math>'s parent's parent, and so on, are called '''ancestors''' (with the term ''strict ancestor'' being defined analogously). The root is ancestral to all nodes.
 
* In the ''tree of life'', each vertex represents a species and edges represent evolution of one species into another. The root of this tree is some universal common ancestor of life on Earth, and the leaves of this tree are either extant species or species that have gone extinct without radiating or transitioning to a newer species.
 
* In the ''tree of life'', each vertex represents a species and edges represent evolution of one species into another. The root of this tree is some universal common ancestor of life on Earth, and the leaves of this tree are either extant species or species that have gone extinct without radiating or transitioning to a newer species.
 
* A corporate hierarchy can theoretically be represented by a rooted tree, where each employee is a vertex and a vertex's children are those vertices corresponding to the employees managed by subordinates, and the vertex corresponding to the CEO (or equivalent) is the root of the tree. Hierarchies in general are easily representable by trees.
 
* A corporate hierarchy can theoretically be represented by a rooted tree, where each employee is a vertex and a vertex's children are those vertices corresponding to the employees managed by subordinates, and the vertex corresponding to the CEO (or equivalent) is the root of the tree. Hierarchies in general are easily representable by trees.
Line 29: Line 29:
 
Tree '''traversal''' is the act of visiting every vertex of a tree. Traversing a tree produces an ordering of the tree's vertices. If a tree has <math>N</math> nodes, then there are <math>N!</math> different orderings, but two are particularly important: '''preorder''' and '''postorder'''. In a preorder traversal, we perform a [[depth-first search]] starting from the tree's root, and a node is considered to be visited as soon as it is first pushed onto the stack. A postorder traversal is similar, but now a node is considered to be visited only after all its children have been visited. (Leaf nodes are still considered visited as soon as they are pushed on the stack, as they have no children.) In some trees, an order is already imposed on the children of any given vertex; that is, a node with multiple children has a well-defined first child, second child, and so on. In these case, preorder and postorder traversals always visit a node's children in order, and are unique for a given rooted tree.
 
Tree '''traversal''' is the act of visiting every vertex of a tree. Traversing a tree produces an ordering of the tree's vertices. If a tree has <math>N</math> nodes, then there are <math>N!</math> different orderings, but two are particularly important: '''preorder''' and '''postorder'''. In a preorder traversal, we perform a [[depth-first search]] starting from the tree's root, and a node is considered to be visited as soon as it is first pushed onto the stack. A postorder traversal is similar, but now a node is considered to be visited only after all its children have been visited. (Leaf nodes are still considered visited as soon as they are pushed on the stack, as they have no children.) In some trees, an order is already imposed on the children of any given vertex; that is, a node with multiple children has a well-defined first child, second child, and so on. In these case, preorder and postorder traversals always visit a node's children in order, and are unique for a given rooted tree.
  
==Binary and ''k''-ary trees==
+
==Binary trees==
 
A '''binary tree''' is a rooted tree in which every vertex has at most two children, called ''left'' and ''right''. The subtree rooted at a node's left child is called the ''left subtree'', with ''right subtree'' defined analogously. Binary trees are of special interest because of their ability to organize data, as in [[binary search tree]]s and [[binary heap]]s.
 
A '''binary tree''' is a rooted tree in which every vertex has at most two children, called ''left'' and ''right''. The subtree rooted at a node's left child is called the ''left subtree'', with ''right subtree'' defined analogously. Binary trees are of special interest because of their ability to organize data, as in [[binary search tree]]s and [[binary heap]]s.
  
Line 43: Line 43:
  
 
A binary tree is said to be '''complete''' when levels 0 through <math>h-1</math> are completely filled, and all nodes on level <math>h</math> are as far to the left as possible. A complete binary tree will always have the minimum height possible. A binary tree is said to be '''balanced''' when all external nodes have similar height; if the heights of any two external nodes differ by at most one, it is said to be '''perfectly balanced'''. Balanced trees are useful for searching efficiently.
 
A binary tree is said to be '''complete''' when levels 0 through <math>h-1</math> are completely filled, and all nodes on level <math>h</math> are as far to the left as possible. A complete binary tree will always have the minimum height possible. A binary tree is said to be '''balanced''' when all external nodes have similar height; if the heights of any two external nodes differ by at most one, it is said to be '''perfectly balanced'''. Balanced trees are useful for searching efficiently.
 
A ''k''-ary tree is a rooted tree in which every vertex has at most <math>k</math> children. Similar terminology applies.
 
  
 
===Inorder traversal===
 
===Inorder traversal===
Line 72: Line 70:
 
* '''[[Lowest common ancestor]] (LCA) problem''': Given a pair of nodes in a tree, efficiently determine their lowest common ancestor, that is, the deepest node that is an ancestor of both given nodes. A variety of algorithms exist for this important problem.
 
* '''[[Lowest common ancestor]] (LCA) problem''': Given a pair of nodes in a tree, efficiently determine their lowest common ancestor, that is, the deepest node that is an ancestor of both given nodes. A variety of algorithms exist for this important problem.
 
* It is easy to find the ''distance'' between any pair of nodes in a tree, weighted or unweighted, because there is only one path to consider, which may be found using depth-first search or breadth-first search.
 
* It is easy to find the ''distance'' between any pair of nodes in a tree, weighted or unweighted, because there is only one path to consider, which may be found using depth-first search or breadth-first search.
:* To find the ''diameter'' of a tree, pick any starting vertex <math>u</math>, find the vertex <math>v</math> furthest away from <math>u</math> using DFS or BFS (breaking ties arbitrarily), and then find the vertex <math>w</math> furthest away from <math>v</math>. The distance between <math>v</math> and <math>w</math> is the tree's diameter.<ref name="diameter">Bang Ye Wu and Kun&ndash;Mao Chao. ''A note on Eccentricities, diameters, and radii''. Retrieved from http://www.csie.ntu.edu.tw/~kmchao/tree07spr/diameter.pdf</ref>
+
:* To find the ''diameter'' of a tree, pick any starting vertex <math>u</math>, find the vertex <math>v</math> furthest away from <math>u</math> using DFS or BFS (breaking ties arbitrarily), and then find the vertex <math>w</math> furthest away from <math>v</math>. The distance between <math>v</math> and <math>w</math> is the tree's diameter. ([[Tree/Diameter proof|proof]])
 
:* In the ''dynamic distance query'' problem, we wish to be able to efficiently determine the distances between pairs of nodes in a tree, but we also want to be able to change the weights of edges. This problem is solved using the [[heavy-light decomposition]].
 
:* In the ''dynamic distance query'' problem, we wish to be able to efficiently determine the distances between pairs of nodes in a tree, but we also want to be able to change the weights of edges. This problem is solved using the [[heavy-light decomposition]].
 
* The ''maximum matching problem'', ''minimum vertex cover problem'', ''minimum edge cover problem'', and ''maximum independent set problem'' all admit simple [[dynamic programming]] solutions when the graph involved is a tree.
 
* The ''maximum matching problem'', ''minimum vertex cover problem'', ''minimum edge cover problem'', and ''maximum independent set problem'' all admit simple [[dynamic programming]] solutions when the graph involved is a tree.
 
==References==
 
<references/>
 
  
 
[[Category:Graph theory]]
 
[[Category:Graph theory]]
 
[[Category:Pages needing diagrams]]
 
[[Category:Pages needing diagrams]]
[[Category:Trees]]
 

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)
Retrieved from "https://wcipeg.com/wiki/Tree"