Docsity

Binary Trees: Definition, Properties, and Traversals, Study notes of Computer Science

Partial preview of the text

Download Binary Trees: Definition, Properties, and Traversals and more Study notes Computer Science in PDF only on Docsity!

CHAPTER 10

BINARY TREES

A. Tree Terminology

Defn: A tree can be defined in one of the following different, but equivalent, manners:  a connected graph with no cycles.  a directed acyclic graph with one node from which all other nodes are reachable. Example: A B C D E F G

A B C^ D E F G Defn: In a tree, the node from which all other nodes are reachable is called the root. Defn: The children of a node X are all of the nodes you can access from X by following one edge. Defn: The parent of a node X is the node of which X is a child. Defn: The siblings of a node X are all other children with the same parent as X. Defn: The descendants of a node X are all of the nodes reachable from X.

B. Binary Trees

Defn: A binary tree is a tree in which every node has at most two children. Example: A B C D (^) E F G H Defn: A strictly binary tree is a tree in which every node has either 0 or 2 children.

Defn: A binary tree of depth k is called full if it contains all possible nodes at depth k. Example: A full tree of depth 2 A B C D (^) E F G H Defn: A complete binary tree is an almost full binary tree except that it may be missing nodes at the right-hand side of the lowest level. Example: A complete tree A B C D (^) E F

C. Binary Tree Traversals

Defn: A tree traversal is a method of traveling from node to node through a tree, visiting every node. There are three types of traversals :

1. Preorder preorder (node): visit the node preorder(node’s left subtree) preorder(node’s right subtree) 2. Inorder inorder (node): inorder(node’s left subtree) visit the node inorder(node’s right subtree) 3. Postorder postorder (node): postorder(node’s left subtree) postorder(node’s right subtree) visit the node

Example: Tree Traversals A B C D (^) E F G Preorder: A, B, D, E, G, C, F Inorder: D, B, G, E, A, C, F Postorder: D, G, E, B, F, C, A