Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

The ending node has 'null' for the next. next. data. Next. null. data. head. Node with double link ... The ending node (leaf) has 'null' links. head. left ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 25
Provided by: thaddeusf
Category:
Tags: ending | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • CSC 171 FALL 2001
  • LECTURE 21

2
COURSE EVALUATIONS
  • Tuesday, December 11th, in Lecture

3
History Personal Computers
  • 1975 the first mass produced and marketed
    personal computer (available both as a kit or
    assembled) was welcomed with open arms.
    Developers Edward Roberts, William Yates and Jim
    Bybee the MITS Altair 8800. 375, 256 bytes of
    memory (not 256k), had no keyboard, no display,
    and no auxiliary storage device.

4
History Microsoft
  • 1975 Bill Gates and Paul Allen wrote their first
    product for the Altair - a BASIC compiler

5
History Apple
  • 1976 - A year after the Altair was produced,
    Steve Jobs and Steve Wozniak produced the Apple
    II that was assembled and complete with its own
    keyboard and monitor.

6
Review Node with single link
  • class Node
  • private Object data // the data
  • private Node next // the link

data next
7
Linked List linear structure
  • A linked list has-a reference to a node
  • The head of the list
  • The ending node has null for the next

head
data next
data Next null
8
Node with double link
  • class Node
  • private Object data // the data
  • private Node left // the link
  • private Node right // the link

data left right
9
Double link tree structure
  • A tree has-a references
  • The root of the list
  • The ending node (leaf) has null links

data left right
head
data left right
data left right
10
Tree Nodes
  • private class Node
  • public Comparable data
  • public Node left
  • public Node right

11
Tree Nodes INSERTION
  • public void insertNode(Node newNode)
  • if (newNode.data.compareTo(data) lt 0)
  • if (left null) left newNode
  • else left.insertNode(newNode)
  • else
  • if (right null) right newNode
  • else right.insertNode(newNode)

12
Tree Node Print
  • public void printNodes()
  • if (left ! null)
  • left.printNodes()
  • System.out.println(data)
  • if (right ! null)
  • right.printNodes()

13
TREE CLASS
  • class Tree
  • private Node root
  • public Tree()
  • root null

14
Insertion an Object on the tree
  • public void insert(Comparable obj)
  • Node newNode new Node()
  • newNode.data obj
  • newNode.left null
  • newNode.right null
  • if (root null) root newNode
  • else root.insertNode(newNode)

15
Tree Print
  • public void print()
  • if (root ! null)
  • root.printNodes()

16
TreeTest
  • public class TreeTest
  • public static void main(String args)
  • Tree staff new Tree()
  • staff.insert("Juliet")
  • staff.insert("Romeo")
  • staff.insert(Eve")
  • staff.insert("Tom")
  • staff.insert("Dick")
  • staff.insert("Harry")
  • staff.print()

17
Binary Search Tree
18
A Binary Tree not a BST
19
BST after 4 insertions
  • insert(Juliet)
  • insert(Dick)
  • insert(Tom)
  • insert(Harry)

20
BST after 5 insertions
  • insert(Juliet)
  • insert(Dick)
  • insert(Tom)
  • insert(Harry)
  • insert(Romeo)

21
Unbalance BST
  • insert(Tom)
  • insert(Dick)
  • insert(Harry)
  • insert(Romeo)

22
Tree Node Pre-Order
  • public void printNodes()
  • System.out.println(data)
  • if (left ! null)
  • left.printNodes()
  • if (right ! null)
  • right.printNodes()

23
Tree Node In-Order
  • public void printNodes()
  • if (left ! null)
  • left.printNodes()
  • System.out.println(data)
  • if (right ! null)
  • right.printNodes()

24
Tree Node Post-Order
  • public void printNodes()
  • if (left ! null)
  • left.printNodes()
  • if (right ! null)
  • right.printNodes()
  • System.out.println(data)
Write a Comment
User Comments (0)
About PowerShow.com