Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tonga Institute of Higher Education

Similar presentations


Presentation on theme: "Tonga Institute of Higher Education"— Presentation transcript:

1 Tonga Institute of Higher Education
Binary Trees Tonga Institute of Higher Education

2 Review of Ordered Arrays and Linked Lists
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.

3 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

4 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

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

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

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

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

9 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

10 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

11 Presentation Binary Tree Applet

12 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

13 Code View Node Class

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

15 Code View Tree Class

16 Finding a Node Each node can represent an object Each object has a key
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.

17 Demonstration Find in Applet

18 Code View Find Code

19 Find Efficiency The time required to find a node depends on the level that the node is located This is O(logN) time

20 Inserting a Node We can insert an item with these steps
Find the place to put the node Pretend like we’re 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

21 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

22 Demonstration Insert in Applet

23 Code View Insert Code

24 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

25 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

26 Inorder Traversal - 2 Start from the root node Move to right child
left child

27 Inorder, Preorder and Postorder Traversals
Inorder Traversal Go to left child Visit the node Go to right child Preorder Traversal Postorder Traversal

28 Demonstration Traversals in Applet

29 Code View Traversal Code

30 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

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

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

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

34 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

35 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

36 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

37 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

38 Demonstration Deletions in Applet

39 Code View Deletion Code

40 Binary Tree Summary with Driver
Code View Binary Tree Summary with Driver


Download ppt "Tonga Institute of Higher Education"

Similar presentations


Ads by Google