{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

heap, vector heap and priority queue

# heap, vector heap and priority queue - Trees Chapter 8...

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

Chapter 8: Trees 1 Trees

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

View Full Document
Chapter 8: Trees 2 Chapter Outline How trees organize information hierarchically Using recursion to process trees The different ways of traversing a tree Binary trees, Binary Search trees, and Heaps Implementing binary trees, binary search trees, heaps Using linked data structures and arrays Using binary search trees for efficient data lookup Using Huffman trees for compact character storage
Chapter 8: Trees 3 Tree Terminology A tree is a collection of elements (nodes) Each node may have 0 or more successors (Unlike a list, which has 0 or 1 successor) Each node has exactly one predecessor Except the starting / top node, called the root Links from node to its successors are called branches Successors of a node are called its children Predecessor of a node is called its parent Nodes with same parent are siblings Nodes with no children are called leaves

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

View Full Document
Chapter 8: Trees 4 Tree Terminology (2) We also use words like ancestor and descendent
Chapter 8: Trees 5 Tree Terminology (3) Subtree of a node: A tree whose root is a child of that node Depth of a node: A measure of its distance from the root: Depth of the root = 0 Depth of other nodes = 1 + depth of parent

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

View Full Document
Chapter 8: Trees 6 Tree Terminology (4) Depth 0 (root) Depth 1 Depth 1 Depth 2
Chapter 8: Trees 7 Binary Trees Binary tree: a node has at most 2 non-empty subtrees Set of nodes T is a binary tree if either of these is true: T is empty Root of T has two subtrees, both binary trees (Notice that this is a recursive definition) This is a binary tree

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

View Full Document
Chapter 8: Trees 8 Examples of Binary Trees Expression tree Non-leaf ( internal ) nodes contain operators Leaf nodes contain operands Huffman tree Represents Huffman codes for characters appearing in a file or stream Huffman code may use different numbers of bits to encode different characters ASCII or Unicode uses a fixed number of bits for each character
Chapter 8: Trees 9 Examples of Binary Trees (2)

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

View Full Document
Chapter 8: Trees 10 Examples of Binary Trees (3) Code for b = 100000 Code for w = 110001 Code for s = 0011 Code for e = 010
Chapter 8: Trees 11 Examples of Binary Trees (4) Binary search trees Elements in left subtree < element in subtree root Elements in right subtree > element in subtree root Search algorithm: 1. if the tree is empty, return NULL 2. if target equals to root node’s data, return that data 3. if target < root node data, return search(left subtree) 4. otherwise return search(right subtree)

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

View Full Document
Chapter 8: Trees 12 Fullness and Completeness (In computer science) trees grow from the top down New values inserted in new leaf nodes In a full tree, every node has 0 or 2 non-null children A complete tree of height h is filled up to depth h -1, and, at depth h , any unfilled nodes are on the right.
Chapter 8: Trees 13 Fullness and Completeness (2) A binary tree is complete if: All leaves are at level h or level h-1 (for some h ) All level h-1 leaves are to the right

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

View Full Document
Chapter 8: Trees 14 General (Non-Binary) Trees Nodes can have any number of children
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern