Binary Tree

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/44

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

45 Terms

1
New cards

binary tree

a tree-like hierarchical data structure such that each NODE stores a value (instance var value) and has at most two children: left and right (instance node vars)

  • the left node stores a value less than the value of the node

  • the right node stores a value greater than the value of the node

<p>a tree-like hierarchical data structure such that each NODE stores a value (instance var value) and has at most two children: left and right (instance node vars)</p><ul><li><p>the left node stores a value less than the value of the node</p></li><li><p>the right node stores a value greater than the value of the node</p></li></ul><p></p>
2
New cards

all nodes except ONE have exactly ______

one parent

3
New cards

a node that has no parent is called the _____

root

  • drawn at the top of the diagram

<p>root</p><ul><li><p>drawn at the top of the diagram</p></li></ul><p></p>
4
New cards

each node has ________ children

0, 1, or 2 children

5
New cards

nodes with no children are called _______

leaves (or external nodes)

  • in the image, 29 and 11 would be considered leaves

<p>leaves (or external nodes)</p><ul><li><p>in the image, 29 and 11 would be considered leaves</p></li></ul><p></p>
6
New cards

links between nodes are called ________

branches

7
New cards

a node and its descendents is called a ______

subtree

8
New cards

size of a binary tree

how many nodes are in the binary tree

  • an EMPTY tree (no nodes) has size 0

9
New cards

binary trees can be defined ________

recursively

10
New cards

depth of a node

the number of links starting from the root that must be followed to reach that node.

  • the root is the most accessible node. → the depth of a root is always 0

  • the depth of “15” in the image is 1

<p>the number of links starting from the root that must be followed to reach that node.</p><ul><li><p>the root is the most accessible node. → the depth of a root is always 0</p></li><li><p>the depth of “15” in the image is 1</p></li></ul><p></p>
11
New cards

depth of a tree

