Loading...

PPT – Binary Trees PowerPoint presentation | free to download - id: 7302eb-YmQ3M

The Adobe Flash plugin is needed to view this content

Binary Trees

- Tonga Institute of Higher Education

Review of Ordered Arrays and Linked Lists

- Ordered Array
- Searching is fast.
- Binary Search
- O(logN)
- Inserting and Deleting is slow.
- Need to move many values one space over
- O(N)
- Linked List
- Inserting and deleting is fast.
- Just need to change a few references
- O(1)
- Searching is slow
- Must start from the beginning of the list and

check every link. - O(N)

Introduction to Binary Trees

- A Binary Tree combines the advantages of ordered

arrays and linked lists - Like an ordered array, we can
- Search for an item quickly
- Like a linked list, we can
- Insert items quickly
- Delete items quickly

What is a Tree?

- A tree consists of nodes connected by edges
- Nodes represent objects
- Example Customers, plane tickets, etc.
- Edges represent references
- There is 1 node at the top of the tree
- Trees are small on top and large on bottom
- It looks like an upside down tree

Tree Terminology - 1

- Path The route you must take to get to a node
- Root The top node of the tree
- Parent Every node must have one edge running

upwards to another node. The node above it is a

parent - Child Any node may have one or more edges

running downward to another node. The node below

it is a child.

Tree Terminology - 2

- Leaf A node with no children
- Subtree Any node may be considered to be the

root of a subtree, which consists off all the

edges and nodes under that node. - Visiting A node is visited when a program

control arrives at a node to carry out an

operation like checking a value. Just passing

over a node on a path from one node to another is

not considered to be visiting the node.

Tree Terminology - 3

- Traversing To traverse a tree means to visit

all nodes in a specified order. - Levels The level of a node refers to how many

edges the node is from the root. - Keys One data field in an object stored in a

node usually contains a key value. This value is

used to search for the item or perform other

operations on it.

Tree Rules

This is not a valid tree because there is a node

with more than one path to the root

- The only way to move from one node to another is

to follow a path along the lines - There must be only one path to the root.

Using Trees in Everyday Life

- You have been using a Tree for at least half a

year already! - The file structure in your computer follows these

rules! - Desktop is the root node
- Directories are nodes
- Directories without any subdirectories or files

in them are leaves - Files are leaves
- You double-click on a folder to move from one

node to another

Binary Trees

- Binary Trees contain number keys
- Binary Trees can only have a maximum of 2

children - The key in the left child node must be less than

or equal to the key in the right child node

Presentation

- Binary Tree Applet

Node Class

- We can create an class to represent a node
- The class can contain data representing the

object being stored - Employees
- Items
- The class must also contain
- A key value
- A left node
- A right node

Code View

- Node Class

Tree Class

- We can create an class to represent a tree
- A tree has only one field a node that holds the

root - The tree class can have many methods
- Find
- Insert
- Delete
- Etc.

Code View

- Tree Class

Finding a Node

- Each node can represent an object
- Customer
- Item
- Each object has a key
- Customer ID
- Item ID
- We can find an item easily with these steps
- At a node, determine whether the key of the

object you are looking for is greater than, equal

to, or less than the value of the node - If the key is less Move to the left child node

and repeat from step 1. - If no left child node is available, the node does

not exist in this tree. - If the key is equal We have found the node we

are looking for - If the key is greater Move to the right child

node and repeat from step 1. - If no right child node is available, the node

does not exist in this tree.

Demonstration

- Find in Applet

Code View

- Find Code

Find Efficiency

- The time required to find a node depends on the

level that the node is located - This is O(logN) time

Inserting a Node

- We can insert an item with these steps
- Find the place to put the node
- Pretend like were looking for a node
- When we get to the last node and realize that the

node we want to insert does not already exist, we

add it as a left or right child node

Unbalanced Trees

- Sometimes, when we add a lot of nodes, our tree

becomes very heavy on one side - This condition is called an unbalance tree
- How to handle unbalanced trees is covered in

Algorithm Analysis and Design

Demonstration

- Insert in Applet

Code View

- Insert Code

Traversing a Tree

- Traversing To traverse a tree means to visit

all nodes in a specified order. - There are 3 ways to traverse a tree
- Inorder
- Preorder
- Postorder

Inorder Traversal - 1

- An inorder traversal of a binary search tree will

cause all the nodes to be visited in ascending

order. - To traverse a tree Inorder we repeat the 3

following steps until we have visited every node

in the binary tree - Go to left child
- Visit the node
- Go to right child

Inorder Traversal - 2

Start from the root node

Move to right child

Move to left child

Inorder, Preorder and Postorder Traversals

- Inorder Traversal
- Go to left child
- Visit the node
- Go to right child
- Preorder Traversal
- Visit the node
- Go to left child
- Go to right child
- Postorder Traversal
- Go to left child
- Go to right child
- Visit the node

Demonstration

- Traversals in Applet

Code View

- Traversal Code

Finding Maximum and Minimum Values

- To get the minimum value, go to the left-most

node - To get the maximum value, go to the right-most

node

Deleting a Node with No Children

- To delete a node with no children, set the

reference of the parent node to be null. - The node will eventually be cleaned up by the

Java garbage collector.

Deleting a Node with 1 Child

- To delete a node with 1 child, set the reference

of the parent node to be the child of the deleted

node.

Deleting a Node with 2 Children - 1

- When you are deleting a node with 2 children, we

have a problem. - How do we know which node to put in the place of

the deleted node? - In other words, which node is the successor?

Deleting a Node with 2 Children - 2

Subset of the nodes on the right

- To find the successor we must find the smallest

value of the subset of the nodes on the right - To do this
- Go to the right child of the node to be deleted
- Then, find the leftmost node

Finding a Successor Node

These nodes are the leftmost nodes of the right

child!

In this case the successor node is the right

child of the node to be deleted

In this case, the successor node is not the

right child of the node to be deleted

Deleting a Node when the Successor is the Right

Child of the Node to be Deleted

87 replaces 75 because it was the successor

- When the successor is the right child of the node

to be deleted, replace the deleted node with the

successor

Deleting a Node when the Successor is a Left

Descendant of Right Child Node of the Node to be

Deleted

- The successor replaces the deleted node
- The algorithm is redone as if the successor was

deleted until the entire tree has been redone

Demonstration

- Deletions in Applet

Code View

- Deletion Code

Code View

- Binary Tree Summary with Driver