Binary Trees Introduction n n A tree structure
Binary Trees
Introduction n n A tree structure means that the data are organized so that items of information are related by branches Examples:
Parts of a binary tree n n A binary tree is composed of zero or more nodes Each node contains: n n n A binary tree may be empty (contain no nodes) If not empty, a binary tree has a root node n n 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) 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 n In some binary trees, only the leaves contain a value 3
Terminology n Some node: the item of information plus the branches to each node. n degree: the number of sub trees of a node n degree of a tree: the maximum of the degree of the nodes in the tree. n terminal nodes (or leaf): nodes that have degree zero n nonterminal nodes: nodes that don’t belong to terminal nodes. n children: the roots of the sub trees of a node X are the children of X n parent: X is the parent of its children.
Terminology n n 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 a b d g c e h f i j k l 6
Size and depth n a b d The size of a binary tree is the number of nodes in it n c n e f The depth of a node is its distance from the root n g h i j k n n l This tree has size 12 is at depth zero e is at depth 2 a The depth of a binary tree is the depth of its deepest node n This tree has depth 4 7
Balance a a b d c e f b c g h i j A balanced binary tree d e f g h i j An unbalanced binary tree n n A binary tree is balanced if every level above the lowest is “full” (contains 2 n nodes) In most applications, a reasonably balanced binary tree is desirable 8
Example A is the root node Property: (# edges) 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 B The ancestors of node I is A, C, H The descendants of node C is F, G, H, I = (#nodes) - 1 Level A 1 C 2 H D E F G I 3 4
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 : n Pre. Order traversal - visit the parent first and then left and right children; n In. Order traversal - visit the left child, then the parent and the right child; n Post. Order 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: Pre. Order - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 In. Order - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 Post. Order - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
Binary Tree Traversals n Inorder traversal (LVR) (recursive version) output: A / B * C * D + E ptr L V R
Binary Tree Traversals n Preorder traversal (VLR) (recursive version) output: + * * / A B C D E V L R
Binary Tree Traversals n Postorder traversal (LRV) (recursive version) output: A B / C * D * E + L R V
- Slides: 16