So you think you're hot stuff?
Reverse THIS
So you think you're hot stuff?
Other urls found in this thread:
learnlispthehardway.org
gigamonkeys.com
gigamonkeys.com
stackoverflow.com
en.wikipedia.org
en.wikipedia.org
en.wikipedia.org
twitter.com
import binarytreeutils as butt
butt.reverse()
> coders
Do a pre-order traversal, then insert each element into the data structure with a reversed predicate.
Otherwise, just reverse the in-order sequence.
I'm not being careful, so probably what I'm saying is not entirely correct.
def reverse_tree(root):
if root.left is None and root.right is None:
return
else:
if root.left is not None:
root.right = reverse_tree(root.left)
if root_right is not None:
root_left = reverse_tree(root.right)
return
I'm too lazy to test this right now, but I think this is how you do it in Python.
def reverse_tree(root):
if root.left is not None:
root.right = reverse_tree(root.left)
if root_right is not None:
root_left = reverse_tree(root.right)
return
Cleaned it up
...
def reverse_tree(root):
if root.left is not None:
root.right = reverse_tree(root.left)
if root.right is not None:
root.left = reverse_tree(root.right)
return
Whoops found typo.
def reverse_tree(root):
if root.left is not None:
root.right = reverse_tree(root.left)
if root.right is not None:
root.left = reverse_tree(root.right)
return root
And another typo fixed. I'm amazed I'm still this awake after 3.
Fixed.
Does it work?
You're overwriting right with left, and then overwriting left with right. You need a temporary variable for a subtree or alternatively use a swap-function.
>ITT: People that can't swap values.
These.
CL-USER> (defun reverse-tree (tree)
(symbol-macrolet ((l (second tree))
(r (third tree)))
(rotatef l r)
(when r (reverse-tree r))
(when l (reverse-tree l)))
tree)
CL-USER> (reverse-tree '(8 (3 (1 nil nil) (6 (4 nil nil) (7 nil nil))) (10 nil (14 (13 nil nil) nil))))
(8 (10 (14 NIL (13 NIL NIL)) NIL) (3 (6 (7 NIL NIL) (4 NIL NIL)) (1 NIL NIL)))
>inb4 nil nil))nil)))nil)))
Lisp people are funny)))
I'm thorry, ith that tharcathm?
Now with atom leaves for brevity:
CL-USER> (defun reverse-tree (tree)
(symbol-macrolet ((l (second tree))
(r (third tree)))
(rotatef l r)
(macrolet ((nodep (n) (and n (listp n))))
(when (nodep r) (reverse-tree r))
(when (nodep l) (reverse-tree l))))
tree)
CL-USER> (reverse-tree '(8 (3 1 (6 4 7)) (10 nil (14 13 nil))))
(8 (10 NIL (14 13 NIL)) (3 1 (6 4 7)))
>2017
>Not using neural networks for this type of tasks
And golfing a bit:
(defun reverse-tree (tree)
(when (consp tree)
(symbol-macrolet ((l (second tree))
(r (third tree)))
(rotatef l r)
(reverse-tree r)
(reverse-tree l)))
tree)
The indentation is a column off here and there, but fuck it.
[obligatory we don't do your homework post]
define reverse, nigger
And for the OOP in OOL faggots
(defclass tree ()
((content :accessor content :initarg :content)))
(defclass leaf (tree) ())
(defclass node (leaf)
((left :accessor left :initarg :left)
(right :accessor right :initarg :right)))
(defun make-tree (args)
(if (consp args)
(make-instance 'node
:content (first args)
:left (make-tree (second args))
:right (make-tree (third args)))
(make-instance 'leaf :content args)))
(defmethod print-object ((leaf leaf) s)
(format s "~s" (content leaf)))
(defmethod print-object ((node node) s)
(format s "(~a ~a ~a)" (content node) (left node) (right node)))
(defmethod reverse-tree ((leaf leaf)) leaf)
(defmethod reverse-tree ((node node))
(with-slots (left right) node
(rotatef left right)
(reverse-tree left)
(reverse-tree right))
node)
CL-USER> (make-tree '(8 (3 1 (6 4 7)) (10 nil (14 13 nil))))
(8 (3 1 (6 4 7)) (10 NIL (14 13 NIL)))
CL-USER> (reverse-tree (make-tree '(8 (3 1 (6 4 7)) (10 nil (14 13 nil)))))
(8 (10 (14 NIL 13) NIL) (3 (6 7 4) 1))
What esoteric programming language is this?
lisp I think
Common Lisp, not esoteric at all, just a tad obscure nowadays
Why would I wanna waste my time learning that shit?
gigamonkeys.com
The very first chapter explains why in just a couple of minutes.
public void invertTree(Node node) {
if (node == null) {
return;
}
Node temp = node.left;
node.left = node.right;
node.right = temp;
invertTree(node.left);
invertTree(node.right);
}
h-how did i do gee??
If you have ADHD, PTSD, or some other kind of short-sighted autism, you could try reading this instead:
stackoverflow.com
>m-my language gets shit done quicker!
That's pretty much what every language out there claims.
Name 1 (one) relevant piece of software written in LISP. I'll wait.
en.wikipedia.org
en.wikipedia.org
These lists are, of course, vastly incomplete
SwapChildren(Node* n)
{
if(!n) return;
swap(n->left, n->right);
SwapChildren(n->left);
SwapChildren(n->right);
}
reverse :: Tree a -> Tree a
reverse Nil = Nil
reverse (Node x l r) = Node x (reverse r) (reverse l)
>outdated military shit
Not relevant.
Don't see anything relevant on those lists. Is that all you got?
Man, Haskell is the pinnacle of elegancy.
Wish I could find a use case for it outside of university assignments.
...
>half a dozen things you've never even heard of happened to be written in LISP
>an entire world of mainstream software out there is written in other programming languages
>"LEL LISP IS DA BEST"
Maximum autism.
you're inevitably going to run into a situation where you'll really wish you were working in a procedural language instead
just forget about it
>BLABLA POWERFULL BLABLA HOMOICONIC BLABLA MACROS BLABLA MEME DSLS TO MAKE THE CODE EVEN MORE UNREADABLE BLABLA PRINCE OF ALL SAY- PROGRAMMING LANGUAGES
>muh (((java))) runz ebarywer so its de best obdion :D
>Hush little niglet, don't say a word
>And never mind that noise jew heard
>It's just ((((the beast)))) under jewr bed
>In jewr closet, in jewr head
There's always a reader-macro for you if you want m-expressions and brackets
And then the next idiot to maintain the code needs to decipher it.
Any Lisp dialect this side of the 70's is a breeze to read
Quality post.
Thankth
>Any Lisp dialect this side of the 70's is a breeze to read
Unless you are deliberately illiterate, of course
You have to be deliberately retarded to believe that, of course.
Pray do tell, what is your language of choice?
Depends. Most of them aren't production ready, yet.