Docsity

Binary Trees - Data Structures - Lecture Slides | CS 230, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1
17 - 1
Binary Trees
PS4 due 1:30pm Tuesday, April 10
Wellesley College CS230
Lecture 17
Thursday, April 5
Handout #28
17 - 2
Motivation: Inefficiency of Linear Structures
Up to this point our focus has been linear structures:
arrays, vectors, linked lists
Some operations on linear structures are cheap:
• Constant time:
Get/set elt at a given index in array/vector
Add elt to the end of a vector
Add elt to the front of a im/mutable list
Add elt to the back of a mutable list (if have pointer to last node)
• Logarithmic time:
Binary search on sorted array/vector
Other operations are expensive (linear in size):
Inserting/deleting elt at arbitrary position in an array/vector/list
Finding an element in a list (even a sorted one)
There is no linear structure for which insertion, deletion, and membership
operations (e.g. in priority queues, sets, bags, and tables) are all cheaper than
linear.
pf3
pf4
pf5

Partial preview of the text

Download Binary Trees - Data Structures - Lecture Slides | CS 230 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

17 - 1

Binary Trees

PS4 due 1:30pm Tuesday, April 10 Wellesley College CS Lecture 17 Thursday, April 5 Handout # 17 - 2

Motivation: Inefficiency of Linear Structures

Up to this point our focus has been linear structures: arrays, vectors, linked lists Some operations on linear structures are cheap:

  • Constant time: Get/set elt at a given index in array/vector Add elt to the end of a vector Add elt to the front of a im/mutable list Add elt to the back of a mutable list (if have pointer to last node)
  • Logarithmic time: Binary search on sorted array/vector Other operations are expensive (linear in size): Inserting/deleting elt at arbitrary position in an array/vector/list Finding an element in a list (even a sorted one) There is no linear structure for which insertion, deletion, and membership operations (e.g. in priority queues, sets, bags, and tables) are all cheaper than linear.

17 - 3

Idea: Branching of Binary Search

+ Linking of Linked Lists → Binary Trees

A binary tree is either

  • a leaf:
  • a node with (1) a left subtree (2) a value and (3) a right subtree: binary tree node left subtree value right subtree 17 - 4 A Sample Binary Tree F C G

A

D B

E

F

C

G

A

D B

E

Values are often shown in the nodes Often, the leaves are not shown, but they’re still there!

17 - 7 Binary Search Trees E F G

B

A D

C

To get full advantage of binary trees for data structures, need

the values to be ordered.

A binary search tree (BST) is a binary tree in which the following

properties hold at every node:

(1) All elements in the left subtree are ≤ the value;

(2) All elements in the right subtree are ≥ the value.

Here is a sample BST →

BSTs will be very important in the

next lecture, when we use binary

trees to implement data structures

like sets, bags, and tables For now,

we’ll stick with generic binary trees.

17 - 8 General Trees In general, trees can have any number of nodes Trees are used for many purposes:

  • Family trees
  • Sport tournaments
  • Animal kingdom
  • Organizational chart
  • A graph without cycles
  • A home’s plumbing and electrical systems
  • File systems
  • Document structure (e.g. HTML)
  • Java class hierarchy
  • Representing an expression/program
  • …

17 - 9 Arithmetic Expressions as Trees

Arithmetic expressions are often represented as binary trees.

Internal nodes are operations - Leaves are numbers/variables.

Operator precedence is enforced by the tree shape.

a b

a c

b a b

  • c a-b a-(b/c) (^) (a-b)*c 17 - 10 MBinTree ontract public static MBinTree leaf() ; public static boolean isLeaf(MBinTree tr) ; public static MBinTree node(MBinTree lt, T val, MBinTree rt) ; public static T value (MBinTree tr) ; public static MBinTree left (MBinTree tr) ; public static MBinTree right (MBinTree tr) ; public static void setValue (MBinTree tr, T newValue) ; public static void setLeft (MBinTree tr, MBinTree newLeft) ; public static void setRight (MBinTree tr, MBinTree newRight) ;