Binary Trees CS1316: Representing Structure and Behavior.

31
Binary Trees CS1316: Representing Structure and Behavior

Transcript of Binary Trees CS1316: Representing Structure and Behavior.

Page 1: Binary Trees CS1316: Representing Structure and Behavior.

Binary Trees

CS1316: Representing Structure and Behavior

Page 2: Binary Trees CS1316: Representing Structure and Behavior.

Story

Binary Trees Structuring Binary Trees to make them fast for

searching• Binary search trees

• How we insert() in order, how we traverse in order, how we search in order

More than one way to traverse: Pre-order Treating a tree like a list

Page 3: Binary Trees CS1316: Representing Structure and Behavior.

Here’s the question

Searching a list and an array is O(n) We can arrange an array so that

searching it is O(log n)• Binary search of an ordered array.

Lists are harder to arrange to create a faster search.

How about trees?

Page 4: Binary Trees CS1316: Representing Structure and Behavior.

A Binary Tree

A binary tree consists of nodes each of which can have at most two children• A left

• A right

Parent Child

Left Child Right Child

left

right

Page 5: Binary Trees CS1316: Representing Structure and Behavior.

Every node in a binary tree is another tree Parent Child

Left Child Right Child

left

rightParent Child

Left Child Right Child

left

right

Parent Child

Left Child Right Child

left

right

Page 6: Binary Trees CS1316: Representing Structure and Behavior.

Computer scientists know a lot about binary trees

For example, how many levels will you have for n nodes in a binary tree?• Maximum: n

• Minimum: 1+ log n

Page 7: Binary Trees CS1316: Representing Structure and Behavior.

Defining a binary tree

