Download presentation
Presentation is loading. Please wait.
Published byLawrence Cannon Modified over 9 years ago
1
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project 4 is due Friday Nov 3 by 5 pm.. 2.The next OWL assignment is up and due on November 8 at 11 pm 3.HWK2 grades are on OWL 4.No….I haven’t (but I will this weekend!)
2
2 CMPSCI 187 TreesTrees Introduction to Introduction to Programming with Data Structures trees binary trees traversals of trees data structures for trees Balancing unbalanced trees
3
3 CMPSCI 187 Trees l Trees can represent a hierarchy l Corporate structure
4
4 CMPSCI 187 Trees l Table of contents of a book:
5
5 CMPSCI 187 Trees l Unix or Macintosh file system l Web page hierarchy File Directory cs121cs187 8 Queens Interfaces In Java GUI Design
6
6 CMPSCI 187 Trees and Tree Terminology l A tree is a data structure that consists of a set of nodes and a set of edges (or branches) that connect nodes. l Trees are our first example of a nonlinear data structure. H Components do not form simple sequences, like lists. H Trees represent hierarchies.
7
7 CMPSCI 187 Tree: Definition l A tree is a collection of elements (nodes) l Each node may have 0 or more successors F (Unlike a list, which has 0 or 1 successor) l Each node has exactly one predecessor F Except the starting / top node, called the root. F Root has zero predecessors
8
8 CMPSCI 187 Terminology l A is the root node. l B is the parent of D and E. (or ancester) l C is the sibling of B l D and E are the children of B. (or descendents) l D, E, F, G, I are external nodes, or leaves l A, B, C, H are internal nodes l The depth (level) of E is 2 l The height of the tree is 3 l The degree of node B is 2 Property: (# edges) = (#nodes) - 1
9
9 CMPSCI 187 More tree terminology l Subtree of a node: A tree whose root is a child of that node l Level of a node: A measure of its distance from the root: Level of the root = 0 Level of other nodes = 1 + level of parent
10
10 CMPSCI 187 Binary Trees A Family Tree is a binary tree (each node has 0, 1, or 2 branches) Computer scientists like to look at tree upside down!
11
11 CMPSCI 187 Binary Trees l A binary tree is an ordered tree in which every node has at most two children (or at most two non- empty subtrees). l A set of nodes T is a binary tree if: H T is empty H or T consists of a s root node (internal node) s a left binary tree and s a right binary tree ((((3 x (1 + (4 + 6))) + (2 + 8)) x 5) + ( 4 x (7 + 2)))
12
12 CMPSCI 187 Expression Trees + * b / a 26 a/2 + 6*b
13
13 CMPSCI 187 Expression Trees: An Aside I Note that we can traverse this entire structure without any backpointers from child nodes to parent nodes. Whenever we need to back up, we just pop the stack and pick up where we left off. Recursion makes traversing binary trees easy. public void infix () { if (isEmpty ()) return; left.infix (); System.out.println (value ()); right.infix (); } + * b / a 26 a/2 + 6*b
14
14 CMPSCI 187 Expression Trees: An Aside II a 2 / 6 b * + hmmmm….looks like the postfix form of the expression public void postfix () { if (isEmpty ()) return; left.postfix (); right.postfix (); System.out.println (value ()); } + * b / a 26
15
15 CMPSCI 187 Binary Tree Example Decision Tree Rao’s
16
16 CMPSCI 187 Binary Sort Trees l Binary sort (or search) trees have this useful property: An inorder traversal of the tree will process the items in increasing order. F Relationship to binary search???? F Items in left subtree < item at root < items in right subtree
17
17 CMPSCI 187 Properties of Binary Trees l (# external nodes ) = (# internal nodes) + 1 (# nodes at level i) 2 i (# external nodes) 2 (height) (height) log 2 (# external nodes) (height) log 2 (# nodes) - 1 (height) (# internal nodes) = ((# nodes) - 1)/2
18
18 CMPSCI 187 Full Binary Trees l In a full binary tree, H every leaf node has the same depth H every non-leaf node (internal node) has two children. A B DE H IJK C FG L Not a full binary tree. A B DE H IJK C FG L MNO A full binary tree.
19
19 CMPSCI 187 Complete Binary Tree l In a complete binary tree H every level except the deepest must contain as many nodes as possible ( that is, the tree that remains after the deepest level is removed must be full). H at the deepest level, all nodes are as far to the left as possible. A B DE H IJK C FG L Not a Complete Binary Tree A B DE H IJK C FG L A Complete Binary Tree
20
20 CMPSCI 187 A Representation for Complete Binary Trees l Since tree is full, it maps nicely onto an array representation. A B DE H IJK C FG L 0 1 2 3 4 5 6 7 8 9 10 11 12 A B C D E F G H I J K L T: last
21
21 CMPSCI 187 Properties of the Array Representation l Data from the root node is always in T[0]. l Suppose some node appears in T[i] H data for its parent is always at location T[(i-1)/2] (using integer division) H data for its children nodes appears in locations T[2*i+1] for the left child T[2*i+2] for the right child H formulas provide an implicit representation of the edges H can use these formulas to implement efficient algorithms for traversing the tree and moving around in various ways.
22
22 CMPSCI 187 Proper Binary Tree l In a proper binary tree, F each node has exactly zero or two children F each internal node has exactly two children. A B DE H IJK C FG L A B DE H IJK C FG Not a proper binary treeA proper binary tree LM
23
23 CMPSCI 187 Not Complete or Full or Proper A B D E H IJK C F L
24
24 CMPSCI 187 Comparing Trees These two trees are not the same tree! A B A B Why?
25
25 CMPSCI 187 Binary Tree Nodes Data Left Right {Could put in a reference to a node's parent as well} Data Left Right Data Left Right
26
26 CMPSCI 187 Tree Implementation Plan l Different from Koffman and Wolfgang l Define interfaces for F BinaryTreeNode F Binary Tree l Do full implementation of a binary tree F Complete code F But we’ll only hit the highlights in lecture l Read the book to see what they did F Very restricted binary tree implementation F Use it to define binary sort trees F Never does a standard binary tree implementation l Jave does even less … we’ll see later
27
27 CMPSCI 187 An Interface for Nodes in a Binary Tree public interface BinaryNodeInterface { public Object getData(); //returns the data element at this node. public void setData(Object myElement); //sets data element to myElement public void setLeftChild(BinaryNodeInterface myLeft); //set left child to myLeft public void setRightChild(BinaryNodeInterface myRight); //set right child to myRight public BinaryNodeInterface getLeftChild();//returns left child node public BinaryNodeInterface getRightChild(); //returns right child node public boolean hasLeftChild(); //returns true if this node has a left child public boolean hasRightChild(); //returns true if this node has a right child public boolean isLeaf(); //returns true if node is a leaf node. } Basically the set and get methods for the data fields of the binary tree node……….
28
28 CMPSCI 187 The BTreeNode Class public class BinaryNode implements BinaryNodeInterface, java.io.serializable { protected Object data; protected BinaryNode left; protected BinaryNode right; //Constructors public BinaryNode() {this(null);} public BinaryNode(Object dataElement) { this(dataElement, null,null; }
29
29 CMPSCI 187 The BTreeNode Class Constructor public BinaryNode(Object dataElement, BinaryNode leftChild, BinaryNode rightChild) { data = dataElement; left = leftChild; right = rightChild; }
30
30 CMPSCI 187 The BTreeNode Class public boolean isLeaf() { return ((left == null) && (right == null)); } public Object getData() { return data;} public void setData(Object newData) { data=newData;}
31
31 CMPSCI 187 The BTreeNode Class public void setLeftChild(BinaryNodeInterface leftChild) { left = leftChild;} public BinaryNodeInterface getLeftChild() { return left;} public void setRightChild(BinaryNodeInterface rightChild) { right = rightChild;} public BinaryNodeInterface getRightChild() { return right;} Set,Get LeftChild Set,Get RightChild
32
32 CMPSCI 187 The BTreeNode Class Public boolean hasLeftChild() { return left != null } Public boolean hasRightChild() { return right != null } }//end Binary Node
33
33 CMPSCI 187 Operations on Binary Trees l Common operations on trees: F Create a tree F Determine if a tree is empty F Empty a tree F Set and Get the element at the root F Get the left and right subtrees at the root F Create a left and right subtree for the root F Attach a tree as the left or right subtree of the root F Detach the left or right subtree of the root l Can now define an ADT for the binary tree root: tree1: root: tree1:
34
34 CMPSCI 187 The BinaryTree Class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.