Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="alert alert-note">
  <h3>Depth-First Binary Tree Traversal</h3>
  <p>
    Visiting the root node, then right subtree and left subtree. Time complexity <strong>O(n)</strong> space complexity <strong> O(log(n))</strong>

    <li>
      <strong> Pre-Order </strong>: root, left and right. <mark>DLR (data,left,right)</mark>
    </li>
    <li>
      <strong> In-Order</strong>: left, root and right. <mark>LDR ( left, data, right)</mark>
    </li>

    <li>
      <strong> Post-Order</strong>: left, right and root. <mark> LRD ( left, right, data)</mark>
    </li>
  </p>

  <img src="https://i.imgur.com/DYkRBqz.png">
  <br>
  <img src="https://i.imgur.com/mbFltXf.png" alt="">
</div>
              
            
!

CSS

              
                .alert {
  padding: 20px;
  border-radius: 25px;
  margin-bottom: 15px;
}

.alert-tip {
  background-color: #def6dd;
}

.alert-note {
  background-color: #efd9fd;
  overflow-x: auto;
}
pre {
  overflow-x: auto;
  white-space: pre-wrap;
  white-space: -moz-pre-wrap;
  white-space: -pre-wrap;
  white-space: -o-pre-wrap;
  word-wrap: break-word;
}

              
            
!

JS

              
                /* 
Nodes are created in the heap seciton of the memory 
Address of the root node we keep for a tree.
*/

class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

/*
 DLR ( data, left, right)
*/
function preOrder(root) {
  if (root == null) return;
  visit(root);
  preOrder(root.left);
  preOrder(root.right);
}
/*
 LDR ( left, data, right)
*/
function inOrder(root) {
  if (root == null) return;
  inOrder(root.left);
  visit(root);
  inOrder(root.right);
}
/*
  LRD ( left, right, data)
*/
function postOrder(root) {
  if (root == null) return;
  postOrder(root.left);
  postOrder(root.right);
  visit(root);
}

let result = [];

function visit(node) {
  console.log(node.data);
  result.push(node.data);
}

// unit tests
// do not modify the below code
describe("Depth-First Traversal", () => {
  let root = null;

  beforeEach(() => {
    result = [];
    root = null;
    root = insert(root, 15);
    root = insert(root, 10);
    root = insert(root, 20);
    root = insert(root, 25);
    root = insert(root, 8);
    root = insert(root, 12);
  });

  it("preOrder should work correctly", () => {
    preOrder(root);
    expect(result).toEqual([15, 10, 8, 12, 20, 25]);
  });

  it("inOrder should work correctly", () => {
    inOrder(root);
    expect(result).toEqual([8, 10, 12, 15, 20, 25]);
  });

  it("postOrder should work correctly", () => {
    postOrder(root);
    expect(result).toEqual([8, 12, 10, 25, 20, 15]);
  });
});

function insert(root, data) {
  // If the tree is empty create new node.
  let notToInsert = new Node(data);

  let current = root;
  let previous = null;
  while (current != null) {
    previous = current;
    if (data <= current.data) current = current.left;
    else current = current.right;
  }

  if (previous == null) return notToInsert;
  else if (data <= previous.data) previous.left = notToInsert;
  else previous.right = notToInsert;

  return root;
}

              
            
!
999px

Console