Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.

Similar presentations


Presentation on theme: "Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called."— Presentation transcript:

1 Trees Chapter 8

2 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. 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 Each node in a tree has exactly one parent except for the root node, which has no parent

3 3 Tree Terminology (continued) Nodes that have the same parent are siblings A node that has no children is called a leaf node (or, an external node) A node with at least one child is called an internal node. A generalization of the parent-child relationship is the ancestor-descendent relationship

4 4 Tree Terminology (continued) A subtree of a node is a tree whose root is a child of that node E.g. a subtree of node dog is marked as a triangle.

5 5 Tree Terminology (continued) The level (or depth) of a node is a measure of its distance from the root. Defined recursively: If node N is the root, its level is 0. (this is different than our textbook, which lets the level of root to be 1.) If node N is not the root, its level is 1 + the level of its parent. Height of a tree: maximum level (depth) in the tree Height of an empty tree : 0 We will denote height with h We will denote number of nodes by n

6 6 Binary Trees In a binary tree, each node has at most two children. 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

7 7 Example Binary Tree Expression tree Each node contains an operator or an operand

8 8 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 Properties of binary trees: h and n relationship?

9 9 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

10 10 Tree Traversals (continued) Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

11 11 Visualizing Tree Traversals Imagine 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 Preorder? Inorder? Postorder?

12 12 Traversals Expression Trees An inorder traversal of an expression tree (inserting parenthesis where they belong) we get the infix form. A postorder traversal of an expression tree results in postfix form. x y + a b + c / * How can we evaluate an expression stored in such a tree? What type of traversal do we need?

13 13 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 Object A binary tree node must have links to both its left and right subtrees

14 14 The BinaryTree Class * /+ xyba

15 15 The BinaryTree Class (continued)

16 16 See BinaryTree.java Add InOrderTraverse and PostOrderTraverse methods.

17 17 General Trees Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

18 18 General trees The tree node stores data firstChild // instead of left nextSibling //instead of right

19 19 Overview of a Binary Search Tree 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

20 20 Searching a Binary Tree

21 21 Class Interface Search Tree Java API’s TreeSet class implements a Binary Search Tree

22 22 BinarySearchTree Class

23 23 Insertion into a Binary Search Tree

24 24 Removing from a Binary Search Tree

25 25 Removing from a Binary Search Tree (continued)


Download ppt "Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called."

Similar presentations


Ads by Google