public class TreeNode { private String data; private TreeNode left; private TreeNode right; public TreeNode(String something){ data = something; left = null; right = null; }

Our nodes will contain Strings as their data. Could be anything.

Page 8: Binary Trees CS1316: Representing Structure and Behavior.

Accessors

// Accessors public String getData() {return data;} public void setData(String something){data = something;} public TreeNode getLeft() {return left;} public TreeNode getRight() {return right;} public void setLeft(TreeNode newleft){left = newleft;} public void setRight(TreeNode newright){right = newright;}

Page 9: Binary Trees CS1316: Representing Structure and Behavior.

Defining printing on a node (for a tree, recursively)

public String toString()

{return

"This:"+this.getData()+

" Left: "+this.getLeft() +

" Right: "+this.getRight();}

Page 10: Binary Trees CS1316: Representing Structure and Behavior.

Testing the printing

> TreeNode node1 = new TreeNode("george");

> node1

This:george Left: null Right: null

> TreeNode node1b = new TreeNode("alvin")

> node1.setLeft(node1b)

> node1

This:george Left: This:alvin Left: null Right: null Right: null

Page 11: Binary Trees CS1316: Representing Structure and Behavior.

Creating a binary search tree

Simple rule:• Values on the left

branch are less than values in the parent.

• Values on the right branch are greater than values in the parent.

bear

apple cat

left right

Page 12: Binary Trees CS1316: Representing Structure and Behavior.

Inserting in order in a binary tree

Is the value to insert less than the current node’s value?• Does the left branch have a node already?

If not, put it there.

• If so, insert it on the left. The value to insert must be greater than or

equal to the current node’s value.• Does the right branch have a node already?

If not, put it there.

• If so, insert it on the right.

Page 13: Binary Trees CS1316: Representing Structure and Behavior.

How we compare strings

> "abc".compareTo("abc")0> "abc".compareTo("aaa")1> "abc".compareTo("bbb")-1> "bear".compareTo("bear")0> "bear".compareTo("beat")-2

Page 14: Binary Trees CS1316: Representing Structure and Behavior.

Code

// Insert in order public void insert(TreeNode newOne){ if (this.data.compareTo(newOne.data) > 0){ if (this.getLeft() == null) {this.setLeft(newOne);} else {this.getLeft().insert(newOne);}} else { if (this.getRight() == null) {this.setRight(newOne);} else {this.getRight().insert(newOne);} } }

Page 15: Binary Trees CS1316: Representing Structure and Behavior.

Testing

> TreeNode node1 = new TreeNode("george");> TreeNode node2 = new TreeNode("betty");> TreeNode node3 = new TreeNode("joseph");> TreeNode node4 = new TreeNode("zach");> node1This:george Left: null Right: null> node1.insert(node2)> node1This:george Left: This:betty Left: null Right: null

Right: null

Page 16: Binary Trees CS1316: Representing Structure and Behavior.

Whole tree

> node1.insert(node3)> node1This:george Left: This:betty Left:

null Right: null Right: This:joseph Left: null Right: null

> node1.insert(node4)> node1This:george Left: This:betty Left:

null Right: null Right: This:joseph Left: null Right: This:zach Left: null Right: null

george

betty joseph

zach

Page 17: Binary Trees CS1316: Representing Structure and Behavior.

How do we find things here?

Given something to find:• Is it me? If so, return me.

• Is it less than me? • Is the left null? If so, give up.

• If not, search the left.

• Otherwise• Is the right null? If so, give up.

• If not, search the right.

Page 18: Binary Trees CS1316: Representing Structure and Behavior.

Code

public TreeNode find(String someValue){ if (this.getData().compareTo(someValue) == 0) {return this;} if (this.data.compareTo(someValue) > 0){ if (this.getLeft() == null) {return null;} else {return this.getLeft().find(someValue);}} else { if (this.getRight() == null) {return null;} else {return this.getRight().find(someValue);}} }

Page 19: Binary Trees CS1316: Representing Structure and Behavior.

Testing

> node1.find("betty")

This:betty Left: null Right: null

> node1.find("mark")

null

Page 20: Binary Trees CS1316: Representing Structure and Behavior.

Algorithm order: O(log n)

For a balanced tree! A tree can be

unbalanced and still be a binary search tree.

george

betty joseph

zachgeorge

betty

joseph

zach

Do you see why?

Page 21: Binary Trees CS1316: Representing Structure and Behavior.

How do we print a tree in order?

Is there a left node?• Print the left

Print me Is there a right node?

• Print the right

Page 22: Binary Trees CS1316: Representing Structure and Behavior.

> node1.traverse()" betty george joseph zach"

Code

//In-Order Traversal public String traverse(){ String returnValue = ""; // Visit left if (this.getLeft() != null) {returnValue += "

"+this.getLeft().traverse();} // Visit me returnValue += " "+this.getData(); // Visit right if (this.getRight() != null) {returnValue += "

"+this.getRight().traverse();} return returnValue;}

Page 23: Binary Trees CS1316: Representing Structure and Behavior.

There’s more than one way to traverse…but why?

+

*

3 4

*

x y

Page 24: Binary Trees CS1316: Representing Structure and Behavior.

What’s the in-order traversal of this tree?

+

*

3 4

*

x y

It’s the equation, in the order in which you’d compute it:

(3 * 4) + (x * y)

Page 25: Binary Trees CS1316: Representing Structure and Behavior.

Another traversal: Post-order

Is there a left node?• Print the left

Is there a right node?• Print the right

Print me

Page 26: Binary Trees CS1316: Representing Structure and Behavior.

What’s the post-order traversal of this tree?

+

*

3 4

*

x y

It’s the equation, in the order in which you’d compute it on an HP calculator:

3 4 * x y * +

Page 27: Binary Trees CS1316: Representing Structure and Behavior.

So what’s the big deal?

Binary trees are cute.• Easy to do useful things with them.

• Searching quickly.

• Using them to evaluate equations.

• Operations are easily written small and recursively.

• We know interesting facts about them. Is that all?

• Turns out that binary trees can do ANYTHING

Page 28: Binary Trees CS1316: Representing Structure and Behavior.

Binary trees can represent anything!

We can map an n-ary tree (like a scene graph) to a binary tree.

We can use a binary tree to represent a list.• How about the ability to add to the first and to

the last, like our lists?

Page 29: Binary Trees CS1316: Representing Structure and Behavior.

addFirst() and addLast() public void addFirst(TreeNode newOne){ if (this.getLeft() == null) {this.setLeft(newOne);} else {this.getLeft().addFirst(newOne);} } public void addLast(TreeNode newOne){ if (this.getRight() == null) {this.setRight(newOne);} else {this.getRight().addLast(newOne);} }

Page 30: Binary Trees CS1316: Representing Structure and Behavior.

Playing it out

> TreeNode node1 = new TreeNode("the")

> node1.addFirst(new TreeNode("George of"))

> node1.addLast(new TreeNode("jungle"))

> node1.traverse()

" George of the jungle"

Page 31: Binary Trees CS1316: Representing Structure and Behavior.

Summary Binary trees seem simplified, but can actually

do anything that an n-ary or even a linked list can do.

Binary trees have useful applications (fast searching, equation representations).• A binary search tree places lesser items down the left

branch and greater items down the right.• As long as the tree remains balanced, O(log n)

searching. Binary tree code tends to be small and

recursive.