1 / 19

Trees

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.

edward
Télécharger la présentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees Binary trees, binary search trees, balanced trees

  2. 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

  3. 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.

  4. n1 n2 n3 n4 n5

  5. 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

  6. 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

  7. 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)

  8. 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))

  9. 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

  10. 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.

  11. Examples of two-trees:

  12. 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).

  13. Example of a full binary tree. All leaf nodes are at level 3.

  14. 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.

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

More Related