550 likes | 767 Views
Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. General Tree Concepts Binary Trees. Data Structures and Algorithms. Outline. Tree Data Structure Examples Definition Terminology Binary Tree Definitions Examples Tree Traversal Binary Search Trees Definition.
E N D
Mark Allen Weiss: Data Structures and Algorithm Analysis in Java General Tree ConceptsBinary Trees Data Structures and Algorithms
Outline • Tree Data Structure • Examples • Definition • Terminology • Binary Tree • Definitions • Examples • Tree Traversal • Binary Search Trees • Definition
Linear Lists and Trees • Linear lists (arrays, linked list)are useful for serially ordered data • (e1,e2,e3,…,en) • Days of week • Months in a year • Students in a class • Trees are useful for hierarchically ordered data • Khalid’s descendants • Corporate structure • Government Subdivisions • Software structure
Examples of trees • directory structure • family trees: • all descendants of a particular person • all ancestors born after year 1800 of a particular person • evolutionary tress (also called phylogenetic trees) • albegraic expressions
Khalid’s Descendants What are other examples of hierarchically ordered data?
Property of a Tree • A treet is a finite nonempty set of nodes • One of these elements is called the root • Each node is connected by an edge to some other node • A tree is a connected graph • There is a path to every node in the tree • There are no cycles in the tree. • It can be proved that a tree has one less edge than the number of nodes. • Usually depicted with the root at the top
Is it a Tree? NO! All the nodes are not connected yes! (but not a binary tree) yes! NO! There is a cycle and an extra edge (5 nodes and 5 edges) yes! (it’s actually the same graph as the blue one) – but usually we draw tree by its “levels”
Tree Terminology • The element at the top of the hierarchy is the root. • Elements next in the hierarchy are the children of the root. • Elements next in the hierarchy are the grandchildren of the root, and so on. • Elements at the lowest level of the hierarchy are the leaves.
Other Definitions • Leaves, Parent, Grandparent, Siblings,Ancestors, Descendents Leaves = {Hassan,Gafar,Sami,Mohmmed} Parent(Musa) = Khalid Grandparent(Sami) = Musa Siblings(Musa) = {Ahmed,Omer} Ancestors(Hassan) = {Amed,Khalid} Descendents(Musa)={Fahad,Sami}
level 0 level 1 level 2 level 3 Levels and Height • Root is at level 0 and its children are at level 1.
Degree of a node • Node degree is the number of children it has
Tree Degree • Tree degree is the maximum of node degrees tree degree = 3
Measuring Trees • The height of a node v is the number of nodes on the longest path from v to a leaf • The height of the tree is the height of the root, which is the number of nodes on the longest path from the root to a leaf • The depth of a node v is the number of nodes on the path from the root to v • This is also referred to as the level of a node • Note that there are slightly different formulations of the height of a tree • Where the height of a tree is said to be the length (the number of edge) on the longest path from node to a leaf
root E A R E Child (of root) S T A Leaves or terminal nodes M P L E Depth of T: 2 Height of T: 1 Trees - Example Level 0 1 2 3
Tree Terminology Example • Ais the root node • Bis the parentof D and E • Cis the siblingof B • Dand E are the children of B • D, E, F, G, I are external nodes, or leaves • A, B, C, Hare internal nodes • The depth, level, orpath length of Eis 2 • The heightof the tree is 3 • The degreeof node Bis 2
C C A F E G G D Tree Terminology Example A path from A to D to G B D subtree rooted at B leaves: C,E,F,G E F G
TreeRepresentation Class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; }
Binary Trees • A finite (possibly empty) collection of elements • A nonempty binary tree has a root element and the remaining elements (if any) are partitioned into two binary trees • They are called the left and right subtrees of the binary tree A B C right child of A left subtree of A F D E G right subtree of C H I J
- different when viewed as a binary tree a a - same when viewed as a tree b c c b Difference Between a Tree & a Binary Tree • A binary tree may be empty; a tree cannot be empty. • No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. • The subtrees of a binary tree are ordered; those of a tree are not ordered.
L 0 L 1 L 2 L 3 Height of a Complete Binary Tree At each level the number of the nodes is doubled. total number of nodes: 1 + 2 + 22 + 23 = 24 - 1 = 15
Nodes and Levels in a Complete Binary Tree Number of the nodes in a tree with M levels: 1 + 2 + 22 + …. 2M = 2 (M+1) - 1 = 2*2M - 1 Let N be the number of the nodes. N = 2*2M - 1, 2*2M = N + 1 2M = (N+1)/2 M = log( (N+1)/2 ) N nodes : log( (N+1)/2 ) = O(log(N)) levels M levels: 2 (M+1) - 1 = O(2M ) nodes
Binary Tree Properties • The drawing of every binary tree with n elements, n > 0, has exactly n-1 edges. • A binary tree of height h, h >= 0, has at leasth and at most2h-1 elements in it.
maximum number of elements minimum number of elements Binary Tree Properties 3. The height of a binary tree that contains n elements, n >= 0, is at least(log2(n+1)) and at mostn.
Full Binary Tree • A full binary tree of height h has exactly 2h-1 nodes. • Numbering the nodes in a full binary tree • Number the nodes 1 through 2h-1 • Number by levels from top to bottom • Within a level, number from left to right
Node Number Property of Full Binary Tree • Parent of node i is node (i/2), unless i = 1 • Node 1 is the root and has no parent
Node Number Property of Full Binary Tree • Left child of node i is node 2i, unless 2i > n,where n is the total number of nodes. • If 2i > n, node i has no left child.
Node Number Property of Full Binary Tree • Right child of node i is node 2i+1, unless 2i+1 > n,where n is the total number of nodes. • If 2i+1 > n, nodei has no right child.
Complete Binary Tree with N Nodes • Start with a full binary tree that has at least nnodes • Number the nodes as described earlier. • The binary tree defined by the nodes numbered 1 through n is the n-node complete binary tree. • A full binary tree is a special case of a complete binary tree
Example of Complete Binary Tree • Complete binary tree with 10 nodes. • Same node number properties (as in full binary tree) also hold here.
Binary Tree Representation • Array representation • Linked representation
Array Representation of Binary Tree • The binary tree is represented in an array by storing each element at the array position corresponding to the number assigned to it.
Incomplete Binary Trees • Complete binary tree with some missing elements
Example Binary Tree To find a node’s parent use this formula: Parent’s Index = (Child’s Index – 1) / 2 Array index from 0 Ex. To find node’s 2 left and right child Left Child’s Index = 2 * Parent’s Index + 1 Right Child’s Index = 2 * Parent’s Index + 2
Linked Representation of Binary Tree • The most popular way to present a binary tree • Each element is represented by a node that has two link fields (leftChild and rightChild) plus an element field • Each binary tree node is represented as an object whose data type is binaryTreeNode • The space required by an n node binary tree isn * sizeof(binaryTreeNode)
Node Class For Linked Binary Tree TreeNode class private class TreeNode { private String data; private TreeNode left; private TreeNode right; public TreeNode(String element) { data = element; left = null; right = null; } }
Common Binary Tree Operations • Determine the height • Determine the number of nodes • Make a copy • Determine if two binary trees are identical • Display the binary tree • Delete a tree • If it is an expression tree, evaluate the expression • If it is an expression tree, obtain the parenthesized form of the expression
Binary Tree Traversal • A traversal of a tree is a systematic way of accessing or "visiting" all the nodes in the tree. • There are three basic traversal schemes: preorder, postorder, inorder • .In any of these traversals we always visit the left subtree before the right
Binary Tree Traversal Methods • Preorder • The root of the subtree is processed first before going into the left then right subtree (root, left, right). • Inorder • After the complete processing of the left subtree the root is processed followed by the processing of the complete right subtree (left, root, right). • Postorder • The root is processed only after the complete processing of the left and right subtree (left, right, root). • Level order • The tree is processed by levels. So first all nodes on level i are processed from left to right before the first node of level i+1 is visited
Preorder Traversal public void preOrderPrint() { preOrderPrint(root); } private void preOrderPrint(TreeNode tree) { if (tree != null) { System.out.print(tree.data + " "); // Visit the root preOrderPrint(tree.left);// Traverse the left subtree preOrderPrint(tree.right); // Traverse the right subtree } }
H D L F N B J A E I C G K M O Preorder Example (visit = print) N-L-R
Preorder of Expression Tree / * + a b - c d + e f Gives prefix form of expression.
Inorder Traversal public void inOrderPrint() { inOrderPrint(root); } public void inOrderPrint(TreeNode t) { if (t != null) { inOrderPrint(t.left); System.out.print(t.data + " "); inOrderPrint(t.right); } }
H D L F N B J A E I C G K M O Inorder Example (visit = print) L-N-R
Inorder of Expression Tree • Gives infix form of expression, which is how we normally write math expressions. What about parentheses?
Postorder Traversal public void ostOrderPrint() { postOrderPrint(root); } public void postOrderPrint(TreeNode t) { if (t != null) { postOrderPrint(t.left); postOrderPrint(t.right); System.out.print(t.data + " "); } }
H D L F N B J A E I C G K M O Postorder Example (visit = print) L-R-N