Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.

Similar presentations


Presentation on theme: "Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information."— Presentation transcript:

1 Fall 2007CS 2251 Trees Chapter 8

2 Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the difference between binary trees, binary search trees, and heaps To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays

3 Fall 2007CS 2253 Chapter Objectives To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced storage requirements

4 Fall 2007CS 2254 Tree Terminology

5 Fall 2007CS 2255 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 The predecessor of a node is called its parent

6 Fall 2007CS 2256 Tree Terminology Each node in a tree has exactly one parent except for the root node, which has no parent Nodes that have the same parent are siblings A node that has no children is called a leaf node A generalization of the parent-child relationship is the ancestor-descendent relationship A subtree of a node is a tree whose root is a child of that node The level of a node is a measure of its distance from the root

7 Fall 2007CS 2257 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the following is true –T is empty –Its root node has two subtrees, TL and TR, such that TL and TR are binary trees

8 Fall 2007CS 2258 Expression tree Each node contains an operator or an operand –operator nodes have children –operand nodes are leaves

9 Fall 2007CS 2259 Huffman tree Represents Huffman codes for characters that might appear in a text file Huffman code uses different numbers of bits to encode letters as opposed to ASCII or Unicode

10 Fall 2007CS 22510 Binary Search Trees All elements in the left subtree precede those in the right subtree

11 Fall 2007CS 22511 Fullness and Completeness Trees grow from the top down Each new value is inserted in a new leaf node A binary tree is full if every node has two children except for the leaves

12 Fall 2007CS 22512 General Trees Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

13 Fall 2007CS 22513 Tree Traversals Often we want to determine the nodes of a tree and their relationship –Can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three kinds of tree traversal –Inorder –Preorder –Postorder

14 Fall 2007CS 22514 Preorder Tree Traversal Visit root node, traverse TL, traverse TR Algorithm if the tree is empty return else visit the root do preorder traversal of left subtree do preorder traversal of right subtree

15 Fall 2007CS 22515 Inorder Tree Traversals Traverse TL, visit root node, traverse TR Algorithm if the tree is empty return else do preorder traversal of left subtree visit the root do preorder traversal of right subtree

16 Fall 2007CS 22516 Postorder Tree Traversals Traverse TL, Traverse TR, visit root node Algorithm if the tree is empty return else do preorder traversal of left subtree do preorder traversal of right subtree visit the root

17 Fall 2007CS 22517 Visualizing Tree Traversals You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree –If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour Preorder traversal if we record each node as the mouse first encounters it Inorder if each node is recorded as the mouse returns from traversing its left subtree Postorder if we record each node as the mouse last encounters it

18 Fall 2007CS 22518 Visualizing Tree Traversals

19 Fall 2007CS 22519 Traversals of Binary Search Trees An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value

20 Fall 2007CS 22520 Traversals of Expression Trees An inorder traversal of an expression tree inserts parenthesis where they belong (infix form) A postorder traversal of an expression tree results in postfix form A preorder traversal of an expression results in prefix form

21 Fall 2007CS 22521 Implementing trees Trees are generally implemented with linked structure similar to what we did for linked lists Nodes need to have references to at least two child nodes as well as the data A node may also have a parent reference.

22 Fall 2007CS 22522 The Node Class Just as for a linked list, a node consists of a data part and links to successor nodes The data part is a reference to type E A binary tree node must have links to both its left and right subtrees

23 Fall 2007CS 22523 The BinaryTree Class

24 Fall 2007CS 22524 The BinaryTree Class (continued)

25 Fall 2007CS 22525 Overview of a Binary Search Tree Binary search tree definition –A set of nodes T is a binary search tree if either of the following is true T is empty Its 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

26 Fall 2007CS 22526 Binary Search Tree

27 Fall 2007CS 22527 Searching a Binary Tree Searching for kept or jill

28 Fall 2007CS 22528 Class Search Tree

29 Fall 2007CS 22529 BinarySearchTree Class

30 Fall 2007CS 22530 BinarySearchTreeData

31 Fall 2007CS 22531 Binary Search Tree Insertion if the root is null create new node containing item to be the root else if item is the same as root data item is already in tree, return false else if item is less than root data search left subtree else search right subtree

32 Fall 2007CS 22532 Binary Search Tree Delete if root is null return null else if item is less than root data return result of deleting from left subtree else if item is greater than root data return result of deleting from right subtree else // need to replace the root save data in root to return replace the root (see next slide)

33 Fall 2007CS 22533 Replacing root of a subtree if root has no children set parent reference to local root to null else if root has one child set parent reference to root to child else // find the inorder predecessor if left child has no right child set parent reference to left child else find rightmost node in right child of left subtree and move its data to root

34 Fall 2007CS 22534 Delete Example

35 Fall 2007CS 22535 Heaps and Priority Queues In a heap, the value in a node is les than all values in its two subtrees A heap is a complete binary tree with the following properties –The value in the root is the smallest item in the tree –Every subtree is a heap

36 Fall 2007CS 22536 Inserting an Item into a Heap

37 Fall 2007CS 22537 Removing from a Heap

38 Fall 2007CS 22538 Implementing a Heap Because a heap is a complete binary tree, it can be implemented efficiently using an array instead of a linked data structure First element for storing a reference to the root data Use next two elements for storing the two children of the root Use elements with subscripts 3, 4, 5, and 6 for storing the four children of these two nodes and so on

39 Fall 2007CS 22539 Computing Positions Parent of a node at position c is parent = (c - 1) / 2 Children of node at position p are leftchild = 2 p + 1 rightchild = 2 p + 2

40 Fall 2007CS 22540 Inserting into a Heap Implemented as an ArrayList

41 Fall 2007CS 22541 Inserting into a Heap Implemented as an ArrayList

42 Fall 2007CS 22542 Using Heaps The heap is not very useful as an ADT on its own –Will not create a Heap interface or code a class that implements it –Will incorporate its algorithms when we implement a priority queue class and Heapsort The heap is used to implement a special kind of queue called a priority queue

43 Fall 2007CS 22543 Priority Queues Sometimes a FIFO queue may not be the best way to implement a waiting line –What if some entries have higher priority than others and need to be moved ahead in the line? A priority queue is a data structure in which only the highest-priority item is accessible

44 Fall 2007CS 22544 Insertion into a Priority Queue Imagine a print queue that prints the shortest documents first

45 Fall 2007CS 22545 The PriorityQueue Class Java provides a PriorityQueue class that implements the Queue interface given in Chapter 6. Peek, poll, and remove methods return the smallest item in the queue rather than the oldest item in the queue.

46 Fall 2007CS 22546 Design of a KWPriorityQueue Class

47 Fall 2007CS 22547 Huffman Trees A Huffman tree can be implemented using a binary tree and a PriorityQueue A straight binary encoding of an alphabet assigns a unique binary number to each symbol in the alphabet –Unicode for example The message “go eagles” requires 144 bits in Unicode but only 38 using Huffman coding

48 Fall 2007CS 22548 Huffman Tree Example

49 Fall 2007CS 22549 Huffman Trees

50 Fall 2007CS 22550 Chapter Review A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last

51 Fall 2007CS 22551 Chapter Review A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node A heap is a complete binary tree in which the data in each node is less than the data in both its subtrees Insertion and removal in a heap are both O(log n) A Huffman tree is a binary tree used to store a code that facilitates file compression


Download ppt "Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information."

Similar presentations


Ads by Google