tree - Trees(general rooted binary balanced In graph...

This preview shows pages 1–5. Sign up to view the full content.

Trees (general, rooted, binary, balanced) In graph theory (a subject in Mathematics), a tree is a set of nodes (vertices) and edges (links, lines) connecting the nodes such that there are no cycles and that every two nodes are connected by at least one path (a sequence of adjacent edges). In many Computer Science applications, one particular node of the tree is designated as the root, other nodes are arranged by levels below the root level A general tree A rooted tree, height = 2 Level 0 Level 1 Level 2 root

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A Binary Tree: A rooted tree such that each node has at most two nodes connected to it at the next level, distinguished as the left child node and the right child node (if present). A recursive definition for binary trees: (Basis) An empty tree is a binary tree; (Recursive step) A root whose two child nodes are binary trees; (Closure) Every binary tree must be constructed by repeated use of the recursive steps. A B C D E F G H I J Leaf nodes: Nodes without child nodes, i.e., nodes D, H, I, F, J. Each node has one parent node, except for the root. Non-leaf nodes are internal nodes, i.e., A, B, C, E, G.
A BinaryNode class in Java: class BinaryNode { // Constructors BinaryNode(Comparable theElement ) { this( theElement, null, null ); } BinaryNode(Comparable theElement, BinaryNode lt, BinaryNode rt) { element = theElement; left = lt; right = rt; } // Friendly data; accessible by other package routines Comparable element; // The data in the node BinaryNode left; // Left child BinaryNode right; // Right child }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Suppose T is a binary tree of n nodes, n 1, and height h 0. Let n q denote the total number of nodes at level q , for 0 q h . The following properties hold: (a) n q 2 q , for 0 q h ; (b) Proof: We prove (a) by using induction on q , the level number. (Basis Step) When q = 0, that is, at the root level, there is only one node (the root). Thus, n 0 = 1 = 2 0 2 q . So the Basis Step is proved. (Induction Hypothesis) Consider q = k . Suppose Property (a) holds for level k , that is suppose n k = the total number of nodes of tree T at level k 2 k for some k 0. (Induction Step) Consider q = k + 1. We need to prove n k +1 2 k +1 ------ (1). Since each node at level ( k + 1) must be a child of a node at level k , by the definition of level, and since each node has at most two child nodes, by the definition of binary tree, so the total number of nodes at level ( k + 1) = n k +1 2 ( n k ) 2 (2 k ) by the Induction Hypothesis = 2 k+ 1 = RHS of (1). Thus, the Induction Step is proved.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 23

tree - Trees(general rooted binary balanced In graph...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online