Binary Trees
Introduction A tree structure means that the data are organized so that items of information are related by branches Examples:
Parts of a binary tree A binary tree is composed of zero or more nodes Each node contains: A value (some sort of data item) A reference or pointer to a left child (may be null), and A reference or pointer to a right child (may be null) A binary tree may be empty (contain no nodes) If not empty, a binary tree has a root node Every node in the binary tree is reachable from the root node by a unique path A node with neither a left child nor a right child is called a leaf In some binary trees, only the leaves contain a value
Terminology Some node: the item of information plus the branches to each node. degree: the number of sub trees of a node degree of a tree: the maximum of the degree of the nodes in the tree. terminal nodes (or leaf): nodes that have degree zero nonterminal nodes: nodes that don’t belong to terminal nodes. children: the roots of the sub trees of a node X are the children of X parent: X is the parent of its children.
Terminology siblings: children of the same parent are said to be siblings. Ancestors of a node: all the nodes along the path from the root to that node. The level of a node: defined by letting the root be at level one. If a node is at level l, then it children are at level l+1. Height (or depth): the maximum level of any node in the tree
Picture of a binary tree d e g h i l f j k
Size and depth The size of a binary tree is the number of nodes in it This tree has size 12 The depth of a node is its distance from the root a is at depth zero e is at depth 2 The depth of a binary tree is the depth of its deepest node This tree has depth 4 a b c d e f g h i j k l
Balance a b c d e f g h i j A balanced binary tree a b c d e f g h i j An unbalanced binary tree A binary tree is balanced if every level above the lowest is “full” (contains 2n nodes) In most applications, a reasonably balanced binary tree is desirable
Example Property: (# edges) = (#nodes) - 1 A is the root node B is the parent of D and E C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The level of E is 3 The height (depth) of the tree is 4 The degree of node B is 2 The degree of the tree is 3 The ancestors of node I is A, C, H The descendants of node C is F, G, H, I Property: (# edges) = (#nodes) - 1 A B C H I D E F G Level 1 2 3 4
Example of a Tree No. of nodes = 9 Height = 4 Highest Level = 3 5/16/2019 Example of a Tree No. of nodes = 9 Height = 4 Highest Level = 3 Root Node = 8 Leaves = 1,4,7,13 Interior Nodes = 3,10,6,14 Ancestors Of 6 = 3,8 Descendents Of 10 = 14,13 Sibling Of 1 = 6
Full Binary Tree and Complete Binary Tree
Traversal A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. We will consider several traversal algorithms with we group in the following two kinds Depth-first traversal. Breadth-first traversal . There are three different types of depth-first traversals : PreOrder traversal - visit the parent first and then left and right children; InOrder traversal - visit the left child, then the parent and the right child; PostOrder traversal - visit left child, then the right child and then the parent; There is only one kind of breadth-first traversal--the level order traversal. This traversal visits nodes by levels from top to bottom and from left to right.
As an example consider the following tree and its four traversals: PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
Binary Tree Traversals Inorder traversal (LVR) (recursive version) output: A / B * C * D + E ptr L V R
Binary Tree Traversals Preorder traversal (VLR) (recursive version) output: + * * / A B C D E V L R
Binary Tree Traversals Postorder traversal (LRV) (recursive version) output: A B / C * D * E + L R V