depth of the deepest node

  • here, the depth of the tree would be 3 (the level where “11” is"

<p>depth of the deepest node</p><ul><li><p>here, the depth of the tree would be 3 (the level where “11” is"</p></li></ul><p></p>
12
New cards

binary seach tree

a binary search tree is a binary tree data structure such that:

  • the nodes of a LEFT sub-tree only have elements that are less than the element stored at the local root of the subtree (or is empty)

  • the nodes of a RIGHT subtree only have elems that are GREATER than the elem stored at the local root (or is empty)

  • this means that a binary search tree has NO duplicates and is always ORDERED

13
New cards

binary search tree instance vars and nested node class

knowt flashcard image
14
New cards

binary search tree memory diagram of a node

  • note that this is a memory diagram of the root node with no children

<ul><li><p>note that this is a memory diagram of the root node with no children </p></li></ul><p></p>
15
New cards

binary search tree memory diagram of a tree

  • each node has references to their left/right nodes (some refs are null, some are not)

<ul><li><p>each node has references to their left/right nodes (some refs are null, some are not)</p></li></ul><p></p>
16
New cards

leaf node

a node where both left and right node references are null

17
New cards

how to tell if the tree is empty

if the root ref is null

18
New cards

boolean contains(E obj) (recursive implementation)

  • returns true if the value is in binary tree

  • “contains(obj, root)” calls the recursive method, which is a private method that can only be called from inside the class

<ul><li><p>returns true if the value is in binary tree</p></li><li><p>“contains(obj, root)” calls the recursive method, which is a private method that can only be called from inside the class </p></li></ul><p></p>
19
New cards

recursive list processing main components

  • starter method → calls the recursive method

  • recursive method → almost always has one more parameter than the starter method

20
New cards

boolean contains(Node<E> current, E obj) (recursive implementation)

  • the base cases:

    • if the elem is not in the binary tree (current == null)

    • if the elem is found (obj.compareTo(current.value) == 0)

  • the recursive cases

    • if the value of obj is less than the current node (recursive call current.left)

    • if the value of obj is greater than the current node (recursive call current.right)

<ul><li><p>the base cases: </p><ul><li><p>if the elem is not in the binary tree (current == null)</p></li><li><p>if the elem is found (obj.compareTo(current.value) == 0) </p></li></ul></li><li><p>the recursive cases</p><ul><li><p>if the value of obj is less than the current node (recursive call current.left)</p></li><li><p>if the value of obj is greater than the current node (recursive call current.right) </p></li></ul></li></ul><p></p>
21
New cards

boolean contains(E obj) (non-recursive implementation)

knowt flashcard image
22
New cards

three different ways to traverse a binary tree

  1. pre-order: current, left, right

  2. in-order: left, current, right

  3. post-order: left, right, current

23
New cards

starter methods for tree traversal

  • visit method is for printing the value of the node

  • the order methods all call recursive methods that have the same name

<ul><li><p>visit method is for printing the value of the node</p></li><li><p>the order methods all call recursive methods that have the same name</p></li></ul><p></p>
24
New cards

binary tree traversal: pre-order recursive method

knowt flashcard image
25
New cards

binary tree traversal: in-order recursive method

  • remember that IN-ORDER prints the elems in the order of least to greatest (moving from left to right)

<ul><li><p>remember that IN-ORDER prints the elems in the order of least to greatest (moving from left to right) </p></li></ul><p></p>
26
New cards

binary tree traversal: post-order recursive method

  • go to the left-most node

  • then go to the right most-node

  • then go to root

  • and repeat for subtrees

  • check that you have accounted for EVERY NODE

<ul><li><p>go to the left-most node</p></li><li><p>then go to the right most-node</p></li><li><p>then go to root</p></li><li><p>and repeat for subtrees</p></li><li><p>check that you have accounted for EVERY NODE</p></li></ul><p></p>
27
New cards

full binary tree

a full binary tree is when all nodes have exactly TWO CHILDREN except for the leaves

  • meaning no nodes have just one child

<p>a full binary tree is when all nodes have exactly TWO CHILDREN except for the leaves</p><ul><li><p>meaning no nodes have just one child </p></li></ul><p></p>
28
New cards

a balanced binary tree

  • a binary tree of depth d is BALANCED if all the nodes at a depth <= d - 2 have exactly two children (not zero or one)

  • the tree in the image is BALANCED bc the depth of the tree is 3, so every depth <= 1 has exactly 2 children

<ul><li><p>a binary tree of depth d is BALANCED if all the nodes at a depth &lt;= d - 2 have exactly two children (not zero or one)</p></li><li><p>the tree in the image is BALANCED bc the depth of the tree is 3, so every depth &lt;= 1 has exactly 2 children</p></li></ul><p></p>
29
New cards

a balanced binary tree of depth d has

2^d to ((2^(d+1)) - 1) nodes

→ the size = 2^d to ((2^(d+1)) - 1)

30
New cards

the DEPTH of a balanced binary tree of size n (number of nodes in tree)

log2(n)

31
New cards

the relationship b/w efficiency and whether or not the tree is balanced for the contains method

  • a balanced tree would have better efficiency since the contains method eliminates a whole subtree with each comparison (divides the remaining nodes to traverse by 2, hence the efficiency being log2(n)

32
New cards

methods that work better with recursion

  • methods that follow a single path on the tree (from the root to some node) can easily be implemented w/o recursion

  • methods that must visit more than one-subtree simultaneously are better w recursion (like toString which prints the tree out like an array)

33
New cards

boolean add(E obj): special case

  • method adds an elem to the tree

  • very similar to the contains method with the comparisons

  • special case is if the tree is empty so the elem you add becomes the root → root = new Node<E>(obj)

<ul><li><p>method adds an elem to the tree</p></li><li><p>very similar to the contains method with the comparisons</p></li><li><p>special case is if the tree is empty so the elem you add becomes the root → root = new Node&lt;E&gt;(obj)</p></li></ul><p></p>
34
New cards

boolean add(E obj): general case

  • like the contains method, add follows a single path from the root to some node, so it does not need a recursive implementation

  • always replaces null by a new value

  • the structure of the tree is unchanged

<ul><li><p>like the contains method, add follows a single path from the root to some node, so it does not need a recursive implementation</p></li><li><p>always replaces null by a new value</p></li><li><p>the structure of the tree is unchanged</p></li></ul><p></p>
35
New cards

Node<E> remove(E obj): 4 main cases

  1. node (whose value == obj) has no sub-trees → remove the sub-tree

  2. node has a left sub-tree ONLY → replace the node by that sub-tree

  3. node has a right sub-tree ONLY → replace the node by that sub-tree

  4. the node has two sub-trees → either replace with the value that immediately precedes (the value less than) or replace with the value that immediately follows (the value greater than)

consider that each case can have the value be the root, in which the replacement node must be referred to as the root

36
New cards

Node<E> remove(E obj): case for removing the root

  • removeTopMost(Node<E> current) is where the bulk of the code is

<ul><li><p>removeTopMost(Node&lt;E&gt; current) is where the bulk of the code is </p></li></ul><p></p>
37
New cards

Node<E> remove(E obj): pre-conditions

  • the obj must not be null

  • the tree must not be empty

<ul><li><p>the obj must not be null</p></li><li><p>the tree must not be empty</p></li></ul><p></p>
38
New cards

Node<E> remove(E obj): case for if the obj is not found at root

knowt flashcard image
39
New cards

Node<E> remove(E obj): general case for finding the node to remove

  • first if-statement is for if the obj-to-be-removed is found

  • second if-statement is for if you still need to traverse

<ul><li><p>first if-statement is for if the obj-to-be-removed is found</p></li><li><p>second if-statement is for if you still need to traverse</p></li></ul><p></p>
40
New cards

Node<E> removeTopMost(Node<E> current)

  • first checks if “current” only has one subtree

<ul><li><p>first checks if “current” only has one subtree</p></li></ul><p></p>
41
New cards

E getLeftMost(Node<E> current)

→ recursively finds the left-most node

<p>→ recursively finds the left-most node</p>
42
New cards

Node<E> removeLeftMost(Node<E> current)

→ recursively finds and removes the left-most node

  • “parent” always points to the node that was the previous node that “current” pointed to

  • replaces the left-most node with its right neighbour

<p>→ recursively finds and removes the left-most node</p><ul><li><p>“parent” always points to the node that was the previous node that “current” pointed to</p></li><li><p>replaces the left-most node with its right neighbour</p></li></ul><p></p>
43
New cards

PreOrderIterator

  • this code does not include the next() and hasNext() methods

<ul><li><p>this code does not include the next() and hasNext() methods</p></li></ul><p></p>
44
New cards

PreOrderIterator nested class: boolean hasNext()

knowt flashcard image
45
New cards

PreOrderIterator nested class: E next()

knowt flashcard image