Click here to load reader
Upload
jarah
View
30
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Trees. Tree Terminology. A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the top of a tree is called its root The links from a node to its successors are called branches The successors of a node are called its children - PowerPoint PPT Presentation
Citation preview
Trees
TreesTree TerminologyChapter 8: Trees2A tree consists of a collection of elements or nodes, with each node linked to its successorsThe node at the top of a tree is called its rootThe links from a node to its successors are called branchesThe successors of a node are called its childrenThe predecessor of a node is called its parentTree Terminology (continued)Chapter 8: Trees3Each node in a tree has exactly one parent except for the root node, which has no parentNodes that have the same parent are siblingsA node that has no children is called a leaf nodeA generalization of the parent-child relationship is the ancestor-descendent relationship
Tree Terminology (continued)Chapter 8: Trees4A subtree of a node is a tree whose root is a child of that nodeThe level of a node is a measure of its distance from the root
Binary TreesChapter 8: Trees5In a binary tree, each node has at most two subtreesA set of nodes T is a binary tree if either of the following is trueT is emptyIts root node has two subtrees, TL and TR, such that TL and TR are binary trees
Some Types of Binary TreesChapter 8: Trees6Expression treeEach node contains an operator or an operandHuffman treeRepresents Huffman codes for characters that might appear in a text fileHuffman code uses different numbers of bits to encode letters as opposed to ASCII or UnicodeBinary search treesAll elements in the left subtree precede those in the right subtreeSome Types of Binary Trees (continued)Chapter 8: Trees7
Fullness and CompletenessChapter 8: Trees8Trees grow from the top downEach new value is inserted in a new leaf nodeA binary tree is full if every node has two children except for the leaves
General TreesChapter 8: Trees9Nodes of a general tree can have any number of subtreesA general tree can be represented using a binary tree
Tree TraversalsChapter 8: Trees10Often we want to determine the nodes of a tree and their relationshipCan do this by walking through the tree in a prescribed order and visiting the nodes as they are encounteredThis process is called tree traversalThree kinds of tree traversalInorderPreorderPostorderTree Traversals (continued)Chapter 8: Trees11Preorder: Visit root node, traverse TL, traverse TRInorder: Traverse TL, visit root node, traverse TRPostorder: Traverse TL, Traverse TR, visit root node
Visualizing Tree TraversalsChapter 8: Trees12You can visualize a tree traversal by imagining a mouse that walks along the edge of the treeIf the mouse always keeps the tree to the left, it will trace a route known as the Euler tourPreorder traversal if we record each node as the mouse first encounters itInorder if each node is recorded as the mouse returns from traversing its left subtreePostorder if we record each node as the mouse last encounters it
Traversals of Binary Search Trees and Expression TreesChapter 8: Trees13An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data valueAn inorder traversal of an expression tree inserts parenthesis where they belong (infix form)A postorder traversal of an expression tree results in postfix form
The Node ClassChapter 8: Trees14Just as for a linked list, a node consists of a data part and links to successor nodesThe data part is a reference to type ObjectA binary tree node must have links to both its left and right subtrees
The BinaryTree ClassChapter 8: Trees15
The BinaryTree Class (continued)Chapter 8: Trees16
Overview of a Binary Search TreeChapter 8: Trees17Binary search tree definitionA set of nodes T is a binary search tree if either of the following is trueT is emptyIts root has two subtrees such that each is a binary search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree
Searching a Binary TreeChapter 8: Trees18
Class TreeSet and Interface Search TreeChapter 8: Trees19
BinarySearchTree ClassChapter 8: Trees20
Insertion into a Binary Search TreeChapter 8: Trees21
Removing from a Binary Search TreeChapter 8: Trees22
Removing from a Binary Search Tree (continued)Chapter 8: Trees23
Heaps and Priority QueuesChapter 8: Trees24In a heap, the value in a node is les than all values in its two subtreesA heap is a complete binary tree with the following propertiesThe value in the root is the smallest item in the treeEvery subtree is a heap
Removing an Item from a HeapChapter 8: Trees25
Implementing a HeapChapter 8: Trees26Because a heap is a complete binary tree, it can be implemented efficiently using an array instead of a linked data structureFirst element for storing a reference to the root dataUse next two elements for storing the two children of the rootUse elements with subscripts 3, 4, 5, and 6 for storing the four children of these two nodes and so on
Inserting into a Heap Implemented as an ArrayListChapter 8: Trees27
Inserting into a Heap Implemented as an ArrayList (continued)Chapter 8: Trees28
Priority QueuesChapter 8: Trees29The heap is used to implement a special kind of queue called a priority queueThe heap is not very useful as an ADT on its ownWill not create a Heap interface or code a class that implements itWill incorporate its algorithms when we implement a priority queue class and HeapsortSometimes a FIFO queue may not be the best way to implement a waiting lineA priority queue is a data structure in which only the highest-priority item is accessibleInsertion into a Priority QueueChapter 8: Trees30
The PriorityQueue InterfaceChapter 8: Trees31Effectively the same as the Queue interface provided in chapter six
Design of a HeapPriorityQueue ClassChapter 8: Trees32
HeapPQwithComparatorChapter 8: Trees33
Huffman TreesChapter 8: Trees34A Huffman tree can be implemented using a binary tree and a PriorityQueueA straight binary encoding of an alphabet assigns a unique binary number to each symbol in the alphabetUnicode for exampleThe message go eagles requires 144 bits in Unicode but only 38 using Huffman coding
Huffman Trees (continued)Chapter 8: Trees35