28
Trees Chapter 8

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

Embed Size (px)

Citation preview

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

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

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

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

• We will denote height with h• We will denote number of nodes by n

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

Example Binary Tree

• Expression tree• Each node contains an operator or an operand

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

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

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

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

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

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

The BinaryTree Class*

/+

x y ba

15

The BinaryTree Class (continued)

16

See BinaryTree.java

• Add InOrderTraverse and PostOrderTraverse methods.

• Running time of postorder, preorder, inorder traversals?

17

Add to BinaryTree.java

• public int height() //return the height of the tree

18

• Given an expression tree T• Write algorithms (in pseudo-code)

• printExpression(T) //print the expression in //parenthesized infix

form

• evaluateExpression(T) //return the value of the //expression

19

General Trees

• Nodes of a general tree can have any number of subtrees

• A general tree can be represented using a binary tree

20

General trees

• The tree node stores• data• firstChild // instead of left• nextSibling //instead of right

21

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

22

Searching a Binary Tree

23

Class Interface Search Tree

Java API’s TreeSet class implements a Binary Search Tree

24

BinarySearchTree Class

25

Insertion into a Binary Search Tree

26

Removing from a Binary Search Tree

27

Removing from a Binary Search Tree (continued)

28

• public Object findKth(BinarySearchTree T, int k)

//return the k-th smallest item from the BST T.