Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Overview 10.1 10.2 Introduces tree terminology Discusses the implementation of binary trees 10.2 Traversing trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees Binary tree Empty or, A node with a left and right subtree. Each of these subtrees is itself a binary tree. Children The nodes directly below a node In a binary tree, no node can have more than two children.
Binary Trees Parent Siblings Root Every node in a binary tree has exactly once parent, except for one a the top. Siblings Nodes with the same parent. Root Node at the top of a tree.
Binary Trees Leaves Internal nodes Depth Nodes with no children. Nodes that are not leaves. Depth Number of lines along the path back to the root. Root is a depth 0.
Binary Trees Level Height A level of the tree is the set of nodes at a particular depth. Height The height of a tree is the depth of the deepest node.
Binary Trees
Binary Trees
Binary Trees Descendants Proper descendants A node's descendants are itself, its children, their children, and so on. Every node is a descendant of the root. Proper descendants All of it descendants except itself.
Binary Trees Ancestors Proper ancestors Itself, its parents, its grandparent, and so on. Proper ancestors All of its ancestors except itself.
Binary Trees The number of nodes in a binary tree depends on the height of the tree and on how “skinny” or “busy” the tree is. Linear tree Every internal node has only one child. Perfect or Complete All of the leaves are at the same depth. Every internal node has exactly two children.
Binary Trees The number of nodes in a binary tree of height h can be any between h + 1 (for a linear tree). (for a perfect binary tree)
Binary Trees A binary tree with n nodes log2(n + 1) – 1 (for a perfect binary tree) n-1 (for a linear tree) h Θ(log n) for a perfect binary trees.
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
Tree Traversal Four meaningful orders in which to traverse a binary tree. Preorder Inorder Postorder Level order
Tree Traversal
Tree Traversal
Tree Traversal
Tree Traversal
Tree Traversal
Tree Traversal
Tree Traversal Level order traversal is sometimes called breadth-first. The other traversals are called depth-first. Traversal takes Θ(n) in both breadth-first and depth-first. Memory usage in a perfect tree is Θ(log n) in depth-first and Θ(n) in breadth-first traversal.
Tree Traversal
Summary A tree is a branching, hierarchical structure. A binary tree either is empty or consists of a node, a left subtree, and a right subtree. A general tree (which cannot be empty) consists of a node and zero or more subtrees.
Summary In the widest possible binary tree, called a perfect tree, the number of nodes is exponential in the height of the tree. The height of such a tree is logarithmic in the number of nodes.
Summary Binary trees can be traversed Preorder Inorder Postorder Level order The first three have very elegant recursive algorithms, but level order traversal requires a queue.