190 likes | 300 Views
Trees. Binary trees, binary search trees, balanced trees. A tree is a structure that provides a mechanism to store information in a hierarchical manner rather than in a linear manner. There is a parent-child relationship between nodes on a tree.
E N D
Trees Binary trees, binary search trees, balanced trees
A tree is a structure that provides a mechanism to store information in a hierarchical manner rather than in a linear manner. There is a parent-child relationship between nodes on a tree. A tree is either empty, or consists of a root node R and zero, or more, subtrees whose root nodes are children of R. R root nodes of subtrees of R
Example: The structure of a directory system, say in Unix Example: The parse trees for expressions we have been looking at. If a node x is directly connected to a node y where y is higher in the tree than x, than y is called the parent of x, and x is called a child of y. If n1 , n2 , n3 , . . . nk is a sequence of nodes in a tree so that ni is the parent of ni+1 , that is, ni is higher in the tree than ni+1 , then this sequence of nodes is called a path. The length of the path is one less than the number of nodes in the sequence, k – 1. n1 is an ancestor of nk . nk is a descendent of n1 . A branch is a path that begins at the root node.
n1 n2 n3 n4 n5
The root node is the only node with no parent. A node with no children is called a leaf node. A tree is ordered if the position of the nodes in the tree is relevant. Otherwise the tree is unordered. These are the same tree if the tree is unordered, but distinct if the tree is ordered. In general trees are ordered. a a b c c b
A binary tree is either empty or consists of a root node and two dsjoint binary subtrees, called a left subtree and a right subtree. A binary tree is an ordered tree. This is a recursive definition; most results involving binary trees can be most easily accomplished using recursion. Example: a b c d e f g h i j
Another way to define a binary tree is: A binary tree is either empty, or one in which each node has at most two children, a left-child, and a right-child. The level of a node in a tree is the length of a branch to that node, also called the depth of the node. More formally, for any node x on a tree if x is the root node level (x) = 0 else level (x) = 1 + level ( parent of x)
The height of a non-empty tree is the length of the longest branch in the tree. Or, more formally, For any tree T if T is empty height (T) = -1 else height (T) = 1 + max (height (left subtree), height (right subtree))
Example level 0 level 1 level 2 level 3 level 4 A is the root node; nodes d,h, and i are leaf nodes. The height of the tree is 4. a b c d e f h g i
A binary tree T is a two-tree if every non-leaf node of T has exactly two children. Or, more formally, A binary tree is a two-tree if T is empty or both the left and right subtrees of T are two-trees. The tree in the previous example is not a two-tree – nodes c, e, f, and g are not leaf nodes but do not have two children.
A binary tree is full if all leaf nodes are at the same level. Or, more formally, a binary tree T is full if T is empty or if the left and right subtrees of T have the same height, and are full. Obvious result: If a binary tree T is full, then it is a two-tree (with all leaf nodes at the same level).
Example of a full binary tree. All leaf nodes are at level 3.
A binary tree T is a complete tree if it is full up through the level height(T) –1, and all nodes at the lowest level are as far to the left as possible. Example of a complete binary tree.
An important operation on a binary tree is a traversal – to visit each of the nodes. There are three types of traversals: An in-order traversal: visit the left subtree visit the root node visit the right subtree In the above tree an in-order traversal yields the nodes b a c a c b
An pre-order traversal: visit the root node visit the left subtree visit the right subtree In the above tree an pre-order traversal yields the nodes a b c a c b
A post-order traversal of a binary tree: visit the left subtree visit the right subtree visit the root node In the above tree an post-order traversal yields the nodes b c a. a c b
a b c d e f h g i An in-order traversal of this tree yields: d b g i e a c f h A pre-order traversal of this tree yields: a b d e g i c f h A post-order traversal of this tree yields: d i g e b h f c a
a b c d e f g h i j An in-order traversal of this tree yields: d b h e a f c i g j A pre-order traversal of this tree yields: a b d e h c f g i j A post-order traversal of this tree yields: d h e b f i j g